Dan Anglin
55b93c6586
Move all executors to the internal folder package. This PR also comes with additional breaking changes. Changes: - refactor: move all executors to the internal/executor package. - refactor: update naming patterns for constants, variables, custom types, etc. - fix(breaking): renamed the update command to edit. - fix(breaking): update the flags for the switch command to make it more generic. - fix(breaking): renamed the show-account-relationship flag to show-relationship. - fix: update the print message from the whoami command.
80 lines
2 KiB
Go
80 lines
2 KiB
Go
package executor
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
|
)
|
|
|
|
type BlockExecutor struct {
|
|
*flag.FlagSet
|
|
|
|
topLevelFlags TopLevelFlags
|
|
resourceType string
|
|
accountName string
|
|
unblock bool
|
|
}
|
|
|
|
func NewBlockExecutor(tlf TopLevelFlags, name, summary string, unblock bool) *BlockExecutor {
|
|
blockExe := BlockExecutor{
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
|
|
topLevelFlags: tlf,
|
|
unblock: unblock,
|
|
}
|
|
|
|
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 *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.NewClientFromConfig(b.topLevelFlags.ConfigDir)
|
|
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 {
|
|
accountID, err := getAccountID(gtsClient, false, b.accountName, b.topLevelFlags.ConfigDir)
|
|
if err != nil {
|
|
return fmt.Errorf("received an error while getting the account ID; %w", err)
|
|
}
|
|
|
|
if b.unblock {
|
|
return b.unblockAccount(gtsClient, accountID)
|
|
}
|
|
|
|
if err := gtsClient.BlockAccount(accountID); err != nil {
|
|
return fmt.Errorf("unable to block the account; %w", err)
|
|
}
|
|
|
|
fmt.Println("Successfully blocked the account.")
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *BlockExecutor) unblockAccount(gtsClient *client.Client, accountID string) error {
|
|
if err := gtsClient.UnblockAccount(accountID); err != nil {
|
|
return fmt.Errorf("unable to unblock the account; %w", err)
|
|
}
|
|
|
|
fmt.Println("Successfully unblocked the account.")
|
|
|
|
return nil
|
|
}
|