From 4db116121c3f5d4e786f6a9dd002a1e526847cfe Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sat, 18 May 2024 10:37:56 +0100 Subject: [PATCH] checkpoint: add remove command, may be incomplete (it has been a while) --- cmd/enbas/remove.go | 72 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 cmd/enbas/remove.go diff --git a/cmd/enbas/remove.go b/cmd/enbas/remove.go new file mode 100644 index 0000000..72c02fb --- /dev/null +++ b/cmd/enbas/remove.go @@ -0,0 +1,72 @@ +package main + +import ( + "errors" + "flag" + "fmt" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/client" +) + +type removeCommand struct { + *flag.FlagSet + + fromResourceType string + listID string + accountIDs accountIDs +} + +func newRemoveCommand(name, summary string) *removeCommand { + emptyArr := make([]string, 0, 3) + + command := removeCommand{ + FlagSet: flag.NewFlagSet(name, flag.ExitOnError), + accountIDs: accountIDs(emptyArr), + } + + command.StringVar(&command.fromResourceType, "remove-from", "", "specify the type of resource to remove from") + command.StringVar(&command.listID, listIDFlag, "", "the ID of the list to remove from") + command.Var(&command.accountIDs, "account-id", "the ID of the account to remove from the list") + + command.Usage = commandUsageFunc(name, summary, command.FlagSet) + + return &command +} + +func (c *removeCommand) Execute() error { + if c.fromResourceType == "" { + return flagNotSetError{flagText: "remove-from"} + } + + funcMap := map[string]func(*client.Client) error{ + listResource: c.removeAccountsFromList, + } + + doFunc, ok := funcMap[c.fromResourceType] + if !ok { + return unsupportedResourceTypeError{resourceType: c.fromResourceType} + } + + gtsClient, err := client.NewClientFromConfig() + if err != nil { + return fmt.Errorf("unable to create the GoToSocial client; %w", err) + } + + return doFunc(gtsClient) +} + +func (c *removeCommand) removeAccountsFromList(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.RemoveAccountsFromList(c.listID, []string(c.accountIDs)); err != nil { + return fmt.Errorf("unable to remove the accounts from the list; %w", err) + } + + return nil +}