remove TopLevelFlags for now

This commit is contained in:
Dan Anglin 2024-06-17 09:58:14 +01:00
parent a7ad37c559
commit b899a013df
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
4 changed files with 88 additions and 105 deletions

View file

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

View file

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

View file

@ -11,20 +11,24 @@ 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
topLevelFlags TopLevelFlags
printer *printer.Printer
configDir string
instance string
}
func NewLoginExecutor(tlf TopLevelFlags, name, summary string) *LoginExecutor {
func NewLoginExecutor(printer *printer.Printer, configDir, name, summary string) *LoginExecutor {
command := LoginExecutor{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
topLevelFlags: tlf,
printer: printer,
configDir: configDir,
instance: "",
}
@ -66,21 +70,17 @@ func (c *LoginExecutor) Execute() error {
utilities.OpenLink(consentPageURL)
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:
var builder strings.Builder
%s
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: ")
Once you have the code please copy and paste it below.
`
fmt.Printf(consentMessageFormat, consentPageURL)
c.printer.PrintInfo(builder.String())
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 @@ Once you have the code please copy and paste it below.
return fmt.Errorf("unable to verify the credentials: %w", err)
}
loginName, err := config.SaveCredentials(c.topLevelFlags.ConfigDir, account.Username, gtsClient.Authentication)
loginName, err := config.SaveCredentials(c.configDir, account.Username, gtsClient.Authentication)
if err != nil {
return fmt.Errorf("unable to save the authentication details: %w", err)
}
fmt.Printf("Successfully logged into %s\n", loginName)
c.printer.PrintSuccess("Successfully logged into " + loginName + ".")
return nil
}

View file

@ -9,12 +9,14 @@ import (
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
)
type RemoveExecutor struct {
*flag.FlagSet
topLevelFlags TopLevelFlags
printer *printer.Printer
configDir string
resourceType string
fromResourceType string
listID string
@ -22,13 +24,15 @@ type RemoveExecutor struct {
accountNames MultiStringFlagValue
}
func NewRemoveExecutor(tlf TopLevelFlags, name, summary string) *RemoveExecutor {
func NewRemoveExecutor(printer *printer.Printer, configDir, 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)")
@ -59,7 +63,7 @@ func (r *RemoveExecutor) Execute() error {
return UnsupportedTypeError{resourceType: r.fromResourceType}
}
gtsClient, err := client.NewClientFromConfig(r.topLevelFlags.ConfigDir)
gtsClient, err := client.NewClientFromConfig(r.configDir)
if err != nil {
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
}
@ -107,7 +111,7 @@ func (r *RemoveExecutor) removeAccountsFromList(gtsClient *client.Client) error
return fmt.Errorf("unable to remove the accounts from the list: %w", err)
}
fmt.Println("Successfully removed the account(s) from the list.")
r.printer.PrintSuccess("Successfully removed the account(s) from the list.")
return nil
}
@ -133,7 +137,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.topLevelFlags.ConfigDir)
accountID, err := getAccountID(gtsClient, false, r.accountNames[0], r.configDir)
if err != nil {
return fmt.Errorf("received an error while getting the account ID: %w", err)
}
@ -142,7 +146,7 @@ func (r *RemoveExecutor) removeNoteFromAccount(gtsClient *client.Client) error {
return fmt.Errorf("unable to remove the private note from the account: %w", err)
}
fmt.Println("Successfully removed the private note from the account.")
r.printer.PrintSuccess("Successfully removed the private note from the account.")
return nil
}
@ -172,7 +176,7 @@ func (r *RemoveExecutor) removeStatusFromBookmarks(gtsClient *client.Client) err
return fmt.Errorf("unable to remove the status from your bookmarks: %w", err)
}
fmt.Println("Successfully removed the status from your bookmarks.")
r.printer.PrintSuccess("Successfully removed the status from your bookmarks.")
return nil
}
@ -204,7 +208,7 @@ func (r *RemoveExecutor) removeStarFromStatus(gtsClient *client.Client) error {
return fmt.Errorf("unable to remove the %s from the status: %w", r.resourceType, err)
}
fmt.Printf("Successfully removed the %s from the status.\n", r.resourceType)
r.printer.PrintSuccess("Successfully removed the " + r.resourceType + " from the status.")
return nil
}
@ -214,7 +218,7 @@ func (r *RemoveExecutor) removeBoostFromStatus(gtsClient *client.Client) error {
return fmt.Errorf("unable to remove the boost from the status: %w", err)
}
fmt.Println("Successfully removed the boost from the status.")
r.printer.PrintSuccess("Successfully removed the boost from the status.")
return nil
}