Compare commits
2 commits
5fb55ed2cf
...
c04cd7e8a5
Author | SHA1 | Date | |
---|---|---|---|
c04cd7e8a5 | |||
ec706b43e9 |
12 changed files with 295 additions and 258 deletions
|
@ -1,13 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
type unknownCommandError struct {
|
|
||||||
subcommand string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e unknownCommandError) Error() string {
|
|
||||||
return "unknown command '" + e.subcommand + "'"
|
|
||||||
}
|
|
|
@ -13,25 +13,6 @@ import (
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/executor"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/executor"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
commandLogin string = "login"
|
|
||||||
commandVersion string = "version"
|
|
||||||
commandShow string = "show"
|
|
||||||
commandSwitch string = "switch"
|
|
||||||
commandCreate string = "create"
|
|
||||||
commandDelete string = "delete"
|
|
||||||
commandEdit string = "edit"
|
|
||||||
commandWhoami string = "whoami"
|
|
||||||
commandAdd string = "add"
|
|
||||||
commandRemove string = "remove"
|
|
||||||
commandFollow string = "follow"
|
|
||||||
commandUnfollow string = "unfollow"
|
|
||||||
commandBlock string = "block"
|
|
||||||
commandUnblock string = "unblock"
|
|
||||||
commandAccept string = "accept"
|
|
||||||
commandReject string = "reject"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
binaryVersion string
|
binaryVersion string
|
||||||
buildTime string
|
buildTime string
|
||||||
|
@ -47,33 +28,23 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func run() error {
|
func run() error {
|
||||||
commandSummaries := map[string]string{
|
|
||||||
commandLogin: "Login to an account on GoToSocial",
|
|
||||||
commandVersion: "Print the application's version and build information",
|
|
||||||
commandShow: "Print details about a specified resource",
|
|
||||||
commandSwitch: "Perform a switch operation (e.g. switch logged in accounts)",
|
|
||||||
commandCreate: "Create a specific resource",
|
|
||||||
commandDelete: "Delete a specific resource",
|
|
||||||
commandEdit: "Edit a specific resource",
|
|
||||||
commandWhoami: "Print the account that you are currently logged in to",
|
|
||||||
commandAdd: "Add a resource to another resource",
|
|
||||||
commandRemove: "Remove a resource from another resource",
|
|
||||||
commandFollow: "Follow a resource (e.g. an account)",
|
|
||||||
commandUnfollow: "Unfollow a resource (e.g. an account)",
|
|
||||||
commandBlock: "Block a resource (e.g. an account)",
|
|
||||||
commandUnblock: "Unblock a resource (e.g. an account)",
|
|
||||||
commandAccept: "Accept a request (e.g. a follow request)",
|
|
||||||
commandReject: "Reject a request (e.g. a follow request)",
|
|
||||||
}
|
|
||||||
|
|
||||||
topLevelFlags := executor.TopLevelFlags{
|
topLevelFlags := executor.TopLevelFlags{
|
||||||
ConfigDir: "",
|
ConfigDir: "",
|
||||||
NoColor: nil,
|
NoColor: nil,
|
||||||
Pager: "",
|
Pager: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
flag.StringVar(&topLevelFlags.ConfigDir, "config-dir", "", "Specify your config directory")
|
flag.StringVar(
|
||||||
flag.BoolFunc("no-color", "Disable ANSI colour output when displaying text on screen", func(value string) error {
|
&topLevelFlags.ConfigDir,
|
||||||
|
"config-dir",
|
||||||
|
"",
|
||||||
|
"Specify your config directory",
|
||||||
|
)
|
||||||
|
|
||||||
|
flag.BoolFunc(
|
||||||
|
"no-color",
|
||||||
|
"Disable ANSI colour output when displaying text on screen",
|
||||||
|
func(value string) error {
|
||||||
boolVal, err := strconv.ParseBool(value)
|
boolVal, err := strconv.ParseBool(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to parse %q as a boolean: %w", value, err)
|
return fmt.Errorf("unable to parse %q as a boolean: %w", value, err)
|
||||||
|
@ -83,10 +54,17 @@ func run() error {
|
||||||
*topLevelFlags.NoColor = boolVal
|
*topLevelFlags.NoColor = boolVal
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
},
|
||||||
flag.StringVar(&topLevelFlags.Pager, "pager", "", "Specify your preferred pager to page through long outputs. This is disabled by default.")
|
)
|
||||||
|
|
||||||
flag.Usage = usageFunc(commandSummaries)
|
flag.StringVar(
|
||||||
|
&topLevelFlags.Pager,
|
||||||
|
"pager",
|
||||||
|
"",
|
||||||
|
"Specify your preferred pager to page through long outputs. This is disabled by default.",
|
||||||
|
)
|
||||||
|
|
||||||
|
flag.Usage = usageFunc(executor.CommandSummaryMap())
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
@ -109,127 +87,100 @@ func run() error {
|
||||||
command := flag.Arg(0)
|
command := flag.Arg(0)
|
||||||
args := flag.Args()[1:]
|
args := flag.Args()[1:]
|
||||||
|
|
||||||
var err error
|
executorMap := map[string]executor.Executor{
|
||||||
|
executor.CommandAccept: executor.NewAcceptOrRejectExecutor(
|
||||||
switch command {
|
|
||||||
case commandAccept:
|
|
||||||
exe := executor.NewAcceptOrRejectExecutor(
|
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
commandAccept,
|
executor.CommandAccept,
|
||||||
commandSummaries[commandAccept],
|
executor.CommandSummaryLookup(executor.CommandAccept),
|
||||||
)
|
),
|
||||||
err = executor.Execute(exe, args)
|
executor.CommandAdd: executor.NewAddExecutor(
|
||||||
case commandAdd:
|
|
||||||
exe := executor.NewAddExecutor(
|
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
commandAdd,
|
executor.CommandAdd,
|
||||||
commandSummaries[commandAdd],
|
executor.CommandSummaryLookup(executor.CommandAdd),
|
||||||
)
|
),
|
||||||
err = executor.Execute(exe, args)
|
executor.CommandBlock: executor.NewBlockOrUnblockExecutor(
|
||||||
case commandBlock:
|
|
||||||
exe := executor.NewBlockExecutor(
|
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
commandBlock,
|
executor.CommandBlock,
|
||||||
commandSummaries[commandBlock],
|
executor.CommandSummaryLookup(executor.CommandBlock),
|
||||||
false,
|
),
|
||||||
)
|
executor.CommandCreate: executor.NewCreateExecutor(
|
||||||
err = executor.Execute(exe, args)
|
|
||||||
case commandCreate:
|
|
||||||
exe := executor.NewCreateExecutor(
|
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
commandCreate,
|
executor.CommandCreate,
|
||||||
commandSummaries[commandCreate],
|
executor.CommandSummaryLookup(executor.CommandCreate),
|
||||||
)
|
),
|
||||||
err = executor.Execute(exe, args)
|
executor.CommandDelete: executor.NewDeleteExecutor(
|
||||||
case commandDelete:
|
|
||||||
exe := executor.NewDeleteExecutor(
|
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
commandDelete,
|
executor.CommandDelete,
|
||||||
commandSummaries[commandDelete],
|
executor.CommandSummaryLookup(executor.CommandDelete),
|
||||||
)
|
),
|
||||||
err = executor.Execute(exe, args)
|
executor.CommandEdit: executor.NewEditExecutor(
|
||||||
case commandEdit:
|
|
||||||
exe := executor.NewEditExecutor(
|
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
commandEdit,
|
executor.CommandEdit,
|
||||||
commandSummaries[commandEdit],
|
executor.CommandSummaryLookup(executor.CommandEdit),
|
||||||
)
|
),
|
||||||
err = executor.Execute(exe, args)
|
executor.CommandFollow: executor.NewFollowOrUnfollowExecutor(
|
||||||
case commandFollow:
|
|
||||||
exe := executor.NewFollowExecutor(
|
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
commandFollow,
|
executor.CommandFollow,
|
||||||
commandSummaries[commandFollow],
|
executor.CommandSummaryLookup(executor.CommandFollow),
|
||||||
false,
|
),
|
||||||
)
|
executor.CommandLogin: executor.NewLoginExecutor(
|
||||||
err = executor.Execute(exe, args)
|
|
||||||
case commandLogin:
|
|
||||||
exe := executor.NewLoginExecutor(
|
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
commandLogin,
|
executor.CommandLogin,
|
||||||
commandSummaries[commandLogin],
|
executor.CommandSummaryLookup(executor.CommandLogin),
|
||||||
)
|
),
|
||||||
err = executor.Execute(exe, args)
|
executor.CommandReject: executor.NewAcceptOrRejectExecutor(
|
||||||
case commandReject:
|
|
||||||
exe := executor.NewAcceptOrRejectExecutor(
|
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
commandReject,
|
executor.CommandReject,
|
||||||
commandSummaries[commandReject],
|
executor.CommandSummaryLookup(executor.CommandReject),
|
||||||
)
|
),
|
||||||
err = executor.Execute(exe, args)
|
executor.CommandRemove: executor.NewRemoveExecutor(
|
||||||
case commandRemove:
|
|
||||||
exe := executor.NewRemoveExecutor(
|
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
commandRemove,
|
executor.CommandRemove,
|
||||||
commandSummaries[commandRemove],
|
executor.CommandSummaryLookup(executor.CommandRemove),
|
||||||
)
|
),
|
||||||
err = executor.Execute(exe, args)
|
executor.CommandSwitch: executor.NewSwitchExecutor(
|
||||||
case commandSwitch:
|
|
||||||
exe := executor.NewSwitchExecutor(
|
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
commandSwitch,
|
executor.CommandSwitch,
|
||||||
commandSummaries[commandSwitch],
|
executor.CommandSummaryLookup(executor.CommandSwitch),
|
||||||
)
|
),
|
||||||
err = executor.Execute(exe, args)
|
executor.CommandUnfollow: executor.NewFollowOrUnfollowExecutor(
|
||||||
case commandUnfollow:
|
|
||||||
exe := executor.NewFollowExecutor(
|
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
commandUnfollow,
|
executor.CommandUnfollow,
|
||||||
commandSummaries[commandUnfollow],
|
executor.CommandSummaryLookup(executor.CommandUnfollow),
|
||||||
true,
|
),
|
||||||
)
|
executor.CommandUnblock: executor.NewBlockOrUnblockExecutor(
|
||||||
err = executor.Execute(exe, args)
|
|
||||||
case commandUnblock:
|
|
||||||
exe := executor.NewBlockExecutor(
|
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
commandUnblock,
|
executor.CommandUnblock,
|
||||||
commandSummaries[commandUnblock],
|
executor.CommandSummaryLookup(executor.CommandUnblock),
|
||||||
true,
|
),
|
||||||
)
|
executor.CommandShow: executor.NewShowExecutor(
|
||||||
err = executor.Execute(exe, args)
|
topLevelFlags,
|
||||||
case commandShow:
|
executor.CommandShow,
|
||||||
exe := executor.NewShowExecutor(topLevelFlags, commandShow, commandSummaries[commandShow])
|
executor.CommandSummaryLookup(executor.CommandShow),
|
||||||
err = executor.Execute(exe, args)
|
),
|
||||||
case commandVersion:
|
executor.CommandVersion: executor.NewVersionExecutor(
|
||||||
exe := executor.NewVersionExecutor(
|
executor.CommandVersion,
|
||||||
commandVersion,
|
executor.CommandSummaryLookup(executor.CommandVersion),
|
||||||
commandSummaries[commandVersion],
|
|
||||||
binaryVersion,
|
binaryVersion,
|
||||||
buildTime,
|
buildTime,
|
||||||
goVersion,
|
goVersion,
|
||||||
gitCommit,
|
gitCommit,
|
||||||
)
|
),
|
||||||
err = executor.Execute(exe, args)
|
executor.CommandWhoami: executor.NewWhoAmIExecutor(
|
||||||
case commandWhoami:
|
topLevelFlags,
|
||||||
exe := executor.NewWhoAmIExecutor(topLevelFlags, commandWhoami, commandSummaries[commandWhoami])
|
executor.CommandWhoami,
|
||||||
err = executor.Execute(exe, args)
|
executor.CommandSummaryLookup(executor.CommandWhoami),
|
||||||
default:
|
),
|
||||||
flag.Usage()
|
|
||||||
|
|
||||||
return unknownCommandError{command}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
exe, ok := executorMap[command]
|
||||||
|
if !ok {
|
||||||
|
flag.Usage()
|
||||||
|
|
||||||
|
return executor.UnknownCommandError{Command: command}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := executor.Execute(exe, args); err != nil {
|
||||||
return fmt.Errorf("(%s) %w", command, err)
|
return fmt.Errorf("(%s) %w", command, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ type AcceptOrRejectExecutor struct {
|
||||||
topLevelFlags TopLevelFlags
|
topLevelFlags TopLevelFlags
|
||||||
resourceType string
|
resourceType string
|
||||||
accountName string
|
accountName string
|
||||||
action string
|
command string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAcceptOrRejectExecutor(tlf TopLevelFlags, name, summary string) *AcceptOrRejectExecutor {
|
func NewAcceptOrRejectExecutor(tlf TopLevelFlags, name, summary string) *AcceptOrRejectExecutor {
|
||||||
|
@ -25,7 +25,7 @@ func NewAcceptOrRejectExecutor(tlf TopLevelFlags, name, summary string) *AcceptO
|
||||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||||
|
|
||||||
topLevelFlags: tlf,
|
topLevelFlags: tlf,
|
||||||
action: name,
|
command: name,
|
||||||
}
|
}
|
||||||
|
|
||||||
acceptExe.StringVar(&acceptExe.resourceType, flagType, "", "Specify the type of resource to accept or reject")
|
acceptExe.StringVar(&acceptExe.resourceType, flagType, "", "Specify the type of resource to accept or reject")
|
||||||
|
@ -60,10 +60,10 @@ func (a *AcceptOrRejectExecutor) acceptOrRejectFollowRequest(gtsClient *client.C
|
||||||
return fmt.Errorf("received an error while getting the account ID: %w", err)
|
return fmt.Errorf("received an error while getting the account ID: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch a.action {
|
switch a.command {
|
||||||
case "accept":
|
case CommandAccept:
|
||||||
return a.acceptFollowRequest(gtsClient, accountID)
|
return a.acceptFollowRequest(gtsClient, accountID)
|
||||||
case "reject":
|
case CommandReject:
|
||||||
return a.rejectFollowRequest(gtsClient, accountID)
|
return a.rejectFollowRequest(gtsClient, accountID)
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -11,21 +11,21 @@ import (
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BlockExecutor struct {
|
type BlockOrUnblockExecutor struct {
|
||||||
*flag.FlagSet
|
*flag.FlagSet
|
||||||
|
|
||||||
topLevelFlags TopLevelFlags
|
topLevelFlags TopLevelFlags
|
||||||
resourceType string
|
resourceType string
|
||||||
accountName string
|
accountName string
|
||||||
unblock bool
|
command string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBlockExecutor(tlf TopLevelFlags, name, summary string, unblock bool) *BlockExecutor {
|
func NewBlockOrUnblockExecutor(tlf TopLevelFlags, name, summary string) *BlockOrUnblockExecutor {
|
||||||
blockExe := BlockExecutor{
|
blockExe := BlockOrUnblockExecutor{
|
||||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||||
|
|
||||||
topLevelFlags: tlf,
|
topLevelFlags: tlf,
|
||||||
unblock: unblock,
|
command: name,
|
||||||
}
|
}
|
||||||
|
|
||||||
blockExe.StringVar(&blockExe.resourceType, flagType, "", "Specify the type of resource to block or unblock")
|
blockExe.StringVar(&blockExe.resourceType, flagType, "", "Specify the type of resource to block or unblock")
|
||||||
|
@ -36,9 +36,9 @@ func NewBlockExecutor(tlf TopLevelFlags, name, summary string, unblock bool) *Bl
|
||||||
return &blockExe
|
return &blockExe
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlockExecutor) Execute() error {
|
func (b *BlockOrUnblockExecutor) Execute() error {
|
||||||
funcMap := map[string]func(*client.Client) error{
|
funcMap := map[string]func(*client.Client) error{
|
||||||
resourceAccount: b.blockAccount,
|
resourceAccount: b.blockOrUnblockAccount,
|
||||||
}
|
}
|
||||||
|
|
||||||
doFunc, ok := funcMap[b.resourceType]
|
doFunc, ok := funcMap[b.resourceType]
|
||||||
|
@ -54,16 +54,23 @@ func (b *BlockExecutor) Execute() error {
|
||||||
return doFunc(gtsClient)
|
return doFunc(gtsClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlockExecutor) blockAccount(gtsClient *client.Client) error {
|
func (b *BlockOrUnblockExecutor) blockOrUnblockAccount(gtsClient *client.Client) error {
|
||||||
accountID, err := getAccountID(gtsClient, false, b.accountName, b.topLevelFlags.ConfigDir)
|
accountID, err := getAccountID(gtsClient, false, b.accountName, b.topLevelFlags.ConfigDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("received an error while getting the account ID: %w", err)
|
return fmt.Errorf("received an error while getting the account ID: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.unblock {
|
switch b.command {
|
||||||
|
case CommandBlock:
|
||||||
|
return b.blockAccount(gtsClient, accountID)
|
||||||
|
case CommandUnblock:
|
||||||
return b.unblockAccount(gtsClient, accountID)
|
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 {
|
if err := gtsClient.BlockAccount(accountID); err != nil {
|
||||||
return fmt.Errorf("unable to block the account: %w", err)
|
return fmt.Errorf("unable to block the account: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -73,7 +80,7 @@ func (b *BlockExecutor) blockAccount(gtsClient *client.Client) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlockExecutor) unblockAccount(gtsClient *client.Client, accountID string) error {
|
func (b *BlockOrUnblockExecutor) unblockAccount(gtsClient *client.Client, accountID string) error {
|
||||||
if err := gtsClient.UnblockAccount(accountID); err != nil {
|
if err := gtsClient.UnblockAccount(accountID); err != nil {
|
||||||
return fmt.Errorf("unable to unblock the account: %w", err)
|
return fmt.Errorf("unable to unblock the account: %w", err)
|
||||||
}
|
}
|
73
internal/executor/commands.go
Normal file
73
internal/executor/commands.go
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
package executor
|
||||||
|
|
||||||
|
const (
|
||||||
|
CommandAccept string = "accept"
|
||||||
|
CommandAdd string = "add"
|
||||||
|
CommandBlock string = "block"
|
||||||
|
CommandCreate string = "create"
|
||||||
|
CommandDelete string = "delete"
|
||||||
|
CommandEdit string = "edit"
|
||||||
|
CommandFollow string = "follow"
|
||||||
|
CommandLogin string = "login"
|
||||||
|
CommandReject string = "reject"
|
||||||
|
CommandRemove string = "remove"
|
||||||
|
CommandShow string = "show"
|
||||||
|
CommandSwitch string = "switch"
|
||||||
|
CommandUnblock string = "unblock"
|
||||||
|
CommandUnfollow string = "unfollow"
|
||||||
|
CommandVersion string = "version"
|
||||||
|
CommandWhoami string = "whoami"
|
||||||
|
|
||||||
|
commandAcceptSummary string = "Accept a request (e.g. a follow request)"
|
||||||
|
commandAddSummary string = "Add a resource to another resource"
|
||||||
|
commandBlockSummary string = "Block a resource (e.g. an account)"
|
||||||
|
commandCreateSummary string = "Create a specific resource"
|
||||||
|
commandDeleteSummary string = "Delete a specific resource"
|
||||||
|
commandEditSummary string = "Edit a specific resource"
|
||||||
|
commandFollowSummary string = "Follow a resource (e.g. an account)"
|
||||||
|
commandLoginSummary string = "Login to an account on GoToSocial"
|
||||||
|
commandRejectSummary string = "Reject a request (e.g. a follow request)"
|
||||||
|
commandRemoveSummary string = "Remove a resource from another resource"
|
||||||
|
commandShowSummary string = "Print details about a specified resource"
|
||||||
|
commandSwitchSummary string = "Perform a switch operation (e.g. switch logged in accounts)"
|
||||||
|
commandUnblockSummary string = "Unblock a resource (e.g. an account)"
|
||||||
|
commandUnfollowSummary string = "Unfollow a resource (e.g. an account)"
|
||||||
|
commandVersionSummary string = "Print the application's version and build information"
|
||||||
|
commandWhoamiSummary string = "Print the account that you are currently logged in to"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CommandSummaryMap() map[string]string {
|
||||||
|
return map[string]string{
|
||||||
|
CommandAccept: commandAcceptSummary,
|
||||||
|
CommandAdd: commandAddSummary,
|
||||||
|
CommandBlock: commandBlockSummary,
|
||||||
|
CommandCreate: commandCreateSummary,
|
||||||
|
CommandDelete: commandDeleteSummary,
|
||||||
|
CommandEdit: commandEditSummary,
|
||||||
|
CommandFollow: commandFollowSummary,
|
||||||
|
CommandLogin: commandLoginSummary,
|
||||||
|
CommandReject: commandRejectSummary,
|
||||||
|
CommandRemove: commandRemoveSummary,
|
||||||
|
CommandShow: commandShowSummary,
|
||||||
|
CommandSwitch: commandSwitchSummary,
|
||||||
|
CommandUnblock: commandUnblockSummary,
|
||||||
|
CommandUnfollow: commandUnfollowSummary,
|
||||||
|
CommandVersion: commandVersionSummary,
|
||||||
|
CommandWhoami: commandWhoamiSummary,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CommandSummaryLookup(command string) string {
|
||||||
|
commandMap := CommandSummaryMap()
|
||||||
|
|
||||||
|
summary, ok := commandMap[command]
|
||||||
|
if !ok {
|
||||||
|
return "This command does not have a summary"
|
||||||
|
}
|
||||||
|
|
||||||
|
return summary
|
||||||
|
}
|
|
@ -1,54 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
||||||
|
|
||||||
package executor
|
|
||||||
|
|
||||||
const (
|
|
||||||
flagAccountName = "account-name"
|
|
||||||
flagBrowser = "browser"
|
|
||||||
flagContentType = "content-type"
|
|
||||||
flagContent = "content"
|
|
||||||
flagEnableFederation = "enable-federation"
|
|
||||||
flagEnableLikes = "enable-likes"
|
|
||||||
flagEnableReplies = "enable-replies"
|
|
||||||
flagEnableReposts = "enable-reposts"
|
|
||||||
flagFrom = "from"
|
|
||||||
flagFromFile = "from-file"
|
|
||||||
flagInstance = "instance"
|
|
||||||
flagLanguage = "language"
|
|
||||||
flagLimit = "limit"
|
|
||||||
flagListID = "list-id"
|
|
||||||
flagListTitle = "list-title"
|
|
||||||
flagListRepliesPolicy = "list-replies-policy"
|
|
||||||
flagMyAccount = "my-account"
|
|
||||||
flagNotify = "notify"
|
|
||||||
flagSensitive = "sensitive"
|
|
||||||
flagSkipRelationship = "skip-relationship"
|
|
||||||
flagShowPreferences = "show-preferences"
|
|
||||||
flagShowReposts = "show-reposts"
|
|
||||||
flagSpoilerText = "spoiler-text"
|
|
||||||
flagStatusID = "status-id"
|
|
||||||
flagTag = "tag"
|
|
||||||
flagTimelineCategory = "timeline-category"
|
|
||||||
flagTo = "to"
|
|
||||||
flagType = "type"
|
|
||||||
flagVisibility = "visibility"
|
|
||||||
|
|
||||||
resourceAccount = "account"
|
|
||||||
resourceBlocked = "blocked"
|
|
||||||
resourceBookmarks = "bookmarks"
|
|
||||||
resourceBoost = "boost"
|
|
||||||
resourceFollowers = "followers"
|
|
||||||
resourceFollowing = "following"
|
|
||||||
resourceFollowRequest = "follow-request"
|
|
||||||
resourceInstance = "instance"
|
|
||||||
resourceLike = "like"
|
|
||||||
resourceLiked = "liked"
|
|
||||||
resourceList = "list"
|
|
||||||
resourceNote = "note"
|
|
||||||
resourceStatus = "status"
|
|
||||||
resourceStar = "star"
|
|
||||||
resourceStarred = "starred"
|
|
||||||
resourceTimeline = "timeline"
|
|
||||||
)
|
|
|
@ -58,3 +58,11 @@ func (e EmptyContentError) Error() string {
|
||||||
|
|
||||||
return message
|
return message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UnknownCommandError struct {
|
||||||
|
Command string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e UnknownCommandError) Error() string {
|
||||||
|
return "unknown command '" + e.Command + "'"
|
||||||
|
}
|
||||||
|
|
|
@ -6,6 +6,39 @@ package executor
|
||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
|
const (
|
||||||
|
flagAccountName = "account-name"
|
||||||
|
flagBrowser = "browser"
|
||||||
|
flagContentType = "content-type"
|
||||||
|
flagContent = "content"
|
||||||
|
flagEnableFederation = "enable-federation"
|
||||||
|
flagEnableLikes = "enable-likes"
|
||||||
|
flagEnableReplies = "enable-replies"
|
||||||
|
flagEnableReposts = "enable-reposts"
|
||||||
|
flagFrom = "from"
|
||||||
|
flagFromFile = "from-file"
|
||||||
|
flagFull = "full"
|
||||||
|
flagInstance = "instance"
|
||||||
|
flagLanguage = "language"
|
||||||
|
flagLimit = "limit"
|
||||||
|
flagListID = "list-id"
|
||||||
|
flagListTitle = "list-title"
|
||||||
|
flagListRepliesPolicy = "list-replies-policy"
|
||||||
|
flagMyAccount = "my-account"
|
||||||
|
flagNotify = "notify"
|
||||||
|
flagSensitive = "sensitive"
|
||||||
|
flagSkipRelationship = "skip-relationship"
|
||||||
|
flagShowPreferences = "show-preferences"
|
||||||
|
flagShowReposts = "show-reposts"
|
||||||
|
flagSpoilerText = "spoiler-text"
|
||||||
|
flagStatusID = "status-id"
|
||||||
|
flagTag = "tag"
|
||||||
|
flagTimelineCategory = "timeline-category"
|
||||||
|
flagTo = "to"
|
||||||
|
flagType = "type"
|
||||||
|
flagVisibility = "visibility"
|
||||||
|
)
|
||||||
|
|
||||||
type AccountNames []string
|
type AccountNames []string
|
||||||
|
|
||||||
func (a *AccountNames) String() string {
|
func (a *AccountNames) String() string {
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FollowExecutor struct {
|
type FollowOrUnfollowExecutor struct {
|
||||||
*flag.FlagSet
|
*flag.FlagSet
|
||||||
|
|
||||||
topLevelFlags TopLevelFlags
|
topLevelFlags TopLevelFlags
|
||||||
|
@ -19,14 +19,15 @@ type FollowExecutor struct {
|
||||||
accountName string
|
accountName string
|
||||||
showReposts bool
|
showReposts bool
|
||||||
notify bool
|
notify bool
|
||||||
unfollow bool
|
action string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFollowExecutor(tlf TopLevelFlags, name, summary string, unfollow bool) *FollowExecutor {
|
func NewFollowOrUnfollowExecutor(tlf TopLevelFlags, name, summary string) *FollowOrUnfollowExecutor {
|
||||||
command := FollowExecutor{
|
command := FollowOrUnfollowExecutor{
|
||||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||||
unfollow: unfollow,
|
|
||||||
topLevelFlags: tlf,
|
topLevelFlags: tlf,
|
||||||
|
action: name,
|
||||||
}
|
}
|
||||||
|
|
||||||
command.StringVar(&command.resourceType, flagType, "", "Specify the type of resource to follow")
|
command.StringVar(&command.resourceType, flagType, "", "Specify the type of resource to follow")
|
||||||
|
@ -39,17 +40,17 @@ func NewFollowExecutor(tlf TopLevelFlags, name, summary string, unfollow bool) *
|
||||||
return &command
|
return &command
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *FollowExecutor) Execute() error {
|
func (f *FollowOrUnfollowExecutor) Execute() error {
|
||||||
funcMap := map[string]func(*client.Client) error{
|
funcMap := map[string]func(*client.Client) error{
|
||||||
resourceAccount: c.followAccount,
|
resourceAccount: f.followOrUnfollowAccount,
|
||||||
}
|
}
|
||||||
|
|
||||||
doFunc, ok := funcMap[c.resourceType]
|
doFunc, ok := funcMap[f.resourceType]
|
||||||
if !ok {
|
if !ok {
|
||||||
return UnsupportedTypeError{resourceType: c.resourceType}
|
return UnsupportedTypeError{resourceType: f.resourceType}
|
||||||
}
|
}
|
||||||
|
|
||||||
gtsClient, err := client.NewClientFromConfig(c.topLevelFlags.ConfigDir)
|
gtsClient, err := client.NewClientFromConfig(f.topLevelFlags.ConfigDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
|
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -57,20 +58,27 @@ func (c *FollowExecutor) Execute() error {
|
||||||
return doFunc(gtsClient)
|
return doFunc(gtsClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *FollowExecutor) followAccount(gtsClient *client.Client) error {
|
func (f *FollowOrUnfollowExecutor) followOrUnfollowAccount(gtsClient *client.Client) error {
|
||||||
accountID, err := getAccountID(gtsClient, false, c.accountName, c.topLevelFlags.ConfigDir)
|
accountID, err := getAccountID(gtsClient, false, f.accountName, f.topLevelFlags.ConfigDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("received an error while getting the account ID: %w", err)
|
return fmt.Errorf("received an error while getting the account ID: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.unfollow {
|
switch f.action {
|
||||||
return c.unfollowAccount(gtsClient, accountID)
|
case CommandFollow:
|
||||||
|
return f.followAccount(gtsClient, accountID)
|
||||||
|
case CommandUnfollow:
|
||||||
|
return f.unfollowAccount(gtsClient, accountID)
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *FollowOrUnfollowExecutor) followAccount(gtsClient *client.Client, accountID string) error {
|
||||||
form := client.FollowAccountForm{
|
form := client.FollowAccountForm{
|
||||||
AccountID: accountID,
|
AccountID: accountID,
|
||||||
ShowReposts: c.showReposts,
|
ShowReposts: f.showReposts,
|
||||||
Notify: c.notify,
|
Notify: f.notify,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := gtsClient.FollowAccount(form); err != nil {
|
if err := gtsClient.FollowAccount(form); err != nil {
|
||||||
|
@ -82,7 +90,7 @@ func (c *FollowExecutor) followAccount(gtsClient *client.Client) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *FollowExecutor) unfollowAccount(gtsClient *client.Client, accountID string) error {
|
func (f *FollowOrUnfollowExecutor) unfollowAccount(gtsClient *client.Client, accountID string) error {
|
||||||
if err := gtsClient.UnfollowAccount(accountID); err != nil {
|
if err := gtsClient.UnfollowAccount(accountID); err != nil {
|
||||||
return fmt.Errorf("unable to unfollow the account: %w", err)
|
return fmt.Errorf("unable to unfollow the account: %w", err)
|
||||||
}
|
}
|
24
internal/executor/resources.go
Normal file
24
internal/executor/resources.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
package executor
|
||||||
|
|
||||||
|
const(
|
||||||
|
resourceAccount = "account"
|
||||||
|
resourceBlocked = "blocked"
|
||||||
|
resourceBookmarks = "bookmarks"
|
||||||
|
resourceBoost = "boost"
|
||||||
|
resourceFollowers = "followers"
|
||||||
|
resourceFollowing = "following"
|
||||||
|
resourceFollowRequest = "follow-request"
|
||||||
|
resourceInstance = "instance"
|
||||||
|
resourceLike = "like"
|
||||||
|
resourceLiked = "liked"
|
||||||
|
resourceList = "list"
|
||||||
|
resourceNote = "note"
|
||||||
|
resourceStatus = "status"
|
||||||
|
resourceStar = "star"
|
||||||
|
resourceStarred = "starred"
|
||||||
|
resourceTimeline = "timeline"
|
||||||
|
)
|
|
@ -30,7 +30,7 @@ func NewVersionExecutor(name, summary, binaryVersion, buildTime, goVersion, gitC
|
||||||
showFullVersion: false,
|
showFullVersion: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
command.BoolVar(&command.showFullVersion, "Full", false, "prints the full build information")
|
command.BoolVar(&command.showFullVersion, flagFull, false, "prints the full build information")
|
||||||
|
|
||||||
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
||||||
|
|
||||||
|
|
|
@ -199,15 +199,15 @@ func (a AccountList) Display(noColor bool) string {
|
||||||
|
|
||||||
switch a.Type {
|
switch a.Type {
|
||||||
case AccountListFollowers:
|
case AccountListFollowers:
|
||||||
output += utilities.HeaderFormat(noColor, "FOLLOWED BY:")
|
output += utilities.HeaderFormat(noColor, "Followed by:")
|
||||||
case AccountListFollowing:
|
case AccountListFollowing:
|
||||||
output += utilities.HeaderFormat(noColor, "FOLLOWING:")
|
output += utilities.HeaderFormat(noColor, "Following:")
|
||||||
case AccountListBlockedAccount:
|
case AccountListBlockedAccount:
|
||||||
output += utilities.HeaderFormat(noColor, "BLOCKED ACCOUNTS:")
|
output += utilities.HeaderFormat(noColor, "Blocked accounts:")
|
||||||
case AccountListFollowRequests:
|
case AccountListFollowRequests:
|
||||||
output += utilities.HeaderFormat(noColor, "ACCOUNTS THAT HAVE REQUESTED TO FOLLOW YOU:")
|
output += utilities.HeaderFormat(noColor, "Accounts that have requested to follow you:")
|
||||||
default:
|
default:
|
||||||
output += utilities.HeaderFormat(noColor, "ACCOUNTS:")
|
output += utilities.HeaderFormat(noColor, "Accounts:")
|
||||||
}
|
}
|
||||||
|
|
||||||
if a.Type == AccountListBlockedAccount {
|
if a.Type == AccountListBlockedAccount {
|
||||||
|
|
Loading…
Reference in a new issue