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 boostable bool federated bool likeable bool replyable bool sensitive bool content string contentType string language string spoilerText string resourceType string listTitle string listRepliesPolicy string visibility string } func NewCreateExecutor(tlf TopLevelFlags, name, summary string) *CreateExecutor { createExe := CreateExecutor{ FlagSet: flag.NewFlagSet(name, flag.ExitOnError), topLevelFlags: tlf, } createExe.BoolVar(&createExe.boostable, "boostable", true, "specify if the status can be reposted/boosted by others") createExe.BoolVar(&createExe.federated, "federated", true, "specify if the status can be federated beyond the local timelines") createExe.BoolVar(&createExe.likeable, "likeable", true, "specify if the status can be liked/favourited") createExe.BoolVar(&createExe.replyable, "replyable", true, "specify if the status can be replied to") createExe.BoolVar(&createExe.sensitive, "sensitive", false, "specify if the status should be marked as sensitive") createExe.StringVar(&createExe.content, flagContent, "", "the content of the status to create") createExe.StringVar(&createExe.contentType, "content-type", "text/plain", "the type that the contents should be parsed from (valid values are plain and markdown)") createExe.StringVar(&createExe.language, "language", "", "the ISO 639 language code for this status") createExe.StringVar(&createExe.spoilerText, "spoiler-text", "", "the text to display as the status' warning or subject") createExe.StringVar(&createExe.visibility, "visibility", "", "the visibility of the posted status") 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, resourceStatus: c.createStatus, } 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 } func (c *CreateExecutor) createStatus(gtsClient *client.Client) error { if c.content == "" { return FlagNotSetError{flagText: flagContent} } if c.language == "" { return FlagNotSetError{flagText: "language"} } if c.visibility == "" { return FlagNotSetError{flagText: "visibility"} } visibility := model.ParseStatusVisibility(c.visibility) if visibility == model.StatusVisibilityUnknown { return fmt.Errorf("invalid status visibility %q", c.visibility) } status, err := gtsClient.CreateStatus( c.content, c.contentType, c.language, c.spoilerText, c.boostable, c.federated, c.likeable, c.replyable, c.sensitive, visibility, ) if err != nil { return fmt.Errorf("unable to create the status; %w", err) } fmt.Println("Successfully created the following status:") fmt.Println(status) return nil }