From 9e3a783ca2b3d821dcd15c30d8720d1f4d326af7 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Tue, 6 May 2025 20:02:30 +0100 Subject: [PATCH 1/2] Simplify list commands --- commands/list_formatters.go | 24 ++-- commands/list_input_options.go | 24 ++-- commands/list_models.go | 226 ++++++--------------------------- go.mod | 4 - go.sum | 8 -- 5 files changed, 63 insertions(+), 223 deletions(-) diff --git a/commands/list_formatters.go b/commands/list_formatters.go index d978d550..e97d5f5e 100644 --- a/commands/list_formatters.go +++ b/commands/list_formatters.go @@ -72,7 +72,7 @@ func (f *TXTListFormatter) Format(list *List, cnf *config.Config) ([]byte, error if len(cmd.Aliases) > 0 { name = name + " (" + strings.Join(cmd.Aliases, ", ") + ")" } - fmt.Fprintf(writer, " %s\t%s\n", name, cmd.Description.String()) + fmt.Fprintf(writer, " %s\t%s\n", name, cmd.Description) } } writer.Flush() @@ -86,7 +86,7 @@ func (f *RawListFormatter) Format(list *List, _ *config.Config) ([]byte, error) var b bytes.Buffer writer := tabwriter.NewWriter(&b, 0, 8, 16, ' ', 0) for _, cmd := range list.Commands { - fmt.Fprintf(writer, "%s\t%s\n", cmd.Name.String(), cmd.Description.String()) + fmt.Fprintf(writer, "%s\t%s\n", cmd.Name.String(), cmd.Description) } writer.Flush() @@ -122,7 +122,7 @@ func (f *MDListFormatter) Format(list *List, cnf *config.Config) ([]byte, error) for _, cmd := range list.Commands { b.H2(md.Code(cmd.Name.String())) - b.Paragraph(cmd.Description.String()).Ln() + b.Paragraph(cmd.Description).Ln() if len(cmd.Aliases) > 0 { aliases := make([]string, 0, len(cmd.Aliases)) @@ -141,13 +141,12 @@ func (f *MDListFormatter) Format(list *List, cnf *config.Config) ([]byte, error) } if cmd.Help != "" { - b.Paragraph(cmd.Help.String()).Ln() + b.Paragraph(cmd.Help).Ln() } - if cmd.Definition.Arguments != nil && cmd.Definition.Arguments.Len() > 0 { + if len(cmd.Definition.Arguments) > 0 { b.H4("Arguments") - for pair := cmd.Definition.Arguments.Oldest(); pair != nil; pair = pair.Next() { - arg := pair.Value + for _, arg := range cmd.Definition.Arguments { line := md.Code(arg.Name) opts := make([]string, 0, 2) if arg.IsRequired { @@ -162,15 +161,14 @@ func (f *MDListFormatter) Format(list *List, cnf *config.Config) ([]byte, error) b.ListItem(line) if arg.Description != "" { - b.Paragraph(" " + arg.Description.String()).Ln() + b.Paragraph(" " + arg.Description).Ln() } } } - if cmd.Definition.Options != nil && cmd.Definition.Options.Len() > 0 { + if len(cmd.Definition.Options) > 0 { b.H4("Options") - for pair := cmd.Definition.Options.Oldest(); pair != nil; pair = pair.Next() { - opt := pair.Value + for _, opt := range cmd.Definition.Options { line := md.Code(opt.Name) if opt.Shortcut != "" { line += " (" + md.Code(opt.Shortcut) + ")" @@ -180,7 +178,7 @@ func (f *MDListFormatter) Format(list *List, cnf *config.Config) ([]byte, error) } b.ListItem(line) if opt.Description != "" { - b.Paragraph(" " + opt.Description.String()).Ln() + b.Paragraph(" " + opt.Description).Ln() } } } @@ -188,7 +186,7 @@ func (f *MDListFormatter) Format(list *List, cnf *config.Config) ([]byte, error) if len(cmd.Examples) > 0 { b.H3("Examples") for _, example := range cmd.Examples { - b.ListItem(example.Description.String() + ":") + b.ListItem(example.Description + ":") b.CodeBlock(cnf.Application.Executable + " " + cmd.Name.String() + " " + example.Commandline).Ln() } } diff --git a/commands/list_input_options.go b/commands/list_input_options.go index c84f649f..b4a32d54 100644 --- a/commands/list_input_options.go +++ b/commands/list_input_options.go @@ -3,8 +3,6 @@ package commands import ( "fmt" - "github.com/fatih/color" - "github.com/platformsh/cli/internal/config" ) @@ -29,10 +27,10 @@ func NoInteractionOption(cnf *config.Config) Option { AcceptValue: false, IsValueRequired: false, IsMultiple: false, - Description: CleanString("Do not ask any interactive questions; accept default values. " + + Description: "Do not ask any interactive questions; accept default values. " + "Equivalent to using the environment variable: " + - color.YellowString(fmt.Sprintf("%sNO_INTERACTION=1", cnf.Application.EnvPrefix))), - Default: Any{false}, + fmt.Sprintf("%sNO_INTERACTION=1", cnf.Application.EnvPrefix), + Default: false, Hidden: false, } } @@ -45,7 +43,7 @@ var ( IsValueRequired: false, IsMultiple: false, Description: "Display this help message", - Default: Any{false}, + Default: false, Hidden: false, } VerboseOption = Option{ @@ -55,7 +53,7 @@ var ( IsValueRequired: false, IsMultiple: false, Description: "Increase the verbosity of messages", - Default: Any{false}, + Default: false, Hidden: false, } VersionOption = Option{ @@ -65,7 +63,7 @@ var ( IsValueRequired: false, IsMultiple: false, Description: "Display this application version", - Default: Any{false}, + Default: false, Hidden: false, } YesOption = Option{ @@ -76,7 +74,7 @@ var ( IsMultiple: false, Description: "Answer \"yes\" to confirmation questions; " + "accept the default value for other questions; disable interaction", - Default: Any{false}, + Default: false, Hidden: false, } AnsiOption = Option{ @@ -86,7 +84,7 @@ var ( IsValueRequired: false, IsMultiple: false, Description: "Force ANSI output", - Default: Any{false}, + Default: false, Hidden: true, } NoAnsiOption = Option{ @@ -96,7 +94,7 @@ var ( IsValueRequired: false, IsMultiple: false, Description: "Disable ANSI output", - Default: Any{false}, + Default: false, Hidden: true, } NoOption = Option{ @@ -107,7 +105,7 @@ var ( IsMultiple: false, Description: "Answer \"no\" to confirmation questions; " + "accept the default value for other questions; disable interaction", - Default: Any{false}, + Default: false, Hidden: true, } QuietOption = Option{ @@ -117,7 +115,7 @@ var ( IsValueRequired: false, IsMultiple: false, Description: "Do not output any message", - Default: Any{false}, + Default: false, Hidden: true, } ) diff --git a/commands/list_models.go b/commands/list_models.go index 7111b6fa..2cef9680 100644 --- a/commands/list_models.go +++ b/commands/list_models.go @@ -4,14 +4,11 @@ import ( "bytes" "encoding/json" "fmt" - "math" - "regexp" "sort" "strings" "text/tabwriter" "github.com/fatih/color" - orderedmap "github.com/wk8/go-ordered-map/v2" "github.com/platformsh/cli/internal/config" ) @@ -39,29 +36,14 @@ func innerProjectInitCommand(cnf *config.Config) Command { }, }, Definition: Definition{ - Arguments: &orderedmap.OrderedMap[string, Argument]{}, - Options: orderedmap.New[string, Option](orderedmap.WithInitialData[string, Option]( - orderedmap.Pair[string, Option]{ - Key: HelpOption.GetName(), - Value: HelpOption, - }, - orderedmap.Pair[string, Option]{ - Key: VerboseOption.GetName(), - Value: VerboseOption, - }, - orderedmap.Pair[string, Option]{ - Key: VersionOption.GetName(), - Value: VersionOption, - }, - orderedmap.Pair[string, Option]{ - Key: YesOption.GetName(), - Value: YesOption, - }, - orderedmap.Pair[string, Option]{ - Key: noInteractionOption.GetName(), - Value: noInteractionOption, - }, - )), + Arguments: []Argument{}, + Options: []Option{ + HelpOption, + VerboseOption, + VersionOption, + YesOption, + noInteractionOption, + }, }, Hidden: false, } @@ -90,29 +72,14 @@ func innerAppConfigValidateCommand(cnf *config.Config) Command { }, }, Definition: Definition{ - Arguments: &orderedmap.OrderedMap[string, Argument]{}, - Options: orderedmap.New[string, Option](orderedmap.WithInitialData[string, Option]( - orderedmap.Pair[string, Option]{ - Key: HelpOption.GetName(), - Value: HelpOption, - }, - orderedmap.Pair[string, Option]{ - Key: VerboseOption.GetName(), - Value: VerboseOption, - }, - orderedmap.Pair[string, Option]{ - Key: VersionOption.GetName(), - Value: VersionOption, - }, - orderedmap.Pair[string, Option]{ - Key: YesOption.GetName(), - Value: YesOption, - }, - orderedmap.Pair[string, Option]{ - Key: noInteractionOption.GetName(), - Value: noInteractionOption, - }, - )), + Arguments: []Argument{}, + Options: []Option{ + HelpOption, + VerboseOption, + VersionOption, + YesOption, + noInteractionOption, + }, }, Hidden: false, } @@ -135,8 +102,8 @@ type Command struct { Name CommandName `json:"name"` Usage []string `json:"usage"` Aliases []string `json:"aliases"` - Description CleanString `json:"description"` - Help CleanString `json:"help"` + Description string `json:"description"` + Help string `json:"help"` Examples []Example `json:"examples"` Definition Definition `json:"definition"` Hidden bool `json:"hidden"` @@ -147,7 +114,7 @@ func (c *Command) HelpPage(cnf *config.Config) string { writer := tabwriter.NewWriter(&b, 0, 8, 1, ' ', 0) fmt.Fprintln(writer, color.YellowString("Command: ")+c.Name.String()) - fmt.Fprintln(writer, color.YellowString("Description: ")+c.Description.String()) + fmt.Fprintln(writer, color.YellowString("Description: ")+c.Description) fmt.Fprintln(writer, "") if len(c.Usage) > 0 { fmt.Fprintln(writer, color.YellowString("Usage:")) @@ -156,18 +123,16 @@ func (c *Command) HelpPage(cnf *config.Config) string { } fmt.Fprintln(writer, "") } - if c.Definition.Arguments != nil && c.Definition.Arguments.Len() > 0 { + if len(c.Definition.Arguments) > 0 { fmt.Fprintln(writer, color.YellowString("Arguments:")) - for pair := c.Definition.Arguments.Oldest(); pair != nil; pair = pair.Next() { - arg := pair.Value + for _, arg := range c.Definition.Arguments { fmt.Fprintf(writer, " %s\t%s\n", color.GreenString(arg.Name), arg.Description) } fmt.Fprintln(writer, "") } - if c.Definition.Options != nil && c.Definition.Options.Len() > 0 { + if len(c.Definition.Options) > 0 { fmt.Fprintln(writer, color.YellowString("Options:")) - for pair := c.Definition.Options.Oldest(); pair != nil; pair = pair.Next() { - opt := pair.Value + for _, opt := range c.Definition.Options { shortcut := opt.Shortcut if shortcut == "" { shortcut = " " @@ -186,7 +151,7 @@ func (c *Command) HelpPage(cnf *config.Config) string { if len(c.Examples) > 0 { fmt.Fprintln(writer, color.YellowString("Examples:")) for _, example := range c.Examples { - fmt.Fprintln(writer, " "+example.Description.String()+":") + fmt.Fprintln(writer, " "+example.Description+":") fmt.Fprintln(writer, color.GreenString(fmt.Sprintf(" %s %s %s", cnf.Application.Executable, c.Name.String(), example.Commandline))) fmt.Fprintln(writer, "") @@ -235,148 +200,39 @@ func (n *CommandName) UnmarshalJSON(text []byte) error { return nil } -type CleanString string - -func (s CleanString) String() string { - return string(s) -} - -func (s *CleanString) MarshalJSON() ([]byte, error) { - buff := new(bytes.Buffer) - encoder := json.NewEncoder(buff) - encoder.SetEscapeHTML(false) - err := encoder.Encode(s.String()) - return buff.Bytes(), err -} - -var ( - //nolint:lll - regexColor = regexp.MustCompile(`<((?:fg=(?P\w+);?)?(?:bg=(?P\w+);?)?(?:options=(?P\w+);?)?)?>(?P