-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcmd.go
More file actions
49 lines (44 loc) · 1.17 KB
/
cmd.go
File metadata and controls
49 lines (44 loc) · 1.17 KB
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
package amcmd
import (
"errors"
"fmt"
"github.com/wso2/agent-manager/internal/am/clierr"
"github.com/wso2/agent-manager/internal/am/cmd"
"github.com/wso2/agent-manager/internal/am/cmdutil"
"github.com/wso2/agent-manager/internal/am/config"
"github.com/wso2/agent-manager/internal/am/iostreams"
"github.com/wso2/agent-manager/internal/am/render"
)
// Main loads config, builds the root command, and executes it. Returns the
// process exit code.
func Main() int {
io := iostreams.System()
path, err := config.DefaultPath()
if err != nil {
_ = render.Error(io, render.Scope{}, clierr.Newf(clierr.ConfigNotLoaded, "%v", err))
return 1
}
cfg, err := config.Load(path)
if err != nil {
_ = render.Error(io, render.Scope{}, clierr.Newf(clierr.ConfigNotLoaded, "%v", err))
return 1
}
root, err := cmd.NewRootCmd(cmdutil.NewFactory(cfg, io))
if err != nil {
return 1
}
matched, err := root.ExecuteC()
if err != nil {
if !render.IsRendered(err) {
_ = render.Error(io, render.Scope{}, err)
fmt.Fprintln(io.ErrOut)
fmt.Fprint(io.ErrOut, matched.UsageString())
}
var fe *cmdutil.FlagError
if errors.As(err, &fe) {
return 2
}
return 1
}
return 0
}