add ability to update lists

This commit is contained in:
Dan Anglin 2024-02-27 13:36:27 +00:00
parent b978a131e4
commit b300e74409
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 97 additions and 3 deletions

View file

@ -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")
}

View file

@ -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)

90
cmd/enbas/update.go Normal file
View file

@ -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
}