-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcli.go
197 lines (178 loc) · 6.61 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package lambroll
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/alecthomas/kong"
"github.com/fatih/color"
"github.com/fujiwara/logutils"
"github.com/samber/lo"
)
type Option struct {
OptionFilePath string `help:"option file path" env:"LAMBROLL_OPTION" name:"option" json:"-"`
Function string `help:"Function file path" env:"LAMBROLL_FUNCTION" json:"function,omitempty"`
LogLevel string `help:"log level (trace, debug, info, warn, error)" default:"info" enum:",trace,debug,info,warn,error" env:"LAMBROLL_LOGLEVEL" json:"log_level"`
Color bool `help:"enable colored output" default:"true" env:"LAMBROLL_COLOR" negatable:"" json:"color,omitempty"`
Region *string `help:"AWS region" env:"AWS_REGION" json:"region,omitempty"`
Profile *string `help:"AWS credential profile name" env:"AWS_PROFILE" json:"profile,omitempty"`
TFState *string `name:"tfstate" help:"URL to terraform.tfstate" env:"LAMBROLL_TFSTATE" json:"tfstate,omitempty"`
PrefixedTFState map[string]string `name:"prefixed-tfstate" help:"key value pair of the prefix for template function name and URL to terraform.tfstate" env:"LAMBROLL_PREFIXED_TFSTATE" json:"prefixed_tfstate,omitempty"`
Endpoint *string `help:"AWS API Lambda Endpoint" env:"AWS_LAMBDA_ENDPOINT" json:"endpoint,omitempty"`
Envfile []string `help:"environment files" env:"LAMBROLL_ENVFILE" json:"envfile,omitempty"`
ExtStr map[string]string `help:"external string values for Jsonnet" env:"LAMBROLL_EXTSTR" json:"extstr,omitempty"`
ExtCode map[string]string `help:"external code values for Jsonnet" env:"LAMBROLL_EXTCODE" json:"extcode,omitempty"`
}
type CLIOptions struct {
Option
Deploy *DeployOption `cmd:"deploy" help:"deploy or create function"`
Init *InitOption `cmd:"init" help:"init function.json"`
List *ListOption `cmd:"list" help:"list functions"`
Rollback *RollbackOption `cmd:"rollback" help:"rollback function"`
Invoke *InvokeOption `cmd:"invoke" help:"invoke function"`
Archive *ArchiveOption `cmd:"archive" help:"archive function"`
Logs *LogsOption `cmd:"logs" help:"show logs of function"`
Diff *DiffOption `cmd:"diff" help:"show diff of function"`
Render *RenderOption `cmd:"render" help:"render function.json"`
Status *StatusOption `cmd:"status" help:"show status of function"`
Delete *DeleteOption `cmd:"delete" help:"delete function"`
Versions *VersionsOption `cmd:"versions" help:"show versions of function"`
Version struct{} `cmd:"version" help:"show version"`
}
type CLIParseFunc func([]string) (string, *CLIOptions, func(), error)
func prepareCLI(args []string) (string, []string, error) {
var opts CLIOptions
p, err := kong.New(&opts)
if err != nil {
return "", nil, fmt.Errorf("failed to new kong: %w", err)
}
if _, err := p.Parse(args); err != nil {
return "", nil, fmt.Errorf("failed to parse args: %w", err)
}
for _, envfile := range opts.Envfile {
if err := exportEnvFile(envfile); err != nil {
return "", nil, fmt.Errorf("failed to load envfile: %w", err)
}
}
return opts.OptionFilePath, opts.Envfile, nil
}
func ParseCLI(args []string) (string, *CLIOptions, func(), error) {
// compatible with v1
if len(args) == 0 || len(args) > 0 && args[0] == "help" {
args = []string{"--help"}
}
// resolve envfile from args at first
optionFilePath, argEnvfiles, err := prepareCLI(args)
if err != nil {
return "", nil, nil, fmt.Errorf("failed to prepare env from args: %w", err)
}
var envfiles []string
kongOpts := []kong.Option{kong.Vars{"version": Version}}
// load default options
if optionFilePath != "" {
defaultOpt, err := loadDefinitionFile[Option](nil, optionFilePath, nil)
if err != nil {
return "", nil, nil, fmt.Errorf("failed to load option file: %w", err)
}
defaultOptBytes, err := json.Marshal(defaultOpt)
if err != nil {
return "", nil, nil, fmt.Errorf("failed to marshal default options: %w", err)
}
resolver, err := kong.JSON(bytes.NewReader(defaultOptBytes))
if err != nil {
return "", nil, nil, fmt.Errorf("failed to parse default options: %w", err)
}
kongOpts = append(kongOpts, kong.Resolvers(resolver))
envfiles = defaultOpt.Envfile
envfiles = append(envfiles, argEnvfiles...)
} else {
envfiles = argEnvfiles
}
var opts CLIOptions
parser, err := kong.New(&opts, kongOpts...)
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)
}
opts.Envfile = lo.Uniq(envfiles) // envfiles are parsed before, so it's safe to overwrite
sub := strings.Fields(c.Command())[0]
return sub, &opts, func() { c.PrintUsage(true) }, nil
}
func CLI(ctx context.Context, parse CLIParseFunc) (int, error) {
sub, opts, usage, err := parse(os.Args[1:])
if err != nil {
return 1, err
}
color.NoColor = !opts.Color
if opts.LogLevel == "" {
opts.LogLevel = DefaultLogLevel
}
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"trace", "debug", "info", "warn", "error"},
ModifierFuncs: []logutils.ModifierFunc{
logutils.Color(color.FgHiWhite), // trace
logutils.Color(color.FgHiBlack), // debug
nil, // info
logutils.Color(color.FgYellow), // warn
logutils.Color(color.FgRed), // error
},
MinLevel: logutils.LogLevel(opts.LogLevel),
Writer: os.Stderr,
}
log.SetOutput(filter)
if err := dispatchCLI(ctx, sub, usage, opts); err != nil {
return 1, err
}
return 0, nil
}
func dispatchCLI(ctx context.Context, sub string, usage func(), opts *CLIOptions) error {
switch sub {
case "version", "":
fmt.Println("lambroll", Version)
return nil
}
app, err := New(ctx, &opts.Option)
if err != nil {
return err
}
if opts.Function != "" {
log.Printf("[info] lambroll %s with %s", Version, opts.Function)
} else {
log.Printf("[info] lambroll %s", Version)
}
switch sub {
case "init":
return app.Init(ctx, opts.Init)
case "list":
return app.List(ctx, opts.List)
case "deploy":
return app.Deploy(ctx, opts.Deploy)
case "invoke":
return app.Invoke(ctx, opts.Invoke)
case "logs":
return app.Logs(ctx, opts.Logs)
case "versions":
return app.Versions(ctx, opts.Versions)
case "archive":
return app.Archive(ctx, opts.Archive)
case "rollback":
return app.Rollback(ctx, opts.Rollback)
case "render":
return app.Render(ctx, opts.Render)
case "diff":
return app.Diff(ctx, opts.Diff)
case "delete":
return app.Delete(ctx, opts.Delete)
case "status":
return app.Status(ctx, opts.Status)
default:
usage()
}
return nil
}