checkpoint: added block and unblock to schema

This commit is contained in:
Dan Anglin 2024-08-10 10:27:45 +01:00
parent b06fdc2960
commit 93bc7a93f8
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
6 changed files with 162 additions and 101 deletions

View file

@ -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,

View file

@ -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
}

View file

@ -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
}

View file

@ -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

View file

@ -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
}

View file

@ -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": [