Compare commits
No commits in common. "c04cd7e8a5edcfbca25c4a72af7f05ca6c55080b" and "5fb55ed2cfc4f01444022d4a586e3481cefbde26" have entirely different histories.
c04cd7e8a5
...
5fb55ed2cf
12 changed files with 256 additions and 293 deletions
13
cmd/enbas/errors.go
Normal file
13
cmd/enbas/errors.go
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
// 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,6 +13,25 @@ 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
|
||||||
|
@ -28,23 +47,33 @@ 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(
|
flag.StringVar(&topLevelFlags.ConfigDir, "config-dir", "", "Specify your config directory")
|
||||||
&topLevelFlags.ConfigDir,
|
flag.BoolFunc("no-color", "Disable ANSI colour output when displaying text on screen", func(value string) error {
|
||||||
"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)
|
||||||
|
@ -54,17 +83,10 @@ 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.StringVar(
|
flag.Usage = usageFunc(commandSummaries)
|
||||||
&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()
|
||||||
|
|
||||||
|
@ -87,100 +109,127 @@ func run() error {
|
||||||
command := flag.Arg(0)
|
command := flag.Arg(0)
|
||||||
args := flag.Args()[1:]
|
args := flag.Args()[1:]
|
||||||
|
|
||||||
executorMap := map[string]executor.Executor{
|
var err error
|
||||||
executor.CommandAccept: executor.NewAcceptOrRejectExecutor(
|
|
||||||
|
switch command {
|
||||||
|
case commandAccept:
|
||||||
|
exe := executor.NewAcceptOrRejectExecutor(
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
executor.CommandAccept,
|
commandAccept,
|
||||||
executor.CommandSummaryLookup(executor.CommandAccept),
|
commandSummaries[commandAccept],
|
||||||
),
|
)
|
||||||
executor.CommandAdd: executor.NewAddExecutor(
|
err = executor.Execute(exe, args)
|
||||||
|
case commandAdd:
|
||||||
|
exe := executor.NewAddExecutor(
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
executor.CommandAdd,
|
commandAdd,
|
||||||
executor.CommandSummaryLookup(executor.CommandAdd),
|
commandSummaries[commandAdd],
|
||||||
),
|
)
|
||||||
executor.CommandBlock: executor.NewBlockOrUnblockExecutor(
|
err = executor.Execute(exe, args)
|
||||||
|
case commandBlock:
|
||||||
|
exe := executor.NewBlockExecutor(
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
executor.CommandBlock,
|
commandBlock,
|
||||||
executor.CommandSummaryLookup(executor.CommandBlock),
|
commandSummaries[commandBlock],
|
||||||
),
|
false,
|
||||||
executor.CommandCreate: executor.NewCreateExecutor(
|
)
|
||||||
|
err = executor.Execute(exe, args)
|
||||||
|
case commandCreate:
|
||||||
|
exe := executor.NewCreateExecutor(
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
executor.CommandCreate,
|
commandCreate,
|
||||||
executor.CommandSummaryLookup(executor.CommandCreate),
|
commandSummaries[commandCreate],
|
||||||
),
|
)
|
||||||
executor.CommandDelete: executor.NewDeleteExecutor(
|
err = executor.Execute(exe, args)
|
||||||
|
case commandDelete:
|
||||||
|
exe := executor.NewDeleteExecutor(
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
executor.CommandDelete,
|
commandDelete,
|
||||||
executor.CommandSummaryLookup(executor.CommandDelete),
|
commandSummaries[commandDelete],
|
||||||
),
|
)
|
||||||
executor.CommandEdit: executor.NewEditExecutor(
|
err = executor.Execute(exe, args)
|
||||||
|
case commandEdit:
|
||||||
|
exe := executor.NewEditExecutor(
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
executor.CommandEdit,
|
commandEdit,
|
||||||
executor.CommandSummaryLookup(executor.CommandEdit),
|
commandSummaries[commandEdit],
|
||||||
),
|
)
|
||||||
executor.CommandFollow: executor.NewFollowOrUnfollowExecutor(
|
err = executor.Execute(exe, args)
|
||||||
|
case commandFollow:
|
||||||
|
exe := executor.NewFollowExecutor(
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
executor.CommandFollow,
|
commandFollow,
|
||||||
executor.CommandSummaryLookup(executor.CommandFollow),
|
commandSummaries[commandFollow],
|
||||||
),
|
false,
|
||||||
executor.CommandLogin: executor.NewLoginExecutor(
|
)
|
||||||
|
err = executor.Execute(exe, args)
|
||||||
|
case commandLogin:
|
||||||
|
exe := executor.NewLoginExecutor(
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
executor.CommandLogin,
|
commandLogin,
|
||||||
executor.CommandSummaryLookup(executor.CommandLogin),
|
commandSummaries[commandLogin],
|
||||||
),
|
)
|
||||||
executor.CommandReject: executor.NewAcceptOrRejectExecutor(
|
err = executor.Execute(exe, args)
|
||||||
|
case commandReject:
|
||||||
|
exe := executor.NewAcceptOrRejectExecutor(
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
executor.CommandReject,
|
commandReject,
|
||||||
executor.CommandSummaryLookup(executor.CommandReject),
|
commandSummaries[commandReject],
|
||||||
),
|
)
|
||||||
executor.CommandRemove: executor.NewRemoveExecutor(
|
err = executor.Execute(exe, args)
|
||||||
|
case commandRemove:
|
||||||
|
exe := executor.NewRemoveExecutor(
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
executor.CommandRemove,
|
commandRemove,
|
||||||
executor.CommandSummaryLookup(executor.CommandRemove),
|
commandSummaries[commandRemove],
|
||||||
),
|
)
|
||||||
executor.CommandSwitch: executor.NewSwitchExecutor(
|
err = executor.Execute(exe, args)
|
||||||
|
case commandSwitch:
|
||||||
|
exe := executor.NewSwitchExecutor(
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
executor.CommandSwitch,
|
commandSwitch,
|
||||||
executor.CommandSummaryLookup(executor.CommandSwitch),
|
commandSummaries[commandSwitch],
|
||||||
),
|
)
|
||||||
executor.CommandUnfollow: executor.NewFollowOrUnfollowExecutor(
|
err = executor.Execute(exe, args)
|
||||||
|
case commandUnfollow:
|
||||||
|
exe := executor.NewFollowExecutor(
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
executor.CommandUnfollow,
|
commandUnfollow,
|
||||||
executor.CommandSummaryLookup(executor.CommandUnfollow),
|
commandSummaries[commandUnfollow],
|
||||||
),
|
true,
|
||||||
executor.CommandUnblock: executor.NewBlockOrUnblockExecutor(
|
)
|
||||||
|
err = executor.Execute(exe, args)
|
||||||
|
case commandUnblock:
|
||||||
|
exe := executor.NewBlockExecutor(
|
||||||
topLevelFlags,
|
topLevelFlags,
|
||||||
executor.CommandUnblock,
|
commandUnblock,
|
||||||
executor.CommandSummaryLookup(executor.CommandUnblock),
|
commandSummaries[commandUnblock],
|
||||||
),
|
true,
|
||||||
executor.CommandShow: executor.NewShowExecutor(
|
)
|
||||||
topLevelFlags,
|
err = executor.Execute(exe, args)
|
||||||
executor.CommandShow,
|
case commandShow:
|
||||||
executor.CommandSummaryLookup(executor.CommandShow),
|
exe := executor.NewShowExecutor(topLevelFlags, commandShow, commandSummaries[commandShow])
|
||||||
),
|
err = executor.Execute(exe, args)
|
||||||
executor.CommandVersion: executor.NewVersionExecutor(
|
case commandVersion:
|
||||||
executor.CommandVersion,
|
exe := executor.NewVersionExecutor(
|
||||||
executor.CommandSummaryLookup(executor.CommandVersion),
|
commandVersion,
|
||||||
|
commandSummaries[commandVersion],
|
||||||
binaryVersion,
|
binaryVersion,
|
||||||
buildTime,
|
buildTime,
|
||||||
goVersion,
|
goVersion,
|
||||||
gitCommit,
|
gitCommit,
|
||||||
),
|
)
|
||||||
executor.CommandWhoami: executor.NewWhoAmIExecutor(
|
err = executor.Execute(exe, args)
|
||||||
topLevelFlags,
|
case commandWhoami:
|
||||||
executor.CommandWhoami,
|
exe := executor.NewWhoAmIExecutor(topLevelFlags, commandWhoami, commandSummaries[commandWhoami])
|
||||||
executor.CommandSummaryLookup(executor.CommandWhoami),
|
err = executor.Execute(exe, args)
|
||||||
),
|
default:
|
||||||
}
|
|
||||||
|
|
||||||
exe, ok := executorMap[command]
|
|
||||||
if !ok {
|
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
|
|
||||||
return executor.UnknownCommandError{Command: command}
|
return unknownCommandError{command}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := executor.Execute(exe, args); err != nil {
|
if 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
|
||||||
command string
|
action 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,
|
||||||
command: name,
|
action: 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.command {
|
switch a.action {
|
||||||
case CommandAccept:
|
case "accept":
|
||||||
return a.acceptFollowRequest(gtsClient, accountID)
|
return a.acceptFollowRequest(gtsClient, accountID)
|
||||||
case CommandReject:
|
case "reject":
|
||||||
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 BlockOrUnblockExecutor struct {
|
type BlockExecutor struct {
|
||||||
*flag.FlagSet
|
*flag.FlagSet
|
||||||
|
|
||||||
topLevelFlags TopLevelFlags
|
topLevelFlags TopLevelFlags
|
||||||
resourceType string
|
resourceType string
|
||||||
accountName string
|
accountName string
|
||||||
command string
|
unblock bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBlockOrUnblockExecutor(tlf TopLevelFlags, name, summary string) *BlockOrUnblockExecutor {
|
func NewBlockExecutor(tlf TopLevelFlags, name, summary string, unblock bool) *BlockExecutor {
|
||||||
blockExe := BlockOrUnblockExecutor{
|
blockExe := BlockExecutor{
|
||||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||||
|
|
||||||
topLevelFlags: tlf,
|
topLevelFlags: tlf,
|
||||||
command: name,
|
unblock: unblock,
|
||||||
}
|
}
|
||||||
|
|
||||||
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 NewBlockOrUnblockExecutor(tlf TopLevelFlags, name, summary string) *BlockOr
|
||||||
return &blockExe
|
return &blockExe
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlockOrUnblockExecutor) Execute() error {
|
func (b *BlockExecutor) Execute() error {
|
||||||
funcMap := map[string]func(*client.Client) error{
|
funcMap := map[string]func(*client.Client) error{
|
||||||
resourceAccount: b.blockOrUnblockAccount,
|
resourceAccount: b.blockAccount,
|
||||||
}
|
}
|
||||||
|
|
||||||
doFunc, ok := funcMap[b.resourceType]
|
doFunc, ok := funcMap[b.resourceType]
|
||||||
|
@ -54,23 +54,16 @@ func (b *BlockOrUnblockExecutor) Execute() error {
|
||||||
return doFunc(gtsClient)
|
return doFunc(gtsClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlockOrUnblockExecutor) blockOrUnblockAccount(gtsClient *client.Client) error {
|
func (b *BlockExecutor) blockAccount(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)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch b.command {
|
if b.unblock {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
@ -80,7 +73,7 @@ func (b *BlockOrUnblockExecutor) blockAccount(gtsClient *client.Client, accountI
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlockOrUnblockExecutor) unblockAccount(gtsClient *client.Client, accountID string) error {
|
func (b *BlockExecutor) 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)
|
||||||
}
|
}
|
|
@ -1,73 +0,0 @@
|
||||||
// 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
|
|
||||||
}
|
|
54
internal/executor/const.go
Normal file
54
internal/executor/const.go
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
// 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,11 +58,3 @@ 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,39 +6,6 @@ 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 FollowOrUnfollowExecutor struct {
|
type FollowExecutor struct {
|
||||||
*flag.FlagSet
|
*flag.FlagSet
|
||||||
|
|
||||||
topLevelFlags TopLevelFlags
|
topLevelFlags TopLevelFlags
|
||||||
|
@ -19,15 +19,14 @@ type FollowOrUnfollowExecutor struct {
|
||||||
accountName string
|
accountName string
|
||||||
showReposts bool
|
showReposts bool
|
||||||
notify bool
|
notify bool
|
||||||
action string
|
unfollow bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFollowOrUnfollowExecutor(tlf TopLevelFlags, name, summary string) *FollowOrUnfollowExecutor {
|
func NewFollowExecutor(tlf TopLevelFlags, name, summary string, unfollow bool) *FollowExecutor {
|
||||||
command := FollowOrUnfollowExecutor{
|
command := FollowExecutor{
|
||||||
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")
|
||||||
|
@ -40,17 +39,17 @@ func NewFollowOrUnfollowExecutor(tlf TopLevelFlags, name, summary string) *Follo
|
||||||
return &command
|
return &command
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FollowOrUnfollowExecutor) Execute() error {
|
func (c *FollowExecutor) Execute() error {
|
||||||
funcMap := map[string]func(*client.Client) error{
|
funcMap := map[string]func(*client.Client) error{
|
||||||
resourceAccount: f.followOrUnfollowAccount,
|
resourceAccount: c.followAccount,
|
||||||
}
|
}
|
||||||
|
|
||||||
doFunc, ok := funcMap[f.resourceType]
|
doFunc, ok := funcMap[c.resourceType]
|
||||||
if !ok {
|
if !ok {
|
||||||
return UnsupportedTypeError{resourceType: f.resourceType}
|
return UnsupportedTypeError{resourceType: c.resourceType}
|
||||||
}
|
}
|
||||||
|
|
||||||
gtsClient, err := client.NewClientFromConfig(f.topLevelFlags.ConfigDir)
|
gtsClient, err := client.NewClientFromConfig(c.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)
|
||||||
}
|
}
|
||||||
|
@ -58,27 +57,20 @@ func (f *FollowOrUnfollowExecutor) Execute() error {
|
||||||
return doFunc(gtsClient)
|
return doFunc(gtsClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FollowOrUnfollowExecutor) followOrUnfollowAccount(gtsClient *client.Client) error {
|
func (c *FollowExecutor) followAccount(gtsClient *client.Client) error {
|
||||||
accountID, err := getAccountID(gtsClient, false, f.accountName, f.topLevelFlags.ConfigDir)
|
accountID, err := getAccountID(gtsClient, false, c.accountName, c.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)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch f.action {
|
if c.unfollow {
|
||||||
case CommandFollow:
|
return c.unfollowAccount(gtsClient, accountID)
|
||||||
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: f.showReposts,
|
ShowReposts: c.showReposts,
|
||||||
Notify: f.notify,
|
Notify: c.notify,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := gtsClient.FollowAccount(form); err != nil {
|
if err := gtsClient.FollowAccount(form); err != nil {
|
||||||
|
@ -90,7 +82,7 @@ func (f *FollowOrUnfollowExecutor) followAccount(gtsClient *client.Client, accou
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FollowOrUnfollowExecutor) unfollowAccount(gtsClient *client.Client, accountID string) error {
|
func (c *FollowExecutor) 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)
|
||||||
}
|
}
|
|
@ -1,24 +0,0 @@
|
||||||
// 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, flagFull, false, "prints the full build information")
|
command.BoolVar(&command.showFullVersion, "Full", 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