diff --git a/cmd/enbas/create.go b/cmd/enbas/create.go index 49550cd..51da691 100644 --- a/cmd/enbas/create.go +++ b/cmd/enbas/create.go @@ -22,7 +22,7 @@ func newCreateCommand(name, summary string) *createCommand { FlagSet: flag.NewFlagSet(name, flag.ExitOnError), } - command.StringVar(&command.resourceType, "type", "", "specify the type of resource to display") + command.StringVar(&command.resourceType, "type", "", "specify the type of resource to create") command.StringVar(&command.listTitle, "list-title", "", "specify the title of the list") command.StringVar(&command.listRepliesPolicy, "list-replies-policy", "list", "specify the policy of the replies for this list (valid values are followed, list and none)") diff --git a/cmd/enbas/delete.go b/cmd/enbas/delete.go new file mode 100644 index 0000000..906131a --- /dev/null +++ b/cmd/enbas/delete.go @@ -0,0 +1,65 @@ +package main + +import ( + "errors" + "flag" + "fmt" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/client" +) + +type deleteCommand struct { + *flag.FlagSet + + resourceType string + listID string +} + +func newDeleteCommand(name, summary string) *deleteCommand { + command := deleteCommand{ + FlagSet: flag.NewFlagSet(name, flag.ExitOnError), + } + + command.StringVar(&command.resourceType, "type", "", "specify the type of resource to delete") + command.StringVar(&command.listID, "list-id", "", "specify the ID of the list to delete") + + command.Usage = commandUsageFunc(name, summary, command.FlagSet) + + return &command +} + +func (c *deleteCommand) Execute() error { + if c.resourceType == "" { + return errors.New("the type field is not set") + } + + gtsClient, err := client.NewClientFromConfig() + if err != nil { + return fmt.Errorf("unable to create the GoToSocial client; %w", err) + } + + funcMap := map[string]func(*client.Client) error{ + "lists": c.deleteLists, + } + + doFunc, ok := funcMap[c.resourceType] + if !ok { + return fmt.Errorf("unsupported type %q", c.resourceType) + } + + return doFunc(gtsClient) +} + +func (c *deleteCommand) deleteLists(gtsClient *client.Client) error { + if c.listID == "" { + return errors.New("the list-id flag is not set") + } + + if err := gtsClient.DeleteList(c.listID); err != nil { + return fmt.Errorf("unable to delete the list; %w", err) + } + + fmt.Println("The list was successfully deleted.") + + return nil +} diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index a5d6fd8..3bc288c 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -21,19 +21,21 @@ func main() { func run() error { const ( - login string = "login" - version string = "version" - show string = "show" - switchAccount string = "switch" - create string = "create" + login string = "login" + version string = "version" + showResource string = "show" + switchAccount string = "switch" + createResource string = "create" + deleteResource string = "delete" ) summaries := map[string]string{ - login: "login to an account on GoToSocial", - version: "print the application's version and build information", - show: "print details about a specified resource", - switchAccount: "switch to an account", - create: "create a specific resource", + login: "login to an account on GoToSocial", + version: "print the application's version and build information", + showResource: "print details about a specified resource", + switchAccount: "switch to an account", + createResource: "create a specific resource", + deleteResource: "delete a specific resource", } flag.Usage = enbasUsageFunc(summaries) @@ -56,12 +58,14 @@ func run() error { executor = newLoginCommand(login, summaries[login]) case version: executor = newVersionCommand(version, summaries[version]) - case show: - executor = newShowCommand(show, summaries[show]) + case showResource: + executor = newShowCommand(showResource, summaries[showResource]) case switchAccount: executor = newSwitchCommand(switchAccount, summaries[switchAccount]) - case create: - executor = newCreateCommand(create, summaries[create]) + case createResource: + executor = newCreateCommand(createResource, summaries[createResource]) + case deleteResource: + executor = newDeleteCommand(deleteResource, summaries[deleteResource]) default: flag.Usage() return fmt.Errorf("unknown subcommand %q", subcommand)