Dan Anglin
55b93c6586
Move all executors to the internal folder package. This PR also comes with additional breaking changes. Changes: - refactor: move all executors to the internal/executor package. - refactor: update naming patterns for constants, variables, custom types, etc. - fix(breaking): renamed the update command to edit. - fix(breaking): update the flags for the switch command to make it more generic. - fix(breaking): renamed the show-account-relationship flag to show-relationship. - fix: update the print message from the whoami command.
77 lines
2 KiB
Go
77 lines
2 KiB
Go
package executor
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
|
)
|
|
|
|
type CreateExecutor struct {
|
|
*flag.FlagSet
|
|
|
|
topLevelFlags TopLevelFlags
|
|
resourceType string
|
|
listTitle string
|
|
listRepliesPolicy string
|
|
}
|
|
|
|
func NewCreateExecutor(tlf TopLevelFlags, name, summary string) *CreateExecutor {
|
|
createExe := CreateExecutor{
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
|
|
topLevelFlags: tlf,
|
|
}
|
|
|
|
createExe.StringVar(&createExe.resourceType, flagType, "", "specify the type of resource to create")
|
|
createExe.StringVar(&createExe.listTitle, flagListTitle, "", "specify the title of the list")
|
|
createExe.StringVar(&createExe.listRepliesPolicy, flagListRepliesPolicy, "list", "specify the policy of the replies for this list (valid values are followed, list and none)")
|
|
|
|
createExe.Usage = commandUsageFunc(name, summary, createExe.FlagSet)
|
|
|
|
return &createExe
|
|
}
|
|
|
|
func (c *CreateExecutor) Execute() error {
|
|
if c.resourceType == "" {
|
|
return FlagNotSetError{flagText: flagType}
|
|
}
|
|
|
|
gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.ConfigDir)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to create the GoToSocial client; %w", err)
|
|
}
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
|
resourceList: c.createList,
|
|
}
|
|
|
|
doFunc, ok := funcMap[c.resourceType]
|
|
if !ok {
|
|
return UnsupportedTypeError{resourceType: c.resourceType}
|
|
}
|
|
|
|
return doFunc(gtsClient)
|
|
}
|
|
|
|
func (c *CreateExecutor) createList(gtsClient *client.Client) error {
|
|
if c.listTitle == "" {
|
|
return FlagNotSetError{flagText: flagListTitle}
|
|
}
|
|
|
|
repliesPolicy, err := model.ParseListRepliesPolicy(c.listRepliesPolicy)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to parse the list replies policy; %w", err)
|
|
}
|
|
|
|
list, err := gtsClient.CreateList(c.listTitle, repliesPolicy)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to create the list; %w", err)
|
|
}
|
|
|
|
fmt.Println("Successfully created the following list:")
|
|
fmt.Printf("\n%s\n", list)
|
|
|
|
return nil
|
|
}
|