-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.go
92 lines (69 loc) · 2.26 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
// Copyright (c) R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package machineroom
import (
"context"
"os"
"github.com/choria-io/fisk"
"github.com/sirupsen/logrus"
)
var (
version = "development"
defaultName = "machine-room"
defaultHelp = "Management Agent"
)
type cliInstance struct {
opts *Options
log *logrus.Entry
cli *fisk.Application
logfile string
loglevel string
debug bool
cfgFile string
isLeader bool
force bool
ctx context.Context
cancel context.CancelFunc
}
func newCli(o Options) (*cliInstance, error) {
app := &cliInstance{opts: &o}
err := app.validateOptions()
if err != nil {
return nil, err
}
app.cli = app.createCli()
return app, nil
}
// Application expose the command line framework allowing new commands to be added to it at compile time
func (c *cliInstance) Application() *fisk.Application {
return c.cli
}
// Run parses and executes the command
func (c *cliInstance) Run(ctx context.Context) error {
c.ctx, c.cancel = context.WithCancel(ctx)
args := os.Args[1:]
if c.opts.Args != nil {
args = c.opts.Args
}
c.cli.MustParseWithUsage(args)
return nil
}
func (c *cliInstance) createCli() *fisk.Application {
cli := fisk.New(c.opts.Name, c.opts.Help)
cli.Author(c.opts.Contact)
cli.Version(c.opts.Version)
cli.HelpFlag.Short('h')
cli.Flag("debug", "Enables debug logging").Default("false").UnNegatableBoolVar(&c.debug)
run := cli.Commandf("run", "Runs the management agent").Action(c.runCommand)
run.Flag("config", "Configuration file to use").Required().StringVar(&c.cfgFile)
reset := cli.Commandf("reset", "Restores the agent to factory defaults").Action(c.resetCommand)
reset.Flag("config", "Configuration file to use").Required().StringVar(&c.cfgFile)
reset.Flag("force", "Force reset without prompting").UnNegatableBoolVar(&c.force)
// generates and saves facts, will be called from auto agents to
// update facts on a schedule hidden as it's basically a private api
facts := cli.Commandf("facts", "Save facts about this node to a file").Action(c.factsCommand).Hidden()
facts.Flag("config", "Configuration file to use").Required().StringVar(&c.cfgFile)
cli.Commandf("buildinfo", "Shows build information").Action(c.buildInfoCommand).Hidden()
return cli
}