-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
128 lines (106 loc) · 2.44 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
package cli
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
cli "github.com/urfave/cli"
"github.com/ipsn/go-ipfs/gxlibs/github.com/ipfs/iptb/commands"
"github.com/ipsn/go-ipfs/gxlibs/github.com/ipfs/iptb/testbed"
)
func loadPlugins(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return nil
}
plugs, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, f := range plugs {
plg, err := testbed.LoadPlugin(path.Join(dir, f.Name()))
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
continue
}
overloaded, err := testbed.RegisterPlugin(*plg, false)
if overloaded {
fmt.Fprintf(os.Stderr, "overriding built in plugin %s with %s\n", plg.PluginName, path.Join(dir, f.Name()))
}
if err != nil {
return err
}
}
return nil
}
func NewCli() *cli.App {
app := cli.NewApp()
app.Usage = "iptb is a tool for managing test clusters of libp2p nodes"
app.Version = "2.0.0"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "testbed",
Value: "default",
EnvVar: "IPTB_TESTBED",
Usage: "Name of testbed to use under IPTB_ROOT",
},
cli.BoolFlag{
Name: "quiet",
Usage: "Suppresses extra output from iptb",
},
cli.StringFlag{
Name: "IPTB_ROOT",
EnvVar: "IPTB_ROOT",
Hidden: true,
},
}
app.Before = func(c *cli.Context) error {
flagRoot := c.GlobalString("IPTB_ROOT")
if len(flagRoot) == 0 {
home := os.Getenv("HOME")
if len(home) == 0 {
return fmt.Errorf("environment variable HOME not set")
}
flagRoot = path.Join(home, "testbed")
} else {
var err error
flagRoot, err = filepath.Abs(flagRoot)
if err != nil {
return err
}
}
c.Set("IPTB_ROOT", flagRoot)
return loadPlugins(path.Join(flagRoot, "plugins"))
}
app.Commands = []cli.Command{
commands.AutoCmd,
commands.TestbedCmd,
commands.InitCmd,
commands.StartCmd,
commands.StopCmd,
commands.RestartCmd,
commands.RunCmd,
commands.ConnectCmd,
commands.ShellCmd,
commands.AttrCmd,
commands.LogsCmd,
commands.EventsCmd,
commands.MetricCmd,
}
// https://github.com/urfave/cli/issues/736
// Currently unreleased
/*
app.ExitErrHandler = func(c *cli.Context, err error) {
switch err.(type) {
case *commands.UsageError:
fmt.Fprintf(c.App.ErrWriter, "%s\n\n", err)
cli.ShowCommandHelpAndExit(c, c.Command.Name, 1)
default:
cli.HandleExitCoder(err)
}
}
*/
app.ErrWriter = os.Stderr
app.Writer = os.Stdout
return app
}