Compare commits

..

No commits in common. "ed2d941259430a8a0fc3802899f51efe1952cf7e" and "a7ad37c5591b4cd87bf256eebdf855cee2b03cd6" have entirely different histories.

10 changed files with 106 additions and 109 deletions

View file

@ -29,28 +29,41 @@ func main() {
} }
func run() error { func run() error {
var ( topLevelFlags := executor.TopLevelFlags{
configDir string ConfigDir: "",
pager string NoColor: nil,
maxTerminalWidth int Pager: "",
noColor *bool }
flag.StringVar(
&topLevelFlags.ConfigDir,
"config-dir",
"",
"Specify your config directory",
) )
flag.StringVar(&configDir, "config-dir", "", "Specify your config directory") flag.BoolFunc(
flag.StringVar(&pager, "pager", "", "Specify your preferred pager to page through long outputs. This is disabled by default.") "no-color",
flag.IntVar(&maxTerminalWidth, "max-terminal-width", 80, "Specify the maximum terminal width when displaying resources on screen.") "Disable ANSI colour output when displaying text on screen",
func(value string) error {
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)
} }
noColor = new(bool) topLevelFlags.NoColor = new(bool)
*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(executor.CommandSummaryMap()) flag.Usage = usageFunc(executor.CommandSummaryMap())
@ -63,107 +76,109 @@ func run() error {
} }
// If NoColor is still unspecified, check to see if the NO_COLOR environment variable is set // If NoColor is still unspecified, check to see if the NO_COLOR environment variable is set
if noColor == nil { if topLevelFlags.NoColor == nil {
noColor = new(bool) topLevelFlags.NoColor = new(bool)
if os.Getenv("NO_COLOR") != "" { if os.Getenv("NO_COLOR") != "" {
*noColor = true *topLevelFlags.NoColor = true
} else { } else {
*noColor = false *topLevelFlags.NoColor = false
} }
} }
command := flag.Arg(0) command := flag.Arg(0)
args := flag.Args()[1:] args := flag.Args()[1:]
printer := printer.NewPrinter(*noColor, pager, maxTerminalWidth) enbasPrinter := printer.NewPrinter(
*topLevelFlags.NoColor,
topLevelFlags.Pager,
80,
)
executorMap := map[string]executor.Executor{ executorMap := map[string]executor.Executor{
executor.CommandAccept: executor.NewAcceptOrRejectExecutor( executor.CommandAccept: executor.NewAcceptOrRejectExecutor(
printer, enbasPrinter,
configDir, topLevelFlags.ConfigDir,
executor.CommandAccept, executor.CommandAccept,
executor.CommandSummaryLookup(executor.CommandAccept), executor.CommandSummaryLookup(executor.CommandAccept),
), ),
executor.CommandAdd: executor.NewAddExecutor( executor.CommandAdd: executor.NewAddExecutor(
printer, enbasPrinter,
configDir, topLevelFlags.ConfigDir,
executor.CommandAdd, executor.CommandAdd,
executor.CommandSummaryLookup(executor.CommandAdd), executor.CommandSummaryLookup(executor.CommandAdd),
), ),
executor.CommandBlock: executor.NewBlockOrUnblockExecutor( executor.CommandBlock: executor.NewBlockOrUnblockExecutor(
printer, enbasPrinter,
configDir, topLevelFlags.ConfigDir,
executor.CommandBlock, executor.CommandBlock,
executor.CommandSummaryLookup(executor.CommandBlock), executor.CommandSummaryLookup(executor.CommandBlock),
), ),
executor.CommandCreate: executor.NewCreateExecutor( executor.CommandCreate: executor.NewCreateExecutor(
printer, enbasPrinter,
configDir, topLevelFlags.ConfigDir,
executor.CommandCreate, executor.CommandCreate,
executor.CommandSummaryLookup(executor.CommandCreate), executor.CommandSummaryLookup(executor.CommandCreate),
), ),
executor.CommandDelete: executor.NewDeleteExecutor( executor.CommandDelete: executor.NewDeleteExecutor(
printer, enbasPrinter,
configDir, topLevelFlags.ConfigDir,
executor.CommandDelete, executor.CommandDelete,
executor.CommandSummaryLookup(executor.CommandDelete), executor.CommandSummaryLookup(executor.CommandDelete),
), ),
executor.CommandEdit: executor.NewEditExecutor( executor.CommandEdit: executor.NewEditExecutor(
printer, enbasPrinter,
configDir, topLevelFlags.ConfigDir,
executor.CommandEdit, executor.CommandEdit,
executor.CommandSummaryLookup(executor.CommandEdit), executor.CommandSummaryLookup(executor.CommandEdit),
), ),
executor.CommandFollow: executor.NewFollowOrUnfollowExecutor( executor.CommandFollow: executor.NewFollowOrUnfollowExecutor(
printer, enbasPrinter,
configDir, topLevelFlags.ConfigDir,
executor.CommandFollow, executor.CommandFollow,
executor.CommandSummaryLookup(executor.CommandFollow), executor.CommandSummaryLookup(executor.CommandFollow),
), ),
executor.CommandLogin: executor.NewLoginExecutor( executor.CommandLogin: executor.NewLoginExecutor(
printer, topLevelFlags,
configDir,
executor.CommandLogin, executor.CommandLogin,
executor.CommandSummaryLookup(executor.CommandLogin), executor.CommandSummaryLookup(executor.CommandLogin),
), ),
executor.CommandReject: executor.NewAcceptOrRejectExecutor( executor.CommandReject: executor.NewAcceptOrRejectExecutor(
printer, enbasPrinter,
configDir, topLevelFlags.ConfigDir,
executor.CommandReject, executor.CommandReject,
executor.CommandSummaryLookup(executor.CommandReject), executor.CommandSummaryLookup(executor.CommandReject),
), ),
executor.CommandRemove: executor.NewRemoveExecutor( executor.CommandRemove: executor.NewRemoveExecutor(
printer, topLevelFlags,
configDir,
executor.CommandRemove, executor.CommandRemove,
executor.CommandSummaryLookup(executor.CommandRemove), executor.CommandSummaryLookup(executor.CommandRemove),
), ),
executor.CommandSwitch: executor.NewSwitchExecutor( executor.CommandSwitch: executor.NewSwitchExecutor(
printer, enbasPrinter,
configDir, topLevelFlags.ConfigDir,
executor.CommandSwitch, executor.CommandSwitch,
executor.CommandSummaryLookup(executor.CommandSwitch), executor.CommandSummaryLookup(executor.CommandSwitch),
), ),
executor.CommandUnfollow: executor.NewFollowOrUnfollowExecutor( executor.CommandUnfollow: executor.NewFollowOrUnfollowExecutor(
printer, enbasPrinter,
configDir, topLevelFlags.ConfigDir,
executor.CommandUnfollow, executor.CommandUnfollow,
executor.CommandSummaryLookup(executor.CommandUnfollow), executor.CommandSummaryLookup(executor.CommandUnfollow),
), ),
executor.CommandUnblock: executor.NewBlockOrUnblockExecutor( executor.CommandUnblock: executor.NewBlockOrUnblockExecutor(
printer, enbasPrinter,
configDir, topLevelFlags.ConfigDir,
executor.CommandUnblock, executor.CommandUnblock,
executor.CommandSummaryLookup(executor.CommandUnblock), executor.CommandSummaryLookup(executor.CommandUnblock),
), ),
executor.CommandShow: executor.NewShowExecutor( executor.CommandShow: executor.NewShowExecutor(
printer, enbasPrinter,
configDir, topLevelFlags.ConfigDir,
executor.CommandShow, executor.CommandShow,
executor.CommandSummaryLookup(executor.CommandShow), executor.CommandSummaryLookup(executor.CommandShow),
), ),
executor.CommandVersion: executor.NewVersionExecutor( executor.CommandVersion: executor.NewVersionExecutor(
printer, enbasPrinter,
executor.CommandVersion, executor.CommandVersion,
executor.CommandSummaryLookup(executor.CommandVersion), executor.CommandSummaryLookup(executor.CommandVersion),
binaryVersion, binaryVersion,
@ -172,8 +187,8 @@ func run() error {
gitCommit, gitCommit,
), ),
executor.CommandWhoami: executor.NewWhoAmIExecutor( executor.CommandWhoami: executor.NewWhoAmIExecutor(
printer, enbasPrinter,
configDir, topLevelFlags.ConfigDir,
executor.CommandWhoami, executor.CommandWhoami,
executor.CommandSummaryLookup(executor.CommandWhoami), executor.CommandSummaryLookup(executor.CommandWhoami),
), ),

View file

@ -232,7 +232,7 @@ func (a *AddExecutor) addStarToStatus(gtsClient *client.Client) error {
return fmt.Errorf("unable to add the %s to the status: %w", a.resourceType, err) return fmt.Errorf("unable to add the %s to the status: %w", a.resourceType, err)
} }
a.printer.PrintSuccess("Successfully added a " + a.resourceType + " to the status.") a.printer.PrintSuccess("Successfully added a " + a.resourceType + " to the status.\n")
return nil return nil
} }

View file

@ -51,6 +51,12 @@ const (
flagVisibility = "visibility" flagVisibility = "visibility"
) )
type TopLevelFlags struct {
ConfigDir string
NoColor *bool
Pager string
}
type MultiStringFlagValue []string type MultiStringFlagValue []string
func (v *MultiStringFlagValue) String() string { func (v *MultiStringFlagValue) String() string {

View file

@ -11,24 +11,20 @@ import (
"codeflow.dananglin.me.uk/apollo/enbas/internal/client" "codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config" "codeflow.dananglin.me.uk/apollo/enbas/internal/config"
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities" "codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
) )
type LoginExecutor struct { type LoginExecutor struct {
*flag.FlagSet *flag.FlagSet
printer *printer.Printer topLevelFlags TopLevelFlags
configDir string
instance string instance string
} }
func NewLoginExecutor(printer *printer.Printer, configDir, name, summary string) *LoginExecutor { func NewLoginExecutor(tlf TopLevelFlags, name, summary string) *LoginExecutor {
command := LoginExecutor{ command := LoginExecutor{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError), FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
topLevelFlags: tlf,
printer: printer,
configDir: configDir,
instance: "", instance: "",
} }
@ -70,17 +66,21 @@ func (c *LoginExecutor) Execute() error {
utilities.OpenLink(consentPageURL) utilities.OpenLink(consentPageURL)
var builder strings.Builder consentMessageFormat := `
You'll need to sign into your GoToSocial's consent page in order to generate the out-of-band token to continue with
the application's login process. Your browser may have opened the link to the consent page already. If not, please
copy and paste the link below to your browser:
builder.WriteString("\nYou'll need to sign into your GoToSocial's consent page in order to generate the out-of-band token to continue with the application's login process.") %s
builder.WriteString("\nYour browser may have opened the link to the consent page already. If not, please copy and paste the link below to your browser:")
builder.WriteString("\n\n" + consentPageURL)
builder.WriteString("\n\n" + "Once you have the code please copy and paste it below.")
builder.WriteString("\n" + "Out-of-band token: ")
c.printer.PrintInfo(builder.String()) Once you have the code please copy and paste it below.
`
fmt.Printf(consentMessageFormat, consentPageURL)
var code string var code string
fmt.Print("Out-of-band token: ")
if _, err := fmt.Scanln(&code); err != nil { if _, err := fmt.Scanln(&code); err != nil {
return fmt.Errorf("failed to read access code: %w", err) return fmt.Errorf("failed to read access code: %w", err)
@ -95,12 +95,12 @@ func (c *LoginExecutor) Execute() error {
return fmt.Errorf("unable to verify the credentials: %w", err) return fmt.Errorf("unable to verify the credentials: %w", err)
} }
loginName, err := config.SaveCredentials(c.configDir, account.Username, gtsClient.Authentication) loginName, err := config.SaveCredentials(c.topLevelFlags.ConfigDir, account.Username, gtsClient.Authentication)
if err != nil { if err != nil {
return fmt.Errorf("unable to save the authentication details: %w", err) return fmt.Errorf("unable to save the authentication details: %w", err)
} }
c.printer.PrintSuccess("Successfully logged into " + loginName + ".") fmt.Printf("Successfully logged into %s\n", loginName)
return nil return nil
} }

View file

@ -9,14 +9,12 @@ import (
"fmt" "fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client" "codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
) )
type RemoveExecutor struct { type RemoveExecutor struct {
*flag.FlagSet *flag.FlagSet
printer *printer.Printer topLevelFlags TopLevelFlags
configDir string
resourceType string resourceType string
fromResourceType string fromResourceType string
listID string listID string
@ -24,15 +22,13 @@ type RemoveExecutor struct {
accountNames MultiStringFlagValue accountNames MultiStringFlagValue
} }
func NewRemoveExecutor(printer *printer.Printer, configDir, name, summary string) *RemoveExecutor { func NewRemoveExecutor(tlf TopLevelFlags, name, summary string) *RemoveExecutor {
emptyArr := make([]string, 0, 3) emptyArr := make([]string, 0, 3)
removeExe := RemoveExecutor{ removeExe := RemoveExecutor{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError), FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
printer: printer,
configDir: configDir,
accountNames: MultiStringFlagValue(emptyArr), accountNames: MultiStringFlagValue(emptyArr),
topLevelFlags: tlf,
} }
removeExe.StringVar(&removeExe.resourceType, flagType, "", "Specify the resource type to remove (e.g. account, note)") removeExe.StringVar(&removeExe.resourceType, flagType, "", "Specify the resource type to remove (e.g. account, note)")
@ -63,7 +59,7 @@ func (r *RemoveExecutor) Execute() error {
return UnsupportedTypeError{resourceType: r.fromResourceType} return UnsupportedTypeError{resourceType: r.fromResourceType}
} }
gtsClient, err := client.NewClientFromConfig(r.configDir) gtsClient, err := client.NewClientFromConfig(r.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)
} }
@ -111,7 +107,7 @@ func (r *RemoveExecutor) removeAccountsFromList(gtsClient *client.Client) error
return fmt.Errorf("unable to remove the accounts from the list: %w", err) return fmt.Errorf("unable to remove the accounts from the list: %w", err)
} }
r.printer.PrintSuccess("Successfully removed the account(s) from the list.") fmt.Println("Successfully removed the account(s) from the list.")
return nil return nil
} }
@ -137,7 +133,7 @@ func (r *RemoveExecutor) removeNoteFromAccount(gtsClient *client.Client) error {
return fmt.Errorf("unexpected number of accounts specified: want 1, got %d", len(r.accountNames)) return fmt.Errorf("unexpected number of accounts specified: want 1, got %d", len(r.accountNames))
} }
accountID, err := getAccountID(gtsClient, false, r.accountNames[0], r.configDir) accountID, err := getAccountID(gtsClient, false, r.accountNames[0], r.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)
} }
@ -146,7 +142,7 @@ func (r *RemoveExecutor) removeNoteFromAccount(gtsClient *client.Client) error {
return fmt.Errorf("unable to remove the private note from the account: %w", err) return fmt.Errorf("unable to remove the private note from the account: %w", err)
} }
r.printer.PrintSuccess("Successfully removed the private note from the account.") fmt.Println("Successfully removed the private note from the account.")
return nil return nil
} }
@ -176,7 +172,7 @@ func (r *RemoveExecutor) removeStatusFromBookmarks(gtsClient *client.Client) err
return fmt.Errorf("unable to remove the status from your bookmarks: %w", err) return fmt.Errorf("unable to remove the status from your bookmarks: %w", err)
} }
r.printer.PrintSuccess("Successfully removed the status from your bookmarks.") fmt.Println("Successfully removed the status from your bookmarks.")
return nil return nil
} }
@ -208,7 +204,7 @@ func (r *RemoveExecutor) removeStarFromStatus(gtsClient *client.Client) error {
return fmt.Errorf("unable to remove the %s from the status: %w", r.resourceType, err) return fmt.Errorf("unable to remove the %s from the status: %w", r.resourceType, err)
} }
r.printer.PrintSuccess("Successfully removed the " + r.resourceType + " from the status.") fmt.Printf("Successfully removed the %s from the status.\n", r.resourceType)
return nil return nil
} }
@ -218,7 +214,7 @@ func (r *RemoveExecutor) removeBoostFromStatus(gtsClient *client.Client) error {
return fmt.Errorf("unable to remove the boost from the status: %w", err) return fmt.Errorf("unable to remove the boost from the status: %w", err)
} }
r.printer.PrintSuccess("Successfully removed the boost from the status.") fmt.Println("Successfully removed the boost from the status.")
return nil return nil
} }

View file

@ -1,7 +1,3 @@
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
package printer package printer
import ( import (

View file

@ -1,7 +1,3 @@
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
package printer package printer
import ( import (

View file

@ -1,7 +1,3 @@
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
package printer package printer
import ( import (

View file

@ -1,7 +1,3 @@
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
package printer package printer
import ( import (

View file

@ -1,7 +1,3 @@
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
package printer package printer
import ( import (