From c91561b314216bd928e6f9b6bdc594ba0a298dd9 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sun, 11 Aug 2024 17:05:41 +0100 Subject: [PATCH] checkpoint: the account-name field is now of type StringSliceValue --- internal/executor/accept.go | 10 +++- internal/executor/block.go | 10 ++-- internal/executor/executors.go | 98 +++++++++++++++++++--------------- internal/executor/follow.go | 6 ++- internal/executor/mute.go | 10 ++-- internal/executor/reject.go | 10 +++- internal/executor/show.go | 31 ++++++++--- internal/executor/switch.go | 12 +++-- internal/executor/unblock.go | 10 ++-- internal/executor/unfollow.go | 10 +++- internal/executor/unmute.go | 10 ++-- internal/flag/stringslice.go | 8 +++ schema/enbas_cli_schema.json | 2 +- 13 files changed, 156 insertions(+), 71 deletions(-) diff --git a/internal/executor/accept.go b/internal/executor/accept.go index 158927d..9cbd8c2 100644 --- a/internal/executor/accept.go +++ b/internal/executor/accept.go @@ -25,7 +25,15 @@ func (a *AcceptExecutor) Execute() error { } func (a *AcceptExecutor) acceptFollowRequest(gtsClient *client.Client) error { - accountID, err := getAccountID(gtsClient, false, a.accountName, a.config.CredentialsFile) + expectedNumAccountNames := 1 + if !a.accountName.ExpectedLength(expectedNumAccountNames) { + return fmt.Errorf( + "found an unexpected number of --account-name flags: expected %d", + expectedNumAccountNames, + ) + } + + accountID, err := getAccountID(gtsClient, false, a.accountName[0], a.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/block.go b/internal/executor/block.go index 0bf71e4..ea7729e 100644 --- a/internal/executor/block.go +++ b/internal/executor/block.go @@ -25,11 +25,15 @@ func (b *BlockExecutor) Execute() error { } func (b *BlockExecutor) blockAccount(gtsClient *client.Client) error { - if b.accountName == "" { - return FlagNotSetError{flagText: flagAccountName} + expectedNumAccountNames := 1 + if !b.accountName.ExpectedLength(expectedNumAccountNames) { + return fmt.Errorf( + "found an unexpected number of --account-name flags: expected %d", + expectedNumAccountNames, + ) } - accountID, err := getAccountID(gtsClient, false, b.accountName, b.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, b.accountName[0], b.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/executors.go b/internal/executor/executors.go index 9586e51..a68a99e 100644 --- a/internal/executor/executors.go +++ b/internal/executor/executors.go @@ -18,7 +18,7 @@ type AcceptExecutor struct { *flag.FlagSet printer *printer.Printer config *config.Config - accountName string + accountName internalFlag.StringSliceValue resourceType string } @@ -27,14 +27,15 @@ func NewAcceptExecutor( config *config.Config, ) *AcceptExecutor { exe := AcceptExecutor{ - FlagSet: flag.NewFlagSet("accept", flag.ExitOnError), - printer: printer, - config: config, + FlagSet: flag.NewFlagSet("accept", flag.ExitOnError), + printer: printer, + config: config, + accountName: internalFlag.NewStringSliceValue(), } exe.Usage = commandUsageFunc("accept", "Accepts a request (e.g. a follow request)", exe.FlagSet) - exe.StringVar(&exe.accountName, "account-name", "", "The name of the account") + exe.Var(&exe.accountName, "account-name", "The name of the account") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") return &exe @@ -45,7 +46,7 @@ type BlockExecutor struct { *flag.FlagSet printer *printer.Printer config *config.Config - accountName string + accountName internalFlag.StringSliceValue resourceType string } @@ -54,14 +55,15 @@ func NewBlockExecutor( config *config.Config, ) *BlockExecutor { exe := BlockExecutor{ - FlagSet: flag.NewFlagSet("block", flag.ExitOnError), - printer: printer, - config: config, + FlagSet: flag.NewFlagSet("block", flag.ExitOnError), + printer: printer, + config: config, + accountName: internalFlag.NewStringSliceValue(), } exe.Usage = commandUsageFunc("block", "Blocks a resource (e.g. an account)", exe.FlagSet) - exe.StringVar(&exe.accountName, "account-name", "", "The name of the account") + exe.Var(&exe.accountName, "account-name", "The name of the account") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") return &exe @@ -196,7 +198,7 @@ type FollowExecutor struct { *flag.FlagSet printer *printer.Printer config *config.Config - accountName string + accountName internalFlag.StringSliceValue notify bool showReposts bool resourceType string @@ -207,14 +209,15 @@ func NewFollowExecutor( config *config.Config, ) *FollowExecutor { exe := FollowExecutor{ - FlagSet: flag.NewFlagSet("follow", flag.ExitOnError), - printer: printer, - config: config, + FlagSet: flag.NewFlagSet("follow", flag.ExitOnError), + printer: printer, + config: config, + accountName: internalFlag.NewStringSliceValue(), } exe.Usage = commandUsageFunc("follow", "Follow a resource (e.g. an account)", exe.FlagSet) - exe.StringVar(&exe.accountName, "account-name", "", "The name of the account") + exe.Var(&exe.accountName, "account-name", "The name of the account") exe.BoolVar(&exe.notify, "notify", false, "Get notifications from statuses from the account you want to follow") exe.BoolVar(&exe.showReposts, "show-reposts", true, "Show reposts from the account you want to follow") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") @@ -274,7 +277,7 @@ type MuteExecutor struct { *flag.FlagSet printer *printer.Printer config *config.Config - accountName string + accountName internalFlag.StringSliceValue muteDuration internalFlag.TimeDurationValue muteNotifications bool resourceType string @@ -288,12 +291,13 @@ func NewMuteExecutor( FlagSet: flag.NewFlagSet("mute", flag.ExitOnError), printer: printer, config: config, + accountName: internalFlag.NewStringSliceValue(), muteDuration: internalFlag.NewTimeDurationValue(), } exe.Usage = commandUsageFunc("mute", "Mutes a specific resource (e.g. an account)", exe.FlagSet) - exe.StringVar(&exe.accountName, "account-name", "", "The name of the account") + exe.Var(&exe.accountName, "account-name", "The name of the account") exe.Var(&exe.muteDuration, "mute-duration", "Specify how long the mute should last for. To mute indefinitely, set this to 0s") exe.BoolVar(&exe.muteNotifications, "mute-notifications", false, "Set to true to mute notifications as well as posts") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") @@ -306,7 +310,7 @@ type RejectExecutor struct { *flag.FlagSet printer *printer.Printer config *config.Config - accountName string + accountName internalFlag.StringSliceValue resourceType string } @@ -315,14 +319,15 @@ func NewRejectExecutor( config *config.Config, ) *RejectExecutor { exe := RejectExecutor{ - FlagSet: flag.NewFlagSet("reject", flag.ExitOnError), - printer: printer, - config: config, + FlagSet: flag.NewFlagSet("reject", flag.ExitOnError), + printer: printer, + config: config, + accountName: internalFlag.NewStringSliceValue(), } exe.Usage = commandUsageFunc("reject", "Rejects a request (e.g. a follow request)", exe.FlagSet) - exe.StringVar(&exe.accountName, "account-name", "", "The name of the account") + exe.Var(&exe.accountName, "account-name", "The name of the account") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") return &exe @@ -333,7 +338,7 @@ type ShowExecutor struct { *flag.FlagSet printer *printer.Printer config *config.Config - accountName string + accountName internalFlag.StringSliceValue getAllImages bool getAllVideos bool attachmentIDs internalFlag.StringSliceValue @@ -365,12 +370,13 @@ func NewShowExecutor( FlagSet: flag.NewFlagSet("show", flag.ExitOnError), printer: printer, config: config, + accountName: internalFlag.NewStringSliceValue(), attachmentIDs: internalFlag.NewStringSliceValue(), } exe.Usage = commandUsageFunc("show", "Shows details about a specified resource", exe.FlagSet) - exe.StringVar(&exe.accountName, "account-name", "", "The name of the account") + exe.Var(&exe.accountName, "account-name", "The name of the account") exe.BoolVar(&exe.getAllImages, "all-images", false, "Set to true to show all images from a status") exe.BoolVar(&exe.getAllVideos, "all-videos", false, "Set to true to show all videos from a status") exe.Var(&exe.attachmentIDs, "attachment-id", "The ID of the media attachment") @@ -401,7 +407,7 @@ type SwitchExecutor struct { *flag.FlagSet printer *printer.Printer config *config.Config - accountName string + accountName internalFlag.StringSliceValue to string } @@ -410,14 +416,15 @@ func NewSwitchExecutor( config *config.Config, ) *SwitchExecutor { exe := SwitchExecutor{ - FlagSet: flag.NewFlagSet("switch", flag.ExitOnError), - printer: printer, - config: config, + FlagSet: flag.NewFlagSet("switch", flag.ExitOnError), + printer: printer, + config: config, + accountName: internalFlag.NewStringSliceValue(), } exe.Usage = commandUsageFunc("switch", "Performs a switch operation (e.g. switching between logged in accounts)", exe.FlagSet) - exe.StringVar(&exe.accountName, "account-name", "", "The name of the account") + exe.Var(&exe.accountName, "account-name", "The name of the account") exe.StringVar(&exe.to, "to", "", "TBC") return &exe @@ -428,7 +435,7 @@ type UnblockExecutor struct { *flag.FlagSet printer *printer.Printer config *config.Config - accountName string + accountName internalFlag.StringSliceValue resourceType string } @@ -437,14 +444,15 @@ func NewUnblockExecutor( config *config.Config, ) *UnblockExecutor { exe := UnblockExecutor{ - FlagSet: flag.NewFlagSet("unblock", flag.ExitOnError), - printer: printer, - config: config, + FlagSet: flag.NewFlagSet("unblock", flag.ExitOnError), + printer: printer, + config: config, + accountName: internalFlag.NewStringSliceValue(), } exe.Usage = commandUsageFunc("unblock", "Unblocks a resource (e.g. an account)", exe.FlagSet) - exe.StringVar(&exe.accountName, "account-name", "", "The name of the account") + exe.Var(&exe.accountName, "account-name", "The name of the account") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") return &exe @@ -455,7 +463,7 @@ type UnfollowExecutor struct { *flag.FlagSet printer *printer.Printer config *config.Config - accountName string + accountName internalFlag.StringSliceValue resourceType string } @@ -464,14 +472,15 @@ func NewUnfollowExecutor( config *config.Config, ) *UnfollowExecutor { exe := UnfollowExecutor{ - FlagSet: flag.NewFlagSet("unfollow", flag.ExitOnError), - printer: printer, - config: config, + FlagSet: flag.NewFlagSet("unfollow", flag.ExitOnError), + printer: printer, + config: config, + accountName: internalFlag.NewStringSliceValue(), } exe.Usage = commandUsageFunc("unfollow", "Unfollow a resource (e.g. an account)", exe.FlagSet) - exe.StringVar(&exe.accountName, "account-name", "", "The name of the account") + exe.Var(&exe.accountName, "account-name", "The name of the account") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") return &exe @@ -482,7 +491,7 @@ type UnmuteExecutor struct { *flag.FlagSet printer *printer.Printer config *config.Config - accountName string + accountName internalFlag.StringSliceValue resourceType string } @@ -491,14 +500,15 @@ func NewUnmuteExecutor( config *config.Config, ) *UnmuteExecutor { exe := UnmuteExecutor{ - FlagSet: flag.NewFlagSet("unmute", flag.ExitOnError), - printer: printer, - config: config, + FlagSet: flag.NewFlagSet("unmute", flag.ExitOnError), + printer: printer, + config: config, + accountName: internalFlag.NewStringSliceValue(), } exe.Usage = commandUsageFunc("unmute", "Umutes a specific resource (e.g. an account)", exe.FlagSet) - exe.StringVar(&exe.accountName, "account-name", "", "The name of the account") + exe.Var(&exe.accountName, "account-name", "The name of the account") exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") return &exe diff --git a/internal/executor/follow.go b/internal/executor/follow.go index b76ca09..9834d40 100644 --- a/internal/executor/follow.go +++ b/internal/executor/follow.go @@ -25,7 +25,11 @@ func (f *FollowExecutor) Execute() error { } func (f *FollowExecutor) followAccount(gtsClient *client.Client) error { - accountID, err := getAccountID(gtsClient, false, f.accountName, f.config.CredentialsFile) + if !f.accountName.ExpectedLength(1) { + return fmt.Errorf("found an unexpected number of %s flags: expected %d", "--account-name", 1) + } + + accountID, err := getAccountID(gtsClient, false, f.accountName[0], f.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/mute.go b/internal/executor/mute.go index 468a01c..fec24e3 100644 --- a/internal/executor/mute.go +++ b/internal/executor/mute.go @@ -25,11 +25,15 @@ func (m *MuteExecutor) Execute() error { } func (m *MuteExecutor) muteAccount(gtsClient *client.Client) error { - if m.accountName == "" { - return FlagNotSetError{flagText: flagAccountName} + expectedNumAccountNames := 1 + if !m.accountName.ExpectedLength(expectedNumAccountNames) { + return fmt.Errorf( + "found an unexpected number of --account-name flags: expected %d", + expectedNumAccountNames, + ) } - accountID, err := getAccountID(gtsClient, false, m.accountName, m.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, m.accountName[0], m.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/reject.go b/internal/executor/reject.go index adaf804..e4114cf 100644 --- a/internal/executor/reject.go +++ b/internal/executor/reject.go @@ -25,7 +25,15 @@ func (r *RejectExecutor) Execute() error { } func (r *RejectExecutor) rejectFollowRequest(gtsClient *client.Client) error { - accountID, err := getAccountID(gtsClient, false, r.accountName, r.config.CredentialsFile) + expectedNumAccountNames := 1 + if !r.accountName.ExpectedLength(expectedNumAccountNames) { + return fmt.Errorf( + "found an unexpected number of --account-name flags: expected %d", + expectedNumAccountNames, + ) + } + + accountID, err := getAccountID(gtsClient, false, r.accountName[0], r.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/show.go b/internal/executor/show.go index 0894506..2780bbc 100644 --- a/internal/executor/show.go +++ b/internal/executor/show.go @@ -70,11 +70,14 @@ func (s *ShowExecutor) showAccount(gtsClient *client.Client) error { return fmt.Errorf("received an error while getting the account details: %w", err) } } else { - if s.accountName == "" { - return FlagNotSetError{flagText: flagAccountName} + expectedNumAccountNames := 1 + if !s.accountName.ExpectedLength(expectedNumAccountNames) { + return fmt.Errorf( + "found an unexpected number of --account-name flags: expected %d", + expectedNumAccountNames, + ) } - - account, err = getAccount(gtsClient, s.accountName) + account, err = getAccount(gtsClient, s.accountName[0]) if err != nil { return fmt.Errorf("received an error while getting the account details: %w", err) } @@ -269,7 +272,15 @@ func (s *ShowExecutor) showFollowers(gtsClient *client.Client) error { } func (s *ShowExecutor) showFollowersFromAccount(gtsClient *client.Client) error { - accountID, err := getAccountID(gtsClient, s.myAccount, s.accountName, s.config.CredentialsFile) + expectedNumAccountNames := 1 + if !s.accountName.ExpectedLength(expectedNumAccountNames) { + return fmt.Errorf( + "found an unexpected number of --account-name flags: expected %d", + expectedNumAccountNames, + ) + } + + accountID, err := getAccountID(gtsClient, s.myAccount, s.accountName[0], s.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } @@ -309,7 +320,15 @@ func (s *ShowExecutor) showFollowing(gtsClient *client.Client) error { } func (s *ShowExecutor) showFollowingFromAccount(gtsClient *client.Client) error { - accountID, err := getAccountID(gtsClient, s.myAccount, s.accountName, s.config.CredentialsFile) + expectedNumAccountNames := 1 + if !s.accountName.ExpectedLength(expectedNumAccountNames) { + return fmt.Errorf( + "found an unexpected number of --account-name flags: expected %d", + expectedNumAccountNames, + ) + } + + accountID, err := getAccountID(gtsClient, s.myAccount, s.accountName[0], s.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/switch.go b/internal/executor/switch.go index 032aea0..b5bc2af 100644 --- a/internal/executor/switch.go +++ b/internal/executor/switch.go @@ -20,15 +20,19 @@ func (s *SwitchExecutor) Execute() error { } func (s *SwitchExecutor) switchToAccount() error { - if s.accountName == "" { - return NoAccountSpecifiedError{} + expectedNumAccountNames := 1 + if !s.accountName.ExpectedLength(expectedNumAccountNames) { + return fmt.Errorf( + "found an unexpected number of --account-name flags: expected %d", + expectedNumAccountNames, + ) } - if err := config.UpdateCurrentAccount(s.accountName, s.config.CredentialsFile); err != nil { + if err := config.UpdateCurrentAccount(s.accountName[0], s.config.CredentialsFile); err != nil { return fmt.Errorf("unable to switch account to the account: %w", err) } - s.printer.PrintSuccess("The current account is now set to '" + s.accountName + "'.") + s.printer.PrintSuccess("The current account is now set to '" + s.accountName[0] + "'.") return nil } diff --git a/internal/executor/unblock.go b/internal/executor/unblock.go index 53abc46..4559e7c 100644 --- a/internal/executor/unblock.go +++ b/internal/executor/unblock.go @@ -25,11 +25,15 @@ func (b *UnblockExecutor) Execute() error { } func (b *UnblockExecutor) unblockAccount(gtsClient *client.Client) error { - if b.accountName == "" { - return FlagNotSetError{flagText: flagAccountName} + expectedNumAccountNames := 1 + if !b.accountName.ExpectedLength(expectedNumAccountNames) { + return fmt.Errorf( + "found an unexpected number of --account-name flags: expected %d", + expectedNumAccountNames, + ) } - accountID, err := getAccountID(gtsClient, false, b.accountName, b.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, b.accountName[0], b.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/unfollow.go b/internal/executor/unfollow.go index 102ce3d..308dcd6 100644 --- a/internal/executor/unfollow.go +++ b/internal/executor/unfollow.go @@ -25,7 +25,15 @@ func (f *UnfollowExecutor) Execute() error { } func (f *UnfollowExecutor) unfollowAccount(gtsClient *client.Client) error { - accountID, err := getAccountID(gtsClient, false, f.accountName, f.config.CredentialsFile) + expectedNumAccountNames := 1 + if !f.accountName.ExpectedLength(expectedNumAccountNames) { + return fmt.Errorf( + "found an unexpected number of --account-name flags: expected %d", + expectedNumAccountNames, + ) + } + + accountID, err := getAccountID(gtsClient, false, f.accountName[0], f.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/executor/unmute.go b/internal/executor/unmute.go index b05435b..59f3765 100644 --- a/internal/executor/unmute.go +++ b/internal/executor/unmute.go @@ -25,11 +25,15 @@ func (m *UnmuteExecutor) Execute() error { } func (m *UnmuteExecutor) unmuteAccount(gtsClient *client.Client) error { - if m.accountName == "" { - return FlagNotSetError{flagText: flagAccountName} + expectedNumAccountNames := 1 + if !m.accountName.ExpectedLength(expectedNumAccountNames) { + return fmt.Errorf( + "found an unexpected number of --account-name flags: expected %d", + expectedNumAccountNames, + ) } - accountID, err := getAccountID(gtsClient, false, m.accountName, m.config.CredentialsFile) + accountID, err := getAccountID(gtsClient, false, m.accountName[0], m.config.CredentialsFile) if err != nil { return fmt.Errorf("received an error while getting the account ID: %w", err) } diff --git a/internal/flag/stringslice.go b/internal/flag/stringslice.go index 0dbeb3a..7189c2b 100644 --- a/internal/flag/stringslice.go +++ b/internal/flag/stringslice.go @@ -21,3 +21,11 @@ func (v *StringSliceValue) Set(value string) error { return nil } + +func (v StringSliceValue) Empty() bool { + return len(v) == 0 +} + +func (v StringSliceValue) ExpectedLength(expectedLength int) bool { + return len(v) == expectedLength +} diff --git a/schema/enbas_cli_schema.json b/schema/enbas_cli_schema.json index 1865fca..c07e30c 100644 --- a/schema/enbas_cli_schema.json +++ b/schema/enbas_cli_schema.json @@ -1,7 +1,7 @@ { "flags": { "account-name": { - "type": "string", + "type": "StringSliceValue", "description": "The name of the account" }, "all-images": {