checkpoint: add add to the schema

This commit is contained in:
Dan Anglin 2024-08-11 17:55:43 +01:00
parent c91561b314
commit e9e53f70cb
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
5 changed files with 93 additions and 64 deletions

View file

@ -86,8 +86,6 @@ func run() error {
executor.CommandAdd: executor.NewAddExecutor( executor.CommandAdd: executor.NewAddExecutor(
enbasPrinter, enbasPrinter,
enbasConfig, enbasConfig,
executor.CommandAdd,
executor.CommandSummaryLookup(executor.CommandAdd),
), ),
executor.CommandBlock: executor.NewBlockExecutor( executor.CommandBlock: executor.NewBlockExecutor(
enbasPrinter, enbasPrinter,

View file

@ -2,53 +2,11 @@ package executor
import ( import (
"errors" "errors"
"flag"
"fmt" "fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client" "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 { func (a *AddExecutor) Execute() error {
if a.toResourceType == "" { if a.toResourceType == "" {
return FlagNotSetError{flagText: flagTo} return FlagNotSetError{flagText: flagTo}
@ -96,7 +54,7 @@ func (a *AddExecutor) addAccountsToList(gtsClient *client.Client) error {
return FlagNotSetError{flagText: flagListID} return FlagNotSetError{flagText: flagListID}
} }
if len(a.accountNames) == 0 { if a.accountNames.Empty() {
return NoAccountSpecifiedError{} return NoAccountSpecifiedError{}
} }
@ -146,8 +104,12 @@ func (a *AddExecutor) addToAccount(gtsClient *client.Client) error {
} }
func (a *AddExecutor) addNoteToAccount(gtsClient *client.Client) error { func (a *AddExecutor) addNoteToAccount(gtsClient *client.Client) error {
if len(a.accountNames) != 1 { expectedNumAccountNames := 1
return fmt.Errorf("unexpected number of accounts specified: want 1, got %d", len(a.accountNames)) 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) 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 { 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") 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{} return PollClosedError{}
} }
if !poll.Multiple && len(a.choices) > 1 { if !poll.Multiple && !a.votes.ExpectedLength(1) {
return MultipleChoiceError{} 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) return fmt.Errorf("unable to add your vote(s) to the poll: %w", err)
} }

View file

@ -41,6 +41,47 @@ func NewAcceptExecutor(
return &exe 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. // BlockExecutor is the executor for the block command.
type BlockExecutor struct { type BlockExecutor struct {
*flag.FlagSet *flag.FlagSet

View file

@ -39,3 +39,11 @@ func (v *IntSliceValue) Set(text string) error {
return nil return nil
} }
func (v IntSliceValue) Empty() bool {
return len(v) == 0
}
func (v IntSliceValue) ExpectedLength(expectedLength int) bool {
return len(v) == expectedLength
}

View file

@ -180,17 +180,21 @@
"type": "string", "type": "string",
"description": "The timeline category" "description": "The timeline category"
}, },
"type": {
"type": "string",
"description": "The type of resource you want to action on (e.g. account, status)"
},
"to": { "to": {
"type": "string", "type": "string",
"description": "TBC" "description": "TBC"
}, },
"type": {
"type": "string",
"description": "The type of resource you want to action on (e.g. account, status)"
},
"visibility": { "visibility": {
"type": "string", "type": "string",
"description": "The visibility of the posted status" "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": { "accept": {
"additionalFields": [], "additionalFields": [],
"flags": [ "flags": [
{ "flag": "account-name", "default": "" }, { "flag": "account-name" },
{ "flag": "type", "fieldName": "resourceType", "default": "" } { "flag": "type", "fieldName": "resourceType", "default": "" }
], ],
"summary": "Accepts a request (e.g. a follow request)", "summary": "Accepts a request (e.g. a follow request)",
"useConfig": true, "useConfig": true,
"usePrinter": 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": { "block": {
"additionalFields": [], "additionalFields": [],
"flags": [ "flags": [
{ "flag": "account-name", "default": "" }, { "flag": "account-name" },
{ "flag": "type", "fieldName": "resourceType", "default": "" } { "flag": "type", "fieldName": "resourceType", "default": "" }
], ],
"summary": "Blocks a resource (e.g. an account)", "summary": "Blocks a resource (e.g. an account)",
@ -268,7 +288,7 @@
"follow": { "follow": {
"additionalFields": [], "additionalFields": [],
"flags": [ "flags": [
{ "flag": "account-name", "default": "" }, { "flag": "account-name" },
{ "flag": "notify", "default": "false" }, { "flag": "notify", "default": "false" },
{ "flag": "show-reposts", "default": "true" }, { "flag": "show-reposts", "default": "true" },
{ "flag": "type", "fieldName": "resourceType", "default": "" } { "flag": "type", "fieldName": "resourceType", "default": "" }
@ -298,7 +318,7 @@
"mute": { "mute": {
"additionalFields": [], "additionalFields": [],
"flags": [ "flags": [
{ "flag": "account-name", "default": "" }, { "flag": "account-name" },
{ "flag": "mute-duration" }, { "flag": "mute-duration" },
{ "flag": "mute-notifications", "default": "false" }, { "flag": "mute-notifications", "default": "false" },
{ "flag": "type", "fieldName": "resourceType", "default": "" } { "flag": "type", "fieldName": "resourceType", "default": "" }
@ -310,7 +330,7 @@
"reject": { "reject": {
"additionalFields": [], "additionalFields": [],
"flags": [ "flags": [
{ "flag": "account-name", "default": "" }, { "flag": "account-name" },
{ "flag": "type", "fieldName": "resourceType", "default": "" } { "flag": "type", "fieldName": "resourceType", "default": "" }
], ],
"summary": "Rejects a request (e.g. a follow request)", "summary": "Rejects a request (e.g. a follow request)",
@ -320,7 +340,7 @@
"show": { "show": {
"additionalFields": [], "additionalFields": [],
"flags": [ "flags": [
{ "flag": "account-name", "default": "" }, { "flag": "account-name" },
{ "flag": "all-images", "fieldName": "getAllImages", "default": "false" }, { "flag": "all-images", "fieldName": "getAllImages", "default": "false" },
{ "flag": "all-videos", "fieldName": "getAllVideos", "default": "false" }, { "flag": "all-videos", "fieldName": "getAllVideos", "default": "false" },
{ "flag": "attachment-id", "fieldName": "attachmentIDs" }, { "flag": "attachment-id", "fieldName": "attachmentIDs" },
@ -350,7 +370,7 @@
"switch": { "switch": {
"additionalFields": [], "additionalFields": [],
"flags": [ "flags": [
{ "flag": "account-name", "default": "" }, { "flag": "account-name" },
{ "flag": "to", "default": "" } { "flag": "to", "default": "" }
], ],
"summary": "Performs a switch operation (e.g. switching between logged in accounts)", "summary": "Performs a switch operation (e.g. switching between logged in accounts)",
@ -360,7 +380,7 @@
"unblock": { "unblock": {
"additionalFields": [], "additionalFields": [],
"flags": [ "flags": [
{ "flag": "account-name", "default": "" }, { "flag": "account-name" },
{ "flag": "type", "fieldName": "resourceType", "default": "" } { "flag": "type", "fieldName": "resourceType", "default": "" }
], ],
"summary": "Unblocks a resource (e.g. an account)", "summary": "Unblocks a resource (e.g. an account)",
@ -370,7 +390,7 @@
"unfollow": { "unfollow": {
"additionalFields": [], "additionalFields": [],
"flags": [ "flags": [
{ "flag": "account-name", "default": "" }, { "flag": "account-name" },
{ "flag": "type", "fieldName": "resourceType", "default": "" } { "flag": "type", "fieldName": "resourceType", "default": "" }
], ],
"summary": "Unfollow a resource (e.g. an account)", "summary": "Unfollow a resource (e.g. an account)",
@ -380,7 +400,7 @@
"unmute": { "unmute": {
"additionalFields": [], "additionalFields": [],
"flags": [ "flags": [
{ "flag": "account-name", "default": "" }, { "flag": "account-name" },
{ "flag": "type", "fieldName": "resourceType", "default": "" } { "flag": "type", "fieldName": "resourceType", "default": "" }
], ],
"summary": "Umutes a specific resource (e.g. an account)", "summary": "Umutes a specific resource (e.g. an account)",