forked from kayac/ecspresso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cliv2.go
52 lines (46 loc) · 1.24 KB
/
cliv2.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
package ecspresso
import (
"fmt"
"strings"
"github.com/alecthomas/kong"
)
func ParseCLIv2(args []string) (string, *CLIOptions, func(), error) {
// compatible with v1
if len(args) == 0 || len(args) > 0 && args[0] == "help" {
args = []string{"--help"}
}
var opts CLIOptions
parser, err := kong.New(&opts, kong.Vars{"version": Version})
if err != nil {
return "", nil, nil, fmt.Errorf("failed to new kong: %w", err)
}
c, err := parser.Parse(args)
if err != nil {
return "", nil, nil, fmt.Errorf("failed to parse args: %w", err)
}
sub := strings.Fields(c.Command())[0]
for _, envFile := range opts.Envfile {
if err := ExportEnvFile(envFile); err != nil {
return sub, &opts, nil, fmt.Errorf("failed to load envfile: %w", err)
}
}
opts.Option = &Option{
ConfigFilePath: opts.Config,
Debug: opts.Debug,
ExtStr: opts.ExtStr,
ExtCode: opts.ExtCode,
AssumeRoleARN: opts.AssumeRoleARN,
}
if opts.Option.ExtStr == nil {
opts.Option.ExtStr = map[string]string{}
}
if opts.Option.ExtCode == nil {
opts.Option.ExtCode = map[string]string{}
}
switch sub {
case "init":
opts.Init.ConfigFilePath = opts.Config
opts.Option.InitOption = opts.Init
}
return sub, &opts, func() { c.PrintUsage(true) }, nil
}