-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.go
91 lines (82 loc) · 2.34 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Package commandline is responsible for creating, parsing and validating
// command line arguments.
package commandline
import (
"fmt"
"io"
"github.com/UiPath/uipathcli/config"
"github.com/UiPath/uipathcli/executor"
"github.com/UiPath/uipathcli/utils"
"github.com/urfave/cli/v2"
)
// Cli is a wrapper for building the CLI commands.
type Cli struct {
stdIn io.Reader
stdOut io.Writer
stdErr io.Writer
coloredOutput bool
definitionProvider DefinitionProvider
configProvider config.ConfigProvider
executor executor.Executor
pluginExecutor executor.Executor
}
func (c Cli) run(args []string, input utils.Stream) error {
err := c.configProvider.Load()
if err != nil {
return err
}
CommandBuilder := CommandBuilder{
Input: input,
StdIn: c.stdIn,
StdOut: c.stdOut,
StdErr: c.stdErr,
ConfigProvider: c.configProvider,
Executor: c.executor,
PluginExecutor: c.pluginExecutor,
DefinitionProvider: c.definitionProvider,
}
flags := CommandBuilder.CreateDefaultFlags(false)
commands, err := CommandBuilder.Create(args)
if err != nil {
return err
}
app := &cli.App{
Name: "uipath",
Usage: "Command-Line Interface for UiPath Services",
UsageText: "uipath <service> <operation> --parameter",
Version: "1.0",
Flags: flags,
Commands: commands,
Writer: c.stdOut,
ErrWriter: c.stdErr,
HideVersion: true,
HideHelpCommand: true,
DisableSliceFlagSeparator: true,
}
return app.Run(args)
}
const colorRed = "\033[31m"
const colorReset = "\033[0m"
func (c Cli) Run(args []string, input utils.Stream) error {
err := c.run(args, input)
if err != nil {
message := err.Error()
if c.coloredOutput {
message = colorRed + err.Error() + colorReset
}
fmt.Fprintln(c.stdErr, message)
}
return err
}
func NewCli(
stdIn io.Reader,
stdOut io.Writer,
stdErr io.Writer,
colors bool,
definitionProvider DefinitionProvider,
configProvider config.ConfigProvider,
executor executor.Executor,
pluginExecutor executor.Executor,
) *Cli {
return &Cli{stdIn, stdOut, stdErr, colors, definitionProvider, configProvider, executor, pluginExecutor}
}