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 {
var (
configDir string
pager string
maxTerminalWidth int
noColor *bool
topLevelFlags := executor.TopLevelFlags{
ConfigDir: "",
NoColor: nil,
Pager: "",
}
flag.StringVar(
&topLevelFlags.ConfigDir,
"config-dir",
"",
"Specify your config directory",
)
flag.StringVar(&configDir, "config-dir", "", "Specify your config directory")
flag.StringVar(&pager, "pager", "", "Specify your preferred pager to page through long outputs. This is disabled by default.")
flag.IntVar(&maxTerminalWidth, "max-terminal-width", 80, "Specify the maximum terminal width when displaying resources on screen.")
flag.BoolFunc("no-color", "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)
if err != nil {
return fmt.Errorf("unable to parse %q as a boolean: %w", value, err)
}
noColor = new(bool)
*noColor = boolVal
topLevelFlags.NoColor = new(bool)
*topLevelFlags.NoColor = boolVal
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())
@ -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 == nil {
noColor = new(bool)
if topLevelFlags.NoColor == nil {
topLevelFlags.NoColor = new(bool)
if os.Getenv("NO_COLOR") != "" {
*noColor = true
*topLevelFlags.NoColor = true
} else {
*noColor = false
*topLevelFlags.NoColor = false
}
}
command := flag.Arg(0)
args := flag.Args()[1:]
printer := printer.NewPrinter(*noColor, pager, maxTerminalWidth)
enbasPrinter := printer.NewPrinter(
*topLevelFlags.NoColor,
topLevelFlags.Pager,
80,
)
executorMap := map[string]executor.Executor{
executor.CommandAccept: executor.NewAcceptOrRejectExecutor(
printer,
configDir,
enbasPrinter,
topLevelFlags.ConfigDir,
executor.CommandAccept,
executor.CommandSummaryLookup(executor.CommandAccept),
),
executor.CommandAdd: executor.NewAddExecutor(
printer,
configDir,
enbasPrinter,
topLevelFlags.ConfigDir,
executor.CommandAdd,
executor.CommandSummaryLookup(executor.CommandAdd),
),
executor.CommandBlock: executor.NewBlockOrUnblockExecutor(
printer,
configDir,
enbasPrinter,
topLevelFlags.ConfigDir,
executor.CommandBlock,
executor.CommandSummaryLookup(executor.CommandBlock),
),
executor.CommandCreate: executor.NewCreateExecutor(
printer,
configDir,
enbasPrinter,
topLevelFlags.ConfigDir,
executor.CommandCreate,
executor.CommandSummaryLookup(executor.CommandCreate),
),
executor.CommandDelete: executor.NewDeleteExecutor(
printer,
configDir,
enbasPrinter,
topLevelFlags.ConfigDir,
executor.CommandDelete,
executor.CommandSummaryLookup(executor.CommandDelete),
),
executor.CommandEdit: executor.NewEditExecutor(
printer,
configDir,
enbasPrinter,
topLevelFlags.ConfigDir,
executor.CommandEdit,
executor.CommandSummaryLookup(executor.CommandEdit),
),
executor.CommandFollow: executor.NewFollowOrUnfollowExecutor(
printer,
configDir,
enbasPrinter,
topLevelFlags.ConfigDir,
executor.CommandFollow,
executor.CommandSummaryLookup(executor.CommandFollow),
),
executor.CommandLogin: executor.NewLoginExecutor(
printer,
configDir,
topLevelFlags,
executor.CommandLogin,
executor.CommandSummaryLookup(executor.CommandLogin),
),
executor.CommandReject: executor.NewAcceptOrRejectExecutor(
printer,
configDir,
enbasPrinter,
topLevelFlags.ConfigDir,
executor.CommandReject,
executor.CommandSummaryLookup(executor.CommandReject),
),
executor.CommandRemove: executor.NewRemoveExecutor(
printer,
configDir,
topLevelFlags,
executor.CommandRemove,
executor.CommandSummaryLookup(executor.CommandRemove),
),
executor.CommandSwitch: executor.NewSwitchExecutor(
printer,
configDir,
enbasPrinter,
topLevelFlags.ConfigDir,
executor.CommandSwitch,
executor.CommandSummaryLookup(executor.CommandSwitch),
),
executor.CommandUnfollow: executor.NewFollowOrUnfollowExecutor(
printer,
configDir,
enbasPrinter,
topLevelFlags.ConfigDir,
executor.CommandUnfollow,
executor.CommandSummaryLookup(executor.CommandUnfollow),
),
executor.CommandUnblock: executor.NewBlockOrUnblockExecutor(
printer,
configDir,
enbasPrinter,
topLevelFlags.ConfigDir,
executor.CommandUnblock,
executor.CommandSummaryLookup(executor.CommandUnblock),
),
executor.CommandShow: executor.NewShowExecutor(
printer,
configDir,
enbasPrinter,
topLevelFlags.ConfigDir,
executor.CommandShow,
executor.CommandSummaryLookup(executor.CommandShow),
),
executor.CommandVersion: executor.NewVersionExecutor(
printer,
enbasPrinter,
executor.CommandVersion,
executor.CommandSummaryLookup(executor.CommandVersion),
binaryVersion,
@ -172,8 +187,8 @@ func run() error {
gitCommit,
),
executor.CommandWhoami: executor.NewWhoAmIExecutor(
printer,
configDir,
enbasPrinter,
topLevelFlags.ConfigDir,
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)
}
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
}

View file

@ -51,6 +51,12 @@ const (
flagVisibility = "visibility"
)
type TopLevelFlags struct {
ConfigDir string
NoColor *bool
Pager string
}
type MultiStringFlagValue []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/config"
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
)
type LoginExecutor struct {
*flag.FlagSet
printer *printer.Printer
configDir string
topLevelFlags TopLevelFlags
instance string
}
func NewLoginExecutor(printer *printer.Printer, configDir, name, summary string) *LoginExecutor {
func NewLoginExecutor(tlf TopLevelFlags, name, summary string) *LoginExecutor {
command := LoginExecutor{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
printer: printer,
configDir: configDir,
topLevelFlags: tlf,
instance: "",
}
@ -70,17 +66,21 @@ func (c *LoginExecutor) Execute() error {
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.")
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: ")
%s
c.printer.PrintInfo(builder.String())
Once you have the code please copy and paste it below.
`
fmt.Printf(consentMessageFormat, consentPageURL)
var code string
fmt.Print("Out-of-band token: ")
if _, err := fmt.Scanln(&code); err != nil {
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)
}
loginName, err := config.SaveCredentials(c.configDir, account.Username, gtsClient.Authentication)
loginName, err := config.SaveCredentials(c.topLevelFlags.ConfigDir, account.Username, gtsClient.Authentication)
if err != nil {
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
}

View file

@ -9,14 +9,12 @@ import (
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
)
type RemoveExecutor struct {
*flag.FlagSet
printer *printer.Printer
configDir string
topLevelFlags TopLevelFlags
resourceType string
fromResourceType string
listID string
@ -24,15 +22,13 @@ type RemoveExecutor struct {
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)
removeExe := RemoveExecutor{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
printer: printer,
configDir: configDir,
accountNames: MultiStringFlagValue(emptyArr),
topLevelFlags: tlf,
}
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}
}
gtsClient, err := client.NewClientFromConfig(r.configDir)
gtsClient, err := client.NewClientFromConfig(r.topLevelFlags.ConfigDir)
if err != nil {
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)
}
r.printer.PrintSuccess("Successfully removed the account(s) from the list.")
fmt.Println("Successfully removed the account(s) from the list.")
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))
}
accountID, err := getAccountID(gtsClient, false, r.accountNames[0], r.configDir)
accountID, err := getAccountID(gtsClient, false, r.accountNames[0], r.topLevelFlags.ConfigDir)
if err != nil {
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)
}
r.printer.PrintSuccess("Successfully removed the private note from the account.")
fmt.Println("Successfully removed the private note from the account.")
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)
}
r.printer.PrintSuccess("Successfully removed the status from your bookmarks.")
fmt.Println("Successfully removed the status from your bookmarks.")
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)
}
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
}
@ -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)
}
r.printer.PrintSuccess("Successfully removed the boost from the status.")
fmt.Println("Successfully removed the boost from the status.")
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
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
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
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
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
import (