From b300e744092122a15966513cf3d5992798205e91 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 27 Feb 2024 13:36:27 +0000 Subject: [PATCH] add ability to update lists --- cmd/enbas/delete.go | 6 +-- cmd/enbas/main.go | 4 ++ cmd/enbas/update.go | 90 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 cmd/enbas/update.go diff --git a/cmd/enbas/delete.go b/cmd/enbas/delete.go index 906131a..4d2e156 100644 --- a/cmd/enbas/delete.go +++ b/cmd/enbas/delete.go @@ -39,18 +39,18 @@ func (c *deleteCommand) Execute() error { } funcMap := map[string]func(*client.Client) error{ - "lists": c.deleteLists, + "lists": c.deleteList, } doFunc, ok := funcMap[c.resourceType] if !ok { - return fmt.Errorf("unsupported type %q", c.resourceType) + return fmt.Errorf("unsupported resource type %q", c.resourceType) } return doFunc(gtsClient) } -func (c *deleteCommand) deleteLists(gtsClient *client.Client) error { +func (c *deleteCommand) deleteList(gtsClient *client.Client) error { if c.listID == "" { return errors.New("the list-id flag is not set") } diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index 3bc288c..d0e658f 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -27,6 +27,7 @@ func run() error { switchAccount string = "switch" createResource string = "create" deleteResource string = "delete" + updateResource string = "update" ) summaries := map[string]string{ @@ -36,6 +37,7 @@ func run() error { switchAccount: "switch to an account", createResource: "create a specific resource", deleteResource: "delete a specific resource", + updateResource: "update a specific resource", } flag.Usage = enbasUsageFunc(summaries) @@ -66,6 +68,8 @@ func run() error { executor = newCreateCommand(createResource, summaries[createResource]) case deleteResource: executor = newDeleteCommand(deleteResource, summaries[deleteResource]) + case updateResource: + executor = newUpdateCommand(updateResource, summaries[updateResource]) default: flag.Usage() return fmt.Errorf("unknown subcommand %q", subcommand) diff --git a/cmd/enbas/update.go b/cmd/enbas/update.go new file mode 100644 index 0000000..a4afdb4 --- /dev/null +++ b/cmd/enbas/update.go @@ -0,0 +1,90 @@ +package main + +import ( + "errors" + "flag" + "fmt" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/client" + "codeflow.dananglin.me.uk/apollo/enbas/internal/model" +) + +type updateCommand struct { + *flag.FlagSet + + resourceType string + listID string + listTitle string + listRepliesPolicy string +} + +func newUpdateCommand(name, summary string) *updateCommand { + command := updateCommand{ + FlagSet: flag.NewFlagSet(name, flag.ExitOnError), + } + + command.StringVar(&command.resourceType, "type", "", "specify the type of resource to update") + command.StringVar(&command.listID, "list-id", "", "specify the ID of the list to update") + command.StringVar(&command.listTitle, "list-title", "", "specify the title of the list") + command.StringVar(&command.listRepliesPolicy, "list-replies-policy", "", "specify the policy of the replies for this list (valid values are followed, list and none)") + + command.Usage = commandUsageFunc(name, summary, command.FlagSet) + + return &command +} + +func (c *updateCommand) 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.updateList, + } + + doFunc, ok := funcMap[c.resourceType] + if !ok { + return fmt.Errorf("unsupported resource type %q", c.resourceType) + } + + return doFunc(gtsClient) +} + +func (c *updateCommand) updateList(gtsClient *client.Client) error { + if c.listID == "" { + return errors.New("the list-id flag is not set") + } + + list, err := gtsClient.GetList(c.listID) + if err != nil { + return fmt.Errorf("unable to get the list; %w", err) + } + + if c.listTitle != "" { + list.Title = c.listTitle + } + + if c.listRepliesPolicy != "" { + repliesPolicy, err := model.ParseListRepliesPolicy(c.listRepliesPolicy) + if err != nil { + return fmt.Errorf("unable to parse the list replies policy; %w", err) + } + + list.RepliesPolicy = repliesPolicy + } + + updatedList, err := gtsClient.UpdateList(list) + if err != nil { + return fmt.Errorf("unable to update the list; %w", err) + } + + fmt.Println("Successfully updated the list.") + fmt.Printf("\n%s\n", updatedList) + + return nil +}