Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 125558d

Browse files
author
David Chung
authored
Refactor cli into modules and packages (#448)
Signed-off-by: David Chung <[email protected]>
1 parent be78504 commit 125558d

File tree

32 files changed

+1197
-169
lines changed

32 files changed

+1197
-169
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ ifneq (,$(findstring .m,$(VERSION)))
112112
@echo "\nWARNING - repository contains uncommitted changes, tagging binaries as dirty\n"
113113
endif
114114

115-
$(call build_binary,infrakit,github.com/docker/infrakit/cmd/cli)
115+
$(call build_binary,infrakit,github.com/docker/infrakit/cmd/cli/main)
116116
$(call build_binary,infrakit-manager,github.com/docker/infrakit/cmd/manager)
117117
$(call build_binary,infrakit-group-default,github.com/docker/infrakit/cmd/group)
118118
$(call build_binary,infrakit-resource,github.com/docker/infrakit/cmd/resource)
@@ -132,7 +132,7 @@ build-cli:
132132
ifneq (,$(findstring .m,$(VERSION)))
133133
@echo "\nWARNING - repository contains uncommitted changes, tagging binaries as dirty\n"
134134
endif
135-
$(call build_binary,infrakit,github.com/docker/infrakit/cmd/cli)
135+
$(call build_binary,infrakit,github.com/docker/infrakit/cmd/cli/main)
136136

137137
install:
138138
@echo "+ $@"

cmd/cli/base/module.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package base
2+
3+
import (
4+
"sync"
5+
6+
"github.com/docker/infrakit/pkg/discovery"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
type module func(func() discovery.Plugins) *cobra.Command
11+
12+
var (
13+
lock sync.Mutex
14+
15+
modules = []module{}
16+
)
17+
18+
// Register registers a command from the modules
19+
func Register(f module) {
20+
21+
lock.Lock()
22+
defer lock.Unlock()
23+
24+
modules = append(modules, f)
25+
}
26+
27+
// VisitModules iterate through all the modules known
28+
func VisitModules(p func() discovery.Plugins, f func(*cobra.Command)) {
29+
lock.Lock()
30+
defer lock.Unlock()
31+
32+
for _, m := range modules {
33+
command := m(p)
34+
f(command)
35+
}
36+
}

cmd/cli/event.go renamed to cmd/cli/event/event.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package main
1+
package event
22

33
import (
44
"fmt"
55
"strings"
66

7-
log "github.com/Sirupsen/logrus"
7+
"github.com/docker/infrakit/cmd/cli/base"
88
"github.com/docker/infrakit/pkg/discovery"
9+
logutil "github.com/docker/infrakit/pkg/log"
910
metadata_template "github.com/docker/infrakit/pkg/plugin/metadata/template"
1011
"github.com/docker/infrakit/pkg/rpc/client"
1112
event_rpc "github.com/docker/infrakit/pkg/rpc/event"
@@ -15,6 +16,12 @@ import (
1516
"github.com/spf13/cobra"
1617
)
1718

19+
var log = logutil.New("module", "cli/event")
20+
21+
func init() {
22+
base.Register(Command)
23+
}
24+
1825
func getEventPlugin(plugins func() discovery.Plugins, name string) (found event.Plugin, err error) {
1926
err = forEventPlugins(plugins, func(n string, p event.Plugin) error {
2027
if n == name {
@@ -62,7 +69,8 @@ func listAllTopics(m event.Plugin, path types.Path) ([]types.Path, error) {
6269
return result, nil
6370
}
6471

65-
func eventCommand(plugins func() discovery.Plugins) *cobra.Command {
72+
// Command is the entry point of the module
73+
func Command(plugins func() discovery.Plugins) *cobra.Command {
6674

6775
cmd := &cobra.Command{
6876
Use: "event",
@@ -124,7 +132,7 @@ func eventCommand(plugins func() discovery.Plugins) *cobra.Command {
124132
if *all {
125133
allPaths, err := listAllTopics(match, path.Shift(1))
126134
if err != nil {
127-
log.Warningln("Cannot event ls on plugin", target, "err=", err)
135+
log.Warn("Cannot event ls on plugin", "target", target, "err", err)
128136
}
129137
for _, c := range allPaths {
130138
nodes = append(nodes, types.PathFromString(target).Join(c))
@@ -137,7 +145,7 @@ func eventCommand(plugins func() discovery.Plugins) *cobra.Command {
137145
} else {
138146
children, err := match.List(path.Shift(1))
139147
if err != nil {
140-
log.Warningln("Cannot event ls on plugin", target, "err=", err)
148+
log.Warn("Cannot event ls on plugin", "target", target, "err", err)
141149
}
142150
for _, c := range children {
143151
nodes = append(nodes, path.JoinString(c))
@@ -191,7 +199,7 @@ func eventCommand(plugins func() discovery.Plugins) *cobra.Command {
191199
Short: "tail a stream by topic",
192200
RunE: func(c *cobra.Command, args []string) error {
193201

194-
log.Infof("Using %v for rendering view.", templateURL)
202+
log.Info("rendering view", "template=", templateURL)
195203
engine, err := template.NewTemplate(templateURL, template.Options{})
196204
if err != nil {
197205
return err
@@ -272,7 +280,7 @@ func eventCommand(plugins func() discovery.Plugins) *cobra.Command {
272280
return fmt.Errorf("not a subscriber: %s, %v", target, plugin)
273281
}
274282

275-
log.Infoln("Subscribing to", eventTopic)
283+
log.Info("Subscribing", "topic", eventTopic)
276284

277285
stream, err := client.SubscribeOn(eventTopic)
278286
if err != nil {
@@ -286,7 +294,7 @@ func eventCommand(plugins func() discovery.Plugins) *cobra.Command {
286294
select {
287295
case evt, ok := <-stream:
288296
if !ok {
289-
log.Infoln("Server disconnected -- topic=", topic)
297+
log.Info("Server disconnected", "topic", topic)
290298
return
291299
}
292300

@@ -311,12 +319,12 @@ func eventCommand(plugins func() discovery.Plugins) *cobra.Command {
311319

312320
case evt, ok := <-collector:
313321
if !ok {
314-
log.Infoln("Server disconnected.")
322+
log.Info("Server disconnected.")
315323
break loop
316324
}
317325
buff, err := engine.Render(evt)
318326
if err != nil {
319-
log.Warningln("error rendering view: %v", err)
327+
log.Warn("error rendering view", "err=", err)
320328
} else {
321329
fmt.Println(buff)
322330
}

cmd/cli/flavor.go renamed to cmd/cli/flavor/flavor.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package flavor
22

33
import (
44
"encoding/json"
@@ -7,8 +7,10 @@ import (
77
"os"
88
"strings"
99

10-
log "github.com/Sirupsen/logrus"
10+
"github.com/docker/infrakit/cmd/cli/base"
11+
"github.com/docker/infrakit/pkg/cli"
1112
"github.com/docker/infrakit/pkg/discovery"
13+
logutil "github.com/docker/infrakit/pkg/log"
1214
"github.com/docker/infrakit/pkg/plugin"
1315
group_types "github.com/docker/infrakit/pkg/plugin/group/types"
1416
flavor_plugin "github.com/docker/infrakit/pkg/rpc/flavor"
@@ -19,7 +21,14 @@ import (
1921
"github.com/spf13/cobra"
2022
)
2123

22-
func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
24+
var log = logutil.New("module", "cli/flavor")
25+
26+
func init() {
27+
base.Register(Command)
28+
}
29+
30+
// Command is the entry point of this module
31+
func Command(plugins func() discovery.Plugins) *cobra.Command {
2332

2433
var flavorPlugin flavor.Plugin
2534

@@ -30,12 +39,7 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
3039
name := cmd.PersistentFlags().String("name", "", "Name of plugin")
3140

3241
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
33-
if err := upTree(c, func(x *cobra.Command, argv []string) error {
34-
if x.PersistentPreRunE != nil {
35-
return x.PersistentPreRunE(x, argv)
36-
}
37-
return nil
38-
}); err != nil {
42+
if err := cli.EnsurePersistentPreRunE(c); err != nil {
3943
return err
4044
}
4145

@@ -49,6 +53,8 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
4953
return err
5054
}
5155
flavorPlugin = p
56+
57+
cli.MustNotNil(flavorPlugin, "flavor plugin not found", "name", *name)
5258
return nil
5359
}
5460

@@ -102,7 +108,6 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
102108
Use: "validate <flavor configuration file>",
103109
Short: "validate a flavor configuration",
104110
RunE: func(cmd *cobra.Command, args []string) error {
105-
assertNotNil("no plugin", flavorPlugin)
106111

107112
if len(args) != 1 {
108113
cmd.Usage()
@@ -111,7 +116,7 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
111116

112117
buff, err := ioutil.ReadFile(args[0])
113118
if err != nil {
114-
log.Error(err)
119+
log.Warn("error", "err", err)
115120
os.Exit(1)
116121
}
117122

@@ -125,7 +130,6 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
125130
Use: "prepare <flavor configuration file> <instance Spec JSON file>",
126131
Short: "prepare provisioning inputs for an instance",
127132
RunE: func(cmd *cobra.Command, args []string) error {
128-
assertNotNil("no plugin", flavorPlugin)
129133

130134
if len(args) != 2 {
131135
cmd.Usage()
@@ -134,13 +138,13 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
134138

135139
flavorProperties, err := ioutil.ReadFile(args[0])
136140
if err != nil {
137-
log.Error(err)
141+
log.Warn("error", "err", err)
138142
os.Exit(1)
139143
}
140144

141145
buff, err := ioutil.ReadFile(args[1])
142146
if err != nil {
143-
log.Error(err)
147+
log.Warn("error", "err", err)
144148
os.Exit(1)
145149
}
146150

@@ -176,7 +180,6 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
176180
id := healthy.Flags().String("id", "", "ID of resource")
177181
logicalID := healthy.Flags().String("logical-id", "", "Logical ID of resource")
178182
healthy.RunE = func(cmd *cobra.Command, args []string) error {
179-
assertNotNil("no plugin", flavorPlugin)
180183

181184
if len(args) != 1 {
182185
cmd.Usage()
@@ -185,7 +188,7 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
185188

186189
flavorProperties, err := ioutil.ReadFile(args[0])
187190
if err != nil {
188-
log.Error(err)
191+
log.Warn("error", "err", err)
189192
os.Exit(1)
190193
}
191194

cmd/cli/group.go renamed to cmd/cli/group/group.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package group
22

33
import (
44
"encoding/json"
@@ -8,20 +8,29 @@ import (
88
"sort"
99
"strings"
1010

11-
log "github.com/Sirupsen/logrus"
11+
"github.com/docker/infrakit/cmd/cli/base"
12+
"github.com/docker/infrakit/pkg/cli"
1213
"github.com/docker/infrakit/pkg/discovery"
14+
logutil "github.com/docker/infrakit/pkg/log"
1315
"github.com/docker/infrakit/pkg/plugin"
1416
group_plugin "github.com/docker/infrakit/pkg/rpc/group"
1517
"github.com/docker/infrakit/pkg/spi/group"
1618
"github.com/spf13/cobra"
1719
)
1820

21+
var log = logutil.New("module", "cli/group")
22+
1923
const (
2024
// DefaultGroupPluginName specifies the default name of the group plugin if name flag isn't specified.
2125
DefaultGroupPluginName = "group"
2226
)
2327

24-
func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
28+
func init() {
29+
base.Register(Command)
30+
}
31+
32+
// Command is the entrypoint to this module
33+
func Command(plugins func() discovery.Plugins) *cobra.Command {
2534

2635
var groupPlugin group.Plugin
2736

@@ -31,12 +40,7 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
3140
}
3241
name := cmd.PersistentFlags().String("name", DefaultGroupPluginName, "Name of plugin")
3342
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
34-
if err := upTree(c, func(x *cobra.Command, argv []string) error {
35-
if x.PersistentPreRunE != nil {
36-
return x.PersistentPreRunE(x, argv)
37-
}
38-
return nil
39-
}); err != nil {
43+
if err := cli.EnsurePersistentPreRunE(c); err != nil {
4044
return err
4145
}
4246

@@ -50,6 +54,8 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
5054
return err
5155
}
5256
groupPlugin = p
57+
58+
cli.MustNotNil(groupPlugin, "group plugin not found", "name", *name)
5359
return nil
5460
}
5561

@@ -59,7 +65,6 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
5965
}
6066
pretend := commit.Flags().Bool("pretend", false, "Don't actually commit, only explain the commit")
6167
commit.RunE = func(cmd *cobra.Command, args []string) error {
62-
assertNotNil("no plugin", groupPlugin)
6368

6469
if len(args) != 1 {
6570
cmd.Usage()
@@ -68,7 +73,7 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
6873

6974
buff, err := ioutil.ReadFile(args[0])
7075
if err != nil {
71-
log.Error(err)
76+
log.Warn("error", "err", err)
7277
os.Exit(1)
7378
}
7479

@@ -93,7 +98,6 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
9398
Use: "free <group ID>",
9499
Short: "free a group from active monitoring, nondestructive",
95100
RunE: func(cmd *cobra.Command, args []string) error {
96-
assertNotNil("no plugin", groupPlugin)
97101

98102
if len(args) != 1 {
99103
cmd.Usage()
@@ -115,7 +119,6 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
115119
}
116120
quietDescribe := describe.Flags().BoolP("quiet", "q", false, "Print rows without column headers")
117121
describe.RunE = func(cmd *cobra.Command, args []string) error {
118-
assertNotNil("no plugin", groupPlugin)
119122

120123
if len(args) != 1 {
121124
cmd.Usage()
@@ -152,7 +155,6 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
152155
Use: "inspect <group ID>",
153156
Short: "return the raw configuration associated with a group",
154157
RunE: func(cmd *cobra.Command, args []string) error {
155-
assertNotNil("no plugin", groupPlugin)
156158

157159
if len(args) != 1 {
158160
cmd.Usage()
@@ -187,7 +189,6 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
187189
Use: "destroy <group ID>",
188190
Short: "destroy a group",
189191
RunE: func(cmd *cobra.Command, args []string) error {
190-
assertNotNil("no plugin", groupPlugin)
191192

192193
if len(args) != 1 {
193194
cmd.Usage()
@@ -210,7 +211,6 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
210211
}
211212
quietls := describeGroups.Flags().BoolP("quiet", "q", false, "Print rows without column headers")
212213
describeGroups.RunE = func(cmd *cobra.Command, args []string) error {
213-
assertNotNil("no plugin", groupPlugin)
214214

215215
groups, err := groupPlugin.InspectGroups()
216216
if err == nil {

0 commit comments

Comments
 (0)