checkpoint: add command
This commit is contained in:
parent
7f86c593fa
commit
de573416b5
3 changed files with 93 additions and 0 deletions
72
cmd/enbas/add.go
Normal file
72
cmd/enbas/add.go
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
type addCommand struct {
|
||||||
|
*flag.FlagSet
|
||||||
|
|
||||||
|
toResourceType string
|
||||||
|
listID string
|
||||||
|
accountIDs accountIDs
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAddCommand(name, summary string) *addCommand {
|
||||||
|
emptyArr := make([]string, 0, 3)
|
||||||
|
|
||||||
|
command := addCommand{
|
||||||
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||||
|
accountIDs: accountIDs(emptyArr),
|
||||||
|
}
|
||||||
|
|
||||||
|
command.StringVar(&command.toResourceType, "add-to", "", "specify the type of resource to add to")
|
||||||
|
command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to add to")
|
||||||
|
command.Var(&command.accountIDs, "account-id", "the ID of the account to add to the list")
|
||||||
|
|
||||||
|
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
||||||
|
|
||||||
|
return &command
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *addCommand) Execute() error {
|
||||||
|
if c.toResourceType == "" {
|
||||||
|
return flagNotSetError{flagText: "add-to"}
|
||||||
|
}
|
||||||
|
|
||||||
|
funcMap := map[string]func(*client.Client) error{
|
||||||
|
listResource: c.addAccountsToList,
|
||||||
|
}
|
||||||
|
|
||||||
|
doFunc, ok := funcMap[c.toResourceType]
|
||||||
|
if !ok {
|
||||||
|
return unsupportedResourceTypeError{resourceType: c.toResourceType}
|
||||||
|
}
|
||||||
|
|
||||||
|
gtsClient, err := client.NewClientFromConfig()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to create the GoToSocial client; %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return doFunc(gtsClient)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *addCommand) addAccountsToList(gtsClient *client.Client) error {
|
||||||
|
if c.listID == "" {
|
||||||
|
return flagNotSetError{flagText: listIDFlag}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(c.accountIDs) == 0 {
|
||||||
|
return errors.New("no account IDs has been specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := gtsClient.AddAccountsToList(c.listID, []string(c.accountIDs)); err != nil {
|
||||||
|
return fmt.Errorf("unable to add the accounts to the list; %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
17
cmd/enbas/flags.go
Normal file
17
cmd/enbas/flags.go
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
type accountIDs []string
|
||||||
|
|
||||||
|
func (a *accountIDs) String() string {
|
||||||
|
return strings.Join(*a, ", ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *accountIDs) Set(value string) error {
|
||||||
|
if len(value) > 0 {
|
||||||
|
*a = append(*a, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -52,6 +52,7 @@ func run() error {
|
||||||
deleteResource string = "delete"
|
deleteResource string = "delete"
|
||||||
updateResource string = "update"
|
updateResource string = "update"
|
||||||
whoami string = "whoami"
|
whoami string = "whoami"
|
||||||
|
add string = "add"
|
||||||
)
|
)
|
||||||
|
|
||||||
summaries := map[string]string{
|
summaries := map[string]string{
|
||||||
|
@ -63,6 +64,7 @@ func run() error {
|
||||||
deleteResource: "delete a specific resource",
|
deleteResource: "delete a specific resource",
|
||||||
updateResource: "update a specific resource",
|
updateResource: "update a specific resource",
|
||||||
whoami: "print the account that you are currently logged in to",
|
whoami: "print the account that you are currently logged in to",
|
||||||
|
add: "add a resource to another resource",
|
||||||
}
|
}
|
||||||
|
|
||||||
flag.Usage = enbasUsageFunc(summaries)
|
flag.Usage = enbasUsageFunc(summaries)
|
||||||
|
@ -97,6 +99,8 @@ func run() error {
|
||||||
executor = newUpdateCommand(updateResource, summaries[updateResource])
|
executor = newUpdateCommand(updateResource, summaries[updateResource])
|
||||||
case whoami:
|
case whoami:
|
||||||
executor = newWhoAmICommand(whoami, summaries[whoami])
|
executor = newWhoAmICommand(whoami, summaries[whoami])
|
||||||
|
case add:
|
||||||
|
executor = newAddCommand(add, summaries[add])
|
||||||
default:
|
default:
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
return fmt.Errorf("unknown subcommand %q", subcommand)
|
return fmt.Errorf("unknown subcommand %q", subcommand)
|
||||||
|
|
Loading…
Reference in a new issue