Skip to content

Commit

Permalink
Create command abstraction and split up command builder
Browse files Browse the repository at this point in the history
  • Loading branch information
thschmitt committed Oct 1, 2024
1 parent 3959adf commit 57bfdec
Show file tree
Hide file tree
Showing 8 changed files with 458 additions and 514 deletions.
29 changes: 8 additions & 21 deletions commandline/autocomplete_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os"
"path/filepath"
"strings"

"github.com/urfave/cli/v2"
)

const directoryPermissions = 0755
Expand Down Expand Up @@ -136,17 +134,12 @@ func (a autoCompleteHandler) writeCompleterHandler(filePath string, completerHan
return nil
}

func (a autoCompleteHandler) Find(commandText string, commands []*cli.Command, exclude []string) []string {
func (a autoCompleteHandler) Find(commandText string, command *commandDefinition, exclude []string) []string {
words := strings.Split(commandText, " ")
if len(words) < 2 {
return []string{}
}

command := &cli.Command{
Name: "uipath",
Subcommands: commands,
}

for _, word := range words[1 : len(words)-1] {
if strings.HasPrefix(word, "-") {
break
Expand All @@ -164,7 +157,7 @@ func (a autoCompleteHandler) Find(commandText string, commands []*cli.Command, e
return a.searchCommands(lastWord, command.Subcommands, exclude)
}

func (a autoCompleteHandler) findCommand(name string, commands []*cli.Command) *cli.Command {
func (a autoCompleteHandler) findCommand(name string, commands []*commandDefinition) *commandDefinition {
for _, command := range commands {
if command.Name == name {
return command
Expand All @@ -173,7 +166,7 @@ func (a autoCompleteHandler) findCommand(name string, commands []*cli.Command) *
return nil
}

func (a autoCompleteHandler) searchCommands(word string, commands []*cli.Command, exclude []string) []string {
func (a autoCompleteHandler) searchCommands(word string, commands []*commandDefinition, exclude []string) []string {
result := []string{}
for _, command := range commands {
if strings.HasPrefix(command.Name, word) {
Expand All @@ -188,22 +181,16 @@ func (a autoCompleteHandler) searchCommands(word string, commands []*cli.Command
return a.removeDuplicates(a.removeExcluded(result, exclude))
}

func (a autoCompleteHandler) searchFlags(word string, command *cli.Command, exclude []string) []string {
func (a autoCompleteHandler) searchFlags(word string, command *commandDefinition, exclude []string) []string {
result := []string{}
for _, flag := range command.Flags {
flagNames := flag.Names()
for _, flagName := range flagNames {
if strings.HasPrefix(flagName, word) {
result = append(result, "--"+flagName)
}
if strings.HasPrefix(flag.Name, word) {
result = append(result, "--"+flag.Name)
}
}
for _, flag := range command.Flags {
flagNames := flag.Names()
for _, flagName := range flagNames {
if strings.Contains(flagName, word) {
result = append(result, "--"+flagName)
}
if strings.Contains(flag.Name, word) {
result = append(result, "--"+flag.Name)
}
}
return a.removeDuplicates(a.removeExcluded(result, exclude))
Expand Down
93 changes: 90 additions & 3 deletions commandline/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c Cli) run(args []string, input utils.Stream) error {
PluginExecutor: c.pluginExecutor,
DefinitionProvider: c.definitionProvider,
}
flags := CommandBuilder.CreateDefaultFlags(false)
flags := CreateDefaultFlags(false)
commands, err := CommandBuilder.Create(args)
if err != nil {
return err
Expand All @@ -51,8 +51,8 @@ func (c Cli) run(args []string, input utils.Stream) error {
Usage: "Command-Line Interface for UiPath Services",
UsageText: "uipath <service> <operation> --parameter",
Version: "1.0",
Flags: flags,
Commands: commands,
Flags: c.convertFlags(flags...),
Commands: c.convertCommands(commands...),
Writer: c.stdOut,
ErrWriter: c.stdErr,
HideVersion: true,
Expand Down Expand Up @@ -89,3 +89,90 @@ func NewCli(
) *Cli {
return &Cli{stdIn, stdOut, stdErr, colors, definitionProvider, configProvider, executor, pluginExecutor}
}

func (c Cli) convertCommands(commands ...*commandDefinition) []*cli.Command {
result := []*cli.Command{}
for _, command := range commands {
c := cli.Command{
Name: command.Name,
Usage: command.Summary,
Description: command.Description,
Flags: c.convertFlags(command.Flags...),
Subcommands: c.convertCommands(command.Subcommands...),
CustomHelpTemplate: command.CustomHelpTemplate,
Hidden: command.Hidden,
HideHelp: true,
}
if command.Action != nil {
c.Action = func(context *cli.Context) error {
return command.Action(&CommandExecContext{context})
}
}
result = append(result, &c)
}
return result
}

func (c Cli) convertFlags(flags ...*flagDefinition) []cli.Flag {
result := []cli.Flag{}
for _, flag := range flags {
envVars := []string{}
if flag.EnvVarName != "" {
envVars = append(envVars, flag.EnvVarName)
}

if flag.Type == "stringarray" {
f := cli.StringSliceFlag{
Name: flag.Name,
Usage: flag.Summary,
EnvVars: envVars,
Required: flag.Required,
Hidden: flag.Hidden,
}
result = append(result, &f)
} else if flag.Type == "int" {
var value int
if flag.Value != nil {
value = flag.Value.(int)
}
f := cli.IntFlag{
Name: flag.Name,
Usage: flag.Summary,
EnvVars: envVars,
Required: flag.Required,
Hidden: flag.Hidden,
Value: value,
}
result = append(result, &f)
} else if flag.Type == "bool" {
var value bool
if flag.Value != nil {
value = flag.Value.(bool)
}
f := cli.BoolFlag{
Name: flag.Name,
Usage: flag.Summary,
EnvVars: envVars,
Required: flag.Required,
Hidden: flag.Hidden,
Value: value,
}
result = append(result, &f)
} else {
var value string
if flag.Value != nil {
value = flag.Value.(string)
}
f := cli.StringFlag{
Name: flag.Name,
Usage: flag.Summary,
EnvVars: envVars,
Required: flag.Required,
Hidden: flag.Hidden,
Value: value,
}
result = append(result, &f)
}
}
return result
}
Loading

0 comments on commit 57bfdec

Please sign in to comment.