From e9e53f70cba8b292b314ad7eaedc98b0e4222a98 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sun, 11 Aug 2024 17:55:43 +0100 Subject: [PATCH] checkpoint: add add to the schema --- cmd/enbas/main.go | 2 -- internal/executor/add.go | 58 ++++++---------------------------- internal/executor/executors.go | 41 ++++++++++++++++++++++++ internal/flag/intslice.go | 8 +++++ schema/enbas_cli_schema.json | 48 ++++++++++++++++++++-------- 5 files changed, 93 insertions(+), 64 deletions(-) diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index 605f4a4..7e06c0d 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -86,8 +86,6 @@ func run() error { executor.CommandAdd: executor.NewAddExecutor( enbasPrinter, enbasConfig, - executor.CommandAdd, - executor.CommandSummaryLookup(executor.CommandAdd), ), executor.CommandBlock: executor.NewBlockExecutor( enbasPrinter, diff --git a/internal/executor/add.go b/internal/executor/add.go index b09f7b6..fd5e5e7 100644 --- a/internal/executor/add.go +++ b/internal/executor/add.go @@ -2,53 +2,11 @@ package executor import ( "errors" - "flag" "fmt" "codeflow.dananglin.me.uk/apollo/enbas/internal/client" - "codeflow.dananglin.me.uk/apollo/enbas/internal/config" - internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag" - "codeflow.dananglin.me.uk/apollo/enbas/internal/printer" ) -type AddExecutor struct { - *flag.FlagSet - - printer *printer.Printer - config *config.Config - resourceType string - toResourceType string - listID string - statusID string - pollID string - choices internalFlag.IntSliceValue - accountNames internalFlag.StringSliceValue - content string -} - -func NewAddExecutor(printer *printer.Printer, config *config.Config, name, summary string) *AddExecutor { - addExe := AddExecutor{ - FlagSet: flag.NewFlagSet(name, flag.ExitOnError), - - printer: printer, - config: config, - accountNames: internalFlag.NewStringSliceValue(), - } - - addExe.StringVar(&addExe.resourceType, flagType, "", "Specify the resource type to add (e.g. account, note)") - addExe.StringVar(&addExe.toResourceType, flagTo, "", "Specify the target resource type to add to (e.g. list, account, etc)") - addExe.StringVar(&addExe.listID, flagListID, "", "The ID of the list") - addExe.StringVar(&addExe.statusID, flagStatusID, "", "The ID of the status") - addExe.StringVar(&addExe.content, flagContent, "", "The content of the resource") - addExe.StringVar(&addExe.pollID, flagPollID, "", "The ID of the poll") - addExe.Var(&addExe.accountNames, flagAccountName, "The name of the account") - addExe.Var(&addExe.choices, flagVote, "Add a vote to an option in a poll") - - addExe.Usage = commandUsageFunc(name, summary, addExe.FlagSet) - - return &addExe -} - func (a *AddExecutor) Execute() error { if a.toResourceType == "" { return FlagNotSetError{flagText: flagTo} @@ -96,7 +54,7 @@ func (a *AddExecutor) addAccountsToList(gtsClient *client.Client) error { return FlagNotSetError{flagText: flagListID} } - if len(a.accountNames) == 0 { + if a.accountNames.Empty() { return NoAccountSpecifiedError{} } @@ -146,8 +104,12 @@ func (a *AddExecutor) addToAccount(gtsClient *client.Client) error { } func (a *AddExecutor) addNoteToAccount(gtsClient *client.Client) error { - if len(a.accountNames) != 1 { - return fmt.Errorf("unexpected number of accounts specified: want 1, got %d", len(a.accountNames)) + expectedNumAccountNames := 1 + if !a.accountNames.ExpectedLength(expectedNumAccountNames) { + return fmt.Errorf( + "found an unexpected number of --account-name flags: expected %d", + expectedNumAccountNames, + ) } accountID, err := getAccountID(gtsClient, false, a.accountNames[0], a.config.CredentialsFile) @@ -264,7 +226,7 @@ func (a *AddExecutor) addToPoll(gtsClient *client.Client) error { } func (a *AddExecutor) addVoteToPoll(gtsClient *client.Client) error { - if len(a.choices) == 0 { + if a.votes.Empty() { return errors.New("please use --" + flagVote + " to make a choice in this poll") } @@ -277,11 +239,11 @@ func (a *AddExecutor) addVoteToPoll(gtsClient *client.Client) error { return PollClosedError{} } - if !poll.Multiple && len(a.choices) > 1 { + if !poll.Multiple && !a.votes.ExpectedLength(1) { return MultipleChoiceError{} } - if err := gtsClient.VoteInPoll(a.pollID, []int(a.choices)); err != nil { + if err := gtsClient.VoteInPoll(a.pollID, []int(a.votes)); err != nil { return fmt.Errorf("unable to add your vote(s) to the poll: %w", err) } diff --git a/internal/executor/executors.go b/internal/executor/executors.go index a68a99e..db2109e 100644 --- a/internal/executor/executors.go +++ b/internal/executor/executors.go @@ -41,6 +41,47 @@ func NewAcceptExecutor( return &exe } +// AddExecutor is the executor for the add command. +type AddExecutor struct { + *flag.FlagSet + printer *printer.Printer + config *config.Config + accountNames internalFlag.StringSliceValue + content string + listID string + pollID string + statusID string + toResourceType string + resourceType string + votes internalFlag.IntSliceValue +} + +func NewAddExecutor( + printer *printer.Printer, + config *config.Config, +) *AddExecutor { + exe := AddExecutor{ + FlagSet: flag.NewFlagSet("add", flag.ExitOnError), + printer: printer, + config: config, + accountNames: internalFlag.NewStringSliceValue(), + votes: internalFlag.NewIntSliceValue(), + } + + exe.Usage = commandUsageFunc("add", "Add a resource to another resource", exe.FlagSet) + + exe.Var(&exe.accountNames, "account-name", "The name of the account") + exe.StringVar(&exe.content, "content", "", "The content of the created resource") + exe.StringVar(&exe.listID, "list-id", "", "The ID of the list in question") + exe.StringVar(&exe.pollID, "poll-id", "", "The ID of the poll") + exe.StringVar(&exe.statusID, "status-id", "", "The ID of the status") + exe.StringVar(&exe.toResourceType, "to", "", "TBC") + exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + exe.Var(&exe.votes, "vote", "Add a vote to an option in a poll") + + return &exe +} + // BlockExecutor is the executor for the block command. type BlockExecutor struct { *flag.FlagSet diff --git a/internal/flag/intslice.go b/internal/flag/intslice.go index a37d9c4..ed21fde 100644 --- a/internal/flag/intslice.go +++ b/internal/flag/intslice.go @@ -39,3 +39,11 @@ func (v *IntSliceValue) Set(text string) error { return nil } + +func (v IntSliceValue) Empty() bool { + return len(v) == 0 +} + +func (v IntSliceValue) ExpectedLength(expectedLength int) bool { + return len(v) == expectedLength +} diff --git a/schema/enbas_cli_schema.json b/schema/enbas_cli_schema.json index c07e30c..7ff4961 100644 --- a/schema/enbas_cli_schema.json +++ b/schema/enbas_cli_schema.json @@ -180,17 +180,21 @@ "type": "string", "description": "The timeline category" }, - "type": { - "type": "string", - "description": "The type of resource you want to action on (e.g. account, status)" - }, "to": { "type": "string", "description": "TBC" }, + "type": { + "type": "string", + "description": "The type of resource you want to action on (e.g. account, status)" + }, "visibility": { "type": "string", "description": "The visibility of the posted status" + }, + "vote": { + "type": "IntSliceValue", + "description": "Add a vote to an option in a poll" } }, @@ -198,17 +202,33 @@ "accept": { "additionalFields": [], "flags": [ - { "flag": "account-name", "default": "" }, + { "flag": "account-name" }, { "flag": "type", "fieldName": "resourceType", "default": "" } ], "summary": "Accepts a request (e.g. a follow request)", "useConfig": true, "usePrinter": true }, + "add": { + "additionalFields": [], + "flags": [ + { "flag": "account-name", "fieldName": "accountNames" }, + { "flag": "content", "default": "" }, + { "flag": "list-id", "fieldName": "listID", "default": "" }, + { "flag": "poll-id", "fieldName": "pollID", "default": "" }, + { "flag": "status-id", "fieldName": "statusID", "default": "" }, + { "flag": "to", "fieldName": "toResourceType", "default": "" }, + { "flag": "type", "fieldName": "resourceType", "default": "" }, + { "flag": "vote", "fieldName": "votes" } + ], + "summary": "Add a resource to another resource", + "useConfig": true, + "usePrinter": true + }, "block": { "additionalFields": [], "flags": [ - { "flag": "account-name", "default": "" }, + { "flag": "account-name" }, { "flag": "type", "fieldName": "resourceType", "default": "" } ], "summary": "Blocks a resource (e.g. an account)", @@ -268,7 +288,7 @@ "follow": { "additionalFields": [], "flags": [ - { "flag": "account-name", "default": "" }, + { "flag": "account-name" }, { "flag": "notify", "default": "false" }, { "flag": "show-reposts", "default": "true" }, { "flag": "type", "fieldName": "resourceType", "default": "" } @@ -298,7 +318,7 @@ "mute": { "additionalFields": [], "flags": [ - { "flag": "account-name", "default": "" }, + { "flag": "account-name" }, { "flag": "mute-duration" }, { "flag": "mute-notifications", "default": "false" }, { "flag": "type", "fieldName": "resourceType", "default": "" } @@ -310,7 +330,7 @@ "reject": { "additionalFields": [], "flags": [ - { "flag": "account-name", "default": "" }, + { "flag": "account-name" }, { "flag": "type", "fieldName": "resourceType", "default": "" } ], "summary": "Rejects a request (e.g. a follow request)", @@ -320,7 +340,7 @@ "show": { "additionalFields": [], "flags": [ - { "flag": "account-name", "default": "" }, + { "flag": "account-name" }, { "flag": "all-images", "fieldName": "getAllImages", "default": "false" }, { "flag": "all-videos", "fieldName": "getAllVideos", "default": "false" }, { "flag": "attachment-id", "fieldName": "attachmentIDs" }, @@ -350,7 +370,7 @@ "switch": { "additionalFields": [], "flags": [ - { "flag": "account-name", "default": "" }, + { "flag": "account-name" }, { "flag": "to", "default": "" } ], "summary": "Performs a switch operation (e.g. switching between logged in accounts)", @@ -360,7 +380,7 @@ "unblock": { "additionalFields": [], "flags": [ - { "flag": "account-name", "default": "" }, + { "flag": "account-name" }, { "flag": "type", "fieldName": "resourceType", "default": "" } ], "summary": "Unblocks a resource (e.g. an account)", @@ -370,7 +390,7 @@ "unfollow": { "additionalFields": [], "flags": [ - { "flag": "account-name", "default": "" }, + { "flag": "account-name" }, { "flag": "type", "fieldName": "resourceType", "default": "" } ], "summary": "Unfollow a resource (e.g. an account)", @@ -380,7 +400,7 @@ "unmute": { "additionalFields": [], "flags": [ - { "flag": "account-name", "default": "" }, + { "flag": "account-name" }, { "flag": "type", "fieldName": "resourceType", "default": "" } ], "summary": "Umutes a specific resource (e.g. an account)",