2024-05-23 18:06:49 +01:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
2024-06-25 12:39:39 +01:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
|
2024-06-17 18:59:20 +01:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
|
2024-05-23 18:06:49 +01:00
|
|
|
)
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
type BlockOrUnblockExecutor struct {
|
2024-05-23 18:06:49 +01:00
|
|
|
*flag.FlagSet
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
printer *printer.Printer
|
2024-06-25 12:39:39 +01:00
|
|
|
config *config.Config
|
2024-06-17 18:59:20 +01:00
|
|
|
resourceType string
|
|
|
|
accountName string
|
|
|
|
command string
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
func NewBlockOrUnblockExecutor(printer *printer.Printer, config *config.Config, name, summary string) *BlockOrUnblockExecutor {
|
2024-06-10 18:59:01 +01:00
|
|
|
blockExe := BlockOrUnblockExecutor{
|
2024-05-23 18:06:49 +01:00
|
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
printer: printer,
|
|
|
|
config: config,
|
|
|
|
command: name,
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-09 15:58:10 +01:00
|
|
|
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)")
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
blockExe.Usage = commandUsageFunc(name, summary, blockExe.FlagSet)
|
|
|
|
|
|
|
|
return &blockExe
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
func (b *BlockOrUnblockExecutor) Execute() error {
|
2024-05-23 18:06:49 +01:00
|
|
|
funcMap := map[string]func(*client.Client) error{
|
2024-06-10 18:59:01 +01:00
|
|
|
resourceAccount: b.blockOrUnblockAccount,
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[b.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedTypeError{resourceType: b.resourceType}
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
gtsClient, err := client.NewClientFromFile(b.config.CredentialsFile)
|
2024-05-23 18:06:49 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
func (b *BlockOrUnblockExecutor) blockOrUnblockAccount(gtsClient *client.Client) error {
|
2024-06-18 04:59:32 +01:00
|
|
|
if b.accountName == "" {
|
|
|
|
return FlagNotSetError{flagText: flagAccountName}
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
accountID, err := getAccountID(gtsClient, false, b.accountName, b.config.CredentialsFile)
|
2024-05-23 18:06:49 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("received an error while getting the account ID: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
switch b.command {
|
|
|
|
case CommandBlock:
|
|
|
|
return b.blockAccount(gtsClient, accountID)
|
|
|
|
case CommandUnblock:
|
2024-05-23 18:06:49 +01:00
|
|
|
return b.unblockAccount(gtsClient, accountID)
|
2024-06-10 18:59:01 +01:00
|
|
|
default:
|
|
|
|
return nil
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
2024-06-10 18:59:01 +01:00
|
|
|
}
|
2024-05-23 18:06:49 +01:00
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
func (b *BlockOrUnblockExecutor) blockAccount(gtsClient *client.Client, accountID string) error {
|
2024-05-23 18:06:49 +01:00
|
|
|
if err := gtsClient.BlockAccount(accountID); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to block the account: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
b.printer.PrintSuccess("Successfully blocked the account.")
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:59:01 +01:00
|
|
|
func (b *BlockOrUnblockExecutor) unblockAccount(gtsClient *client.Client, accountID string) error {
|
2024-05-23 18:06:49 +01:00
|
|
|
if err := gtsClient.UnblockAccount(accountID); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to unblock the account: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
b.printer.PrintSuccess("Successfully unblocked the account.")
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|