add ability to delete lists

This commit is contained in:
Dan Anglin 2024-02-27 12:35:14 +00:00
parent 6fed0edf4d
commit b978a131e4
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 84 additions and 15 deletions

View file

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

65
cmd/enbas/delete.go Normal file
View file

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

View file

@ -23,17 +23,19 @@ func run() error {
const (
login string = "login"
version string = "version"
show string = "show"
showResource string = "show"
switchAccount string = "switch"
create string = "create"
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",
showResource: "print details about a specified resource",
switchAccount: "switch to an account",
create: "create a specific resource",
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)