diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index 8d44176..187488d 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -99,11 +99,9 @@ func run() error { executor.CommandAdd, executor.CommandSummaryLookup(executor.CommandAdd), ), - executor.CommandBlock: executor.NewBlockOrUnblockExecutor( + executor.CommandBlock: executor.NewBlockExecutor( enbasPrinter, enbasConfig, - executor.CommandBlock, - executor.CommandSummaryLookup(executor.CommandBlock), ), executor.CommandCreate: executor.NewCreateExecutor( enbasPrinter, @@ -165,11 +163,9 @@ func run() error { executor.CommandUnmute, executor.CommandSummaryLookup(executor.CommandUnmute), ), - executor.CommandUnblock: executor.NewBlockOrUnblockExecutor( + executor.CommandUnblock: executor.NewUnblockExecutor( enbasPrinter, enbasConfig, - executor.CommandUnblock, - executor.CommandSummaryLookup(executor.CommandUnblock), ), executor.CommandShow: executor.NewShowExecutor( enbasPrinter, diff --git a/internal/executor/block.go b/internal/executor/block.go new file mode 100644 index 0000000..0bf71e4 --- /dev/null +++ b/internal/executor/block.go @@ -0,0 +1,44 @@ +package executor + +import ( + "fmt" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/client" +) + +func (b *BlockExecutor) Execute() error { + funcMap := map[string]func(*client.Client) error{ + resourceAccount: b.blockAccount, + } + + doFunc, ok := funcMap[b.resourceType] + if !ok { + return UnsupportedTypeError{resourceType: b.resourceType} + } + + gtsClient, err := client.NewClientFromFile(b.config.CredentialsFile) + if err != nil { + return fmt.Errorf("unable to create the GoToSocial client: %w", err) + } + + return doFunc(gtsClient) +} + +func (b *BlockExecutor) blockAccount(gtsClient *client.Client) error { + if b.accountName == "" { + return FlagNotSetError{flagText: flagAccountName} + } + + accountID, err := getAccountID(gtsClient, false, b.accountName, b.config.CredentialsFile) + if err != nil { + return fmt.Errorf("received an error while getting the account ID: %w", err) + } + + if err := gtsClient.BlockAccount(accountID); err != nil { + return fmt.Errorf("unable to block the account: %w", err) + } + + b.printer.PrintSuccess("Successfully blocked the account.") + + return nil +} diff --git a/internal/executor/block_or_unblock.go b/internal/executor/block_or_unblock.go deleted file mode 100644 index 3a38a62..0000000 --- a/internal/executor/block_or_unblock.go +++ /dev/null @@ -1,95 +0,0 @@ -package executor - -import ( - "flag" - "fmt" - - "codeflow.dananglin.me.uk/apollo/enbas/internal/client" - "codeflow.dananglin.me.uk/apollo/enbas/internal/config" - "codeflow.dananglin.me.uk/apollo/enbas/internal/printer" -) - -type BlockOrUnblockExecutor struct { - *flag.FlagSet - - printer *printer.Printer - config *config.Config - resourceType string - accountName string - command string -} - -func NewBlockOrUnblockExecutor(printer *printer.Printer, config *config.Config, name, summary string) *BlockOrUnblockExecutor { - blockExe := BlockOrUnblockExecutor{ - FlagSet: flag.NewFlagSet(name, flag.ExitOnError), - - printer: printer, - config: config, - command: name, - } - - blockExe.StringVar(&blockExe.resourceType, flagType, "", "Specify the type of resource to block or unblock") - blockExe.StringVar(&blockExe.accountName, flagAccountName, "", "Specify the account name in full (username@domain)") - - blockExe.Usage = commandUsageFunc(name, summary, blockExe.FlagSet) - - return &blockExe -} - -func (b *BlockOrUnblockExecutor) Execute() error { - funcMap := map[string]func(*client.Client) error{ - resourceAccount: b.blockOrUnblockAccount, - } - - doFunc, ok := funcMap[b.resourceType] - if !ok { - return UnsupportedTypeError{resourceType: b.resourceType} - } - - gtsClient, err := client.NewClientFromFile(b.config.CredentialsFile) - if err != nil { - return fmt.Errorf("unable to create the GoToSocial client: %w", err) - } - - return doFunc(gtsClient) -} - -func (b *BlockOrUnblockExecutor) blockOrUnblockAccount(gtsClient *client.Client) error { - if b.accountName == "" { - return FlagNotSetError{flagText: flagAccountName} - } - - accountID, err := getAccountID(gtsClient, false, b.accountName, b.config.CredentialsFile) - if err != nil { - return fmt.Errorf("received an error while getting the account ID: %w", err) - } - - switch b.command { - case CommandBlock: - return b.blockAccount(gtsClient, accountID) - case CommandUnblock: - return b.unblockAccount(gtsClient, accountID) - default: - return nil - } -} - -func (b *BlockOrUnblockExecutor) blockAccount(gtsClient *client.Client, accountID string) error { - if err := gtsClient.BlockAccount(accountID); err != nil { - return fmt.Errorf("unable to block the account: %w", err) - } - - b.printer.PrintSuccess("Successfully blocked the account.") - - return nil -} - -func (b *BlockOrUnblockExecutor) unblockAccount(gtsClient *client.Client, accountID string) error { - if err := gtsClient.UnblockAccount(accountID); err != nil { - return fmt.Errorf("unable to unblock the account: %w", err) - } - - b.printer.PrintSuccess("Successfully unblocked the account.") - - return nil -} diff --git a/internal/executor/executors.go b/internal/executor/executors.go index 0f20ba0..b4f6b72 100644 --- a/internal/executor/executors.go +++ b/internal/executor/executors.go @@ -38,6 +38,32 @@ func NewAcceptExecutor( return &exe } +// BlockExecutor is the executor for the block command. +type BlockExecutor struct { + *flag.FlagSet + printer *printer.Printer + config *config.Config + accountName string + resourceType string +} + +func NewBlockExecutor( + printer *printer.Printer, + config *config.Config, +) *BlockExecutor { + exe := BlockExecutor{ + FlagSet: flag.NewFlagSet("block", flag.ExitOnError), + printer: printer, + config: config, + } + + 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.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + return &exe +} + // FollowExecutor is the executor for the follow command. type FollowExecutor struct { *flag.FlagSet @@ -166,6 +192,32 @@ func NewSwitchExecutor( return &exe } +// UnblockExecutor is the executor for the unblock command. +type UnblockExecutor struct { + *flag.FlagSet + printer *printer.Printer + config *config.Config + accountName string + resourceType string +} + +func NewUnblockExecutor( + printer *printer.Printer, + config *config.Config, +) *UnblockExecutor { + exe := UnblockExecutor{ + FlagSet: flag.NewFlagSet("unblock", flag.ExitOnError), + printer: printer, + config: config, + } + + 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.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)") + return &exe +} + // UnfollowExecutor is the executor for the unfollow command. type UnfollowExecutor struct { *flag.FlagSet diff --git a/internal/executor/unblock.go b/internal/executor/unblock.go new file mode 100644 index 0000000..53abc46 --- /dev/null +++ b/internal/executor/unblock.go @@ -0,0 +1,44 @@ +package executor + +import ( + "fmt" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/client" +) + +func (b *UnblockExecutor) Execute() error { + funcMap := map[string]func(*client.Client) error{ + resourceAccount: b.unblockAccount, + } + + doFunc, ok := funcMap[b.resourceType] + if !ok { + return UnsupportedTypeError{resourceType: b.resourceType} + } + + gtsClient, err := client.NewClientFromFile(b.config.CredentialsFile) + if err != nil { + return fmt.Errorf("unable to create the GoToSocial client: %w", err) + } + + return doFunc(gtsClient) +} + +func (b *UnblockExecutor) unblockAccount(gtsClient *client.Client) error { + if b.accountName == "" { + return FlagNotSetError{flagText: flagAccountName} + } + + accountID, err := getAccountID(gtsClient, false, b.accountName, b.config.CredentialsFile) + if err != nil { + return fmt.Errorf("received an error while getting the account ID: %w", err) + } + + if err := gtsClient.UnblockAccount(accountID); err != nil { + return fmt.Errorf("unable to unblock the account: %w", err) + } + + b.printer.PrintSuccess("Successfully unblocked the account.") + + return nil +} diff --git a/schema/enbas_cli_schema.json b/schema/enbas_cli_schema.json index 6401def..6a52f30 100644 --- a/schema/enbas_cli_schema.json +++ b/schema/enbas_cli_schema.json @@ -41,6 +41,16 @@ "useConfig": true, "usePrinter": true }, + "block": { + "additionalFields": [], + "flags": [ + { "flag": "account-name", "default": "" }, + { "flag": "type", "fieldName": "resourceType", "default": "" } + ], + "summary": "Blocks a resource (e.g. an account)", + "useConfig": true, + "usePrinter": true + }, "follow": { "additionalFields": [], "flags": [ @@ -91,6 +101,16 @@ "useConfig": true, "usePrinter": true }, + "unblock": { + "additionalFields": [], + "flags": [ + { "flag": "account-name", "default": "" }, + { "flag": "type", "fieldName": "resourceType", "default": "" } + ], + "summary": "Unblocks a resource (e.g. an account)", + "useConfig": true, + "usePrinter": true + }, "unfollow": { "additionalFields": [], "flags": [