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

Commit ce00509

Browse files
author
David Chung
authored
Dynamic and context-driven Infrakit CLI (#544)
Signed-off-by: David Chung <[email protected]>
1 parent 8f9f533 commit ce00509

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2749
-422
lines changed

cmd/infrakit/base/output.go

Lines changed: 0 additions & 92 deletions
This file was deleted.

cmd/infrakit/base/template.go

Lines changed: 7 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
11
package base
22

33
import (
4-
"fmt"
54
"io/ioutil"
65
"os"
7-
"path"
8-
"strconv"
9-
"strings"
106

117
"github.com/docker/infrakit/pkg/cli"
128
"github.com/docker/infrakit/pkg/discovery"
13-
"github.com/docker/infrakit/pkg/template"
14-
"github.com/ghodss/yaml"
159
"github.com/spf13/pflag"
1610
)
1711

18-
// ProcessTemplateFunc is the function that processes the template at url and returns view or error.
19-
type ProcessTemplateFunc func(url string) (rendered string, err error)
20-
21-
// ToJSONFunc converts the input buffer to json format
22-
type ToJSONFunc func(in []byte) (json []byte, err error)
23-
24-
// FromJSONFunc converts json formatted input to output buffer
25-
type FromJSONFunc func(json []byte) (out []byte, err error)
26-
2712
// ReadFromStdinIfElse checks condition and reads from stdin if true; otherwise it executes other.
2813
func ReadFromStdinIfElse(
2914
condition func() bool,
3015
otherwise func() (string, error),
31-
toJSON ToJSONFunc) (rendered string, err error) {
16+
toJSON cli.ToJSONFunc) (rendered string, err error) {
3217

3318
if condition() {
3419
buff, err := ioutil.ReadAll(os.Stdin)
@@ -52,111 +37,10 @@ func ReadFromStdinIfElse(
5237
}
5338

5439
// TemplateProcessor returns a flagset and a function for processing template input.
55-
func TemplateProcessor(plugins func() discovery.Plugins) (*pflag.FlagSet, ToJSONFunc, FromJSONFunc, ProcessTemplateFunc) {
56-
57-
fs := pflag.NewFlagSet("template", pflag.ExitOnError)
58-
59-
globals := fs.StringSliceP("var", "v", []string{}, "key=value pairs of globally scoped variagbles")
60-
yamlDoc := fs.BoolP("yaml", "y", false, "True if input is in yaml format; json is the default")
61-
dump := fs.BoolP("dump", "x", false, "True to dump to output instead of executing")
62-
singlePass := fs.BoolP("final", "f", false, "True to render template as the final pass")
63-
64-
return fs,
65-
// ToJSONFunc
66-
func(in []byte) (json []byte, err error) {
67-
68-
defer func() {
69-
70-
if *dump {
71-
fmt.Println("Raw:")
72-
fmt.Println(string(in))
73-
fmt.Println("Converted")
74-
fmt.Println(string(json))
75-
os.Exit(0) // special for debugging
76-
}
77-
}()
78-
79-
if *yamlDoc {
80-
json, err = yaml.YAMLToJSON(in)
81-
return
82-
}
83-
json = in
84-
return
85-
86-
},
87-
// FromJSONFunc
88-
func(json []byte) (out []byte, err error) {
89-
90-
defer func() {
91-
92-
if *dump {
93-
fmt.Println("Raw:")
94-
fmt.Println(string(json))
95-
fmt.Println("Converted")
96-
fmt.Println(string(out))
97-
os.Exit(0) // special for debugging
98-
}
99-
}()
100-
101-
if *yamlDoc {
102-
out, err = yaml.JSONToYAML(json)
103-
return
104-
}
105-
out = json
106-
return
107-
108-
},
109-
// ProcessTemplateFunc
110-
func(url string) (view string, err error) {
111-
112-
if !strings.Contains(url, "://") {
113-
p := url
114-
if dir, err := os.Getwd(); err == nil {
115-
p = path.Join(dir, url)
116-
}
117-
url = "file://" + p
118-
}
119-
120-
log.Debug("reading template", "url", url)
121-
engine, err := template.NewTemplate(url, template.Options{MultiPass: !*singlePass})
122-
if err != nil {
123-
return
124-
}
125-
126-
for _, global := range *globals {
127-
kv := strings.SplitN(global, "=", 2)
128-
if len(kv) != 2 {
129-
log.Warn("bad format kv", "input", global)
130-
continue
131-
}
132-
key := strings.TrimSpace(kv[0])
133-
val := strings.TrimSpace(kv[1])
134-
if key != "" && val != "" {
135-
// Attempt to convert to int and bool types so that template operations
136-
// are not only against strings.
137-
if intVal, err := strconv.Atoi(val); err == nil {
138-
engine.Global(key, intVal)
139-
} else if boolVar, err := strconv.ParseBool(val); err == nil {
140-
engine.Global(key, boolVar)
141-
} else {
142-
engine.Global(key, val)
143-
}
144-
}
145-
}
146-
147-
cli.ConfigureTemplate(engine, plugins)
148-
149-
view, err = engine.Render(nil)
150-
if err != nil {
151-
return
152-
}
153-
154-
log.Debug("rendered", "view", view)
155-
if *dump {
156-
fmt.Println("Final:")
157-
fmt.Println(string(view))
158-
os.Exit(0)
159-
}
160-
return
161-
}
40+
func TemplateProcessor(plugins func() discovery.Plugins) (*pflag.FlagSet, cli.ToJSONFunc, cli.FromJSONFunc, cli.ProcessTemplateFunc) {
41+
services := cli.NewServices(plugins)
42+
return services.ProcessTemplateFlags,
43+
services.ToJSON,
44+
services.FromJSON,
45+
services.ProcessTemplate
16246
}

cmd/infrakit/instance/instance.go

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package instance
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"os"
78
"sort"
89
"strings"
@@ -160,7 +161,7 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
160161
tagsTemplate := describe.Flags().StringP("tags-view", "t", "*", "Template to render tags")
161162
propertiesTemplate := describe.Flags().StringP("properties-view", "v", "{{.}}", "Template to render properties")
162163

163-
rawOutputFlags, rawOutput := base.RawOutput()
164+
rawOutputFlags, rawOutput := cli.Output()
164165
describe.Flags().AddFlagSet(rawOutputFlags)
165166

166167
describe.RunE = func(cmd *cobra.Command, args []string) error {
@@ -185,55 +186,50 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
185186
}
186187

187188
desc, err := instancePlugin.DescribeInstances(filter, *properties)
188-
if err == nil {
189-
190-
rendered, err := rawOutput(os.Stdout, desc)
191-
if err != nil {
192-
return err
193-
}
194-
195-
if rendered {
196-
return nil
197-
}
189+
if err != nil {
190+
return err
191+
}
192+
return rawOutput(os.Stdout, desc,
193+
func(io.Writer, interface{}) error {
198194

199-
if !*quiet {
200-
if *properties {
201-
fmt.Printf("%-30s\t%-30s\t%-30s\t%-s\n", "ID", "LOGICAL", "TAGS", "PROPERTIES")
195+
if !*quiet {
196+
if *properties {
197+
fmt.Printf("%-30s\t%-30s\t%-30s\t%-s\n", "ID", "LOGICAL", "TAGS", "PROPERTIES")
202198

203-
} else {
204-
fmt.Printf("%-30s\t%-30s\t%-s\n", "ID", "LOGICAL", "TAGS")
199+
} else {
200+
fmt.Printf("%-30s\t%-30s\t%-s\n", "ID", "LOGICAL", "TAGS")
201+
}
205202
}
206-
}
207-
for _, d := range desc {
203+
for _, d := range desc {
208204

209-
logical := " - "
210-
if d.LogicalID != nil {
211-
logical = string(*d.LogicalID)
212-
}
205+
logical := " - "
206+
if d.LogicalID != nil {
207+
logical = string(*d.LogicalID)
208+
}
213209

214-
tagViewBuff := ""
215-
if *tagsTemplate == "*" {
216-
// default -- this is a hack
217-
printTags := []string{}
218-
for k, v := range d.Tags {
219-
printTags = append(printTags, fmt.Sprintf("%s=%s", k, v))
210+
tagViewBuff := ""
211+
if *tagsTemplate == "*" {
212+
// default -- this is a hack
213+
printTags := []string{}
214+
for k, v := range d.Tags {
215+
printTags = append(printTags, fmt.Sprintf("%s=%s", k, v))
216+
}
217+
sort.Strings(printTags)
218+
tagViewBuff = strings.Join(printTags, ",")
219+
} else {
220+
tagViewBuff = renderTags(d.Tags, tagsView)
220221
}
221-
sort.Strings(printTags)
222-
tagViewBuff = strings.Join(printTags, ",")
223-
} else {
224-
tagViewBuff = renderTags(d.Tags, tagsView)
225-
}
226222

227-
if *properties {
228-
fmt.Printf("%-30s\t%-30s\t%-30s\t%-s\n", d.ID, logical, tagViewBuff,
229-
renderProperties(d.Properties, propertiesView))
230-
} else {
231-
fmt.Printf("%-30s\t%-30s\t%-s\n", d.ID, logical, tagViewBuff)
223+
if *properties {
224+
fmt.Printf("%-30s\t%-30s\t%-30s\t%-s\n", d.ID, logical, tagViewBuff,
225+
renderProperties(d.Properties, propertiesView))
226+
} else {
227+
fmt.Printf("%-30s\t%-30s\t%-s\n", d.ID, logical, tagViewBuff)
228+
}
232229
}
233-
}
234-
}
235230

236-
return err
231+
return nil
232+
})
237233
}
238234

239235
cmd.AddCommand(

0 commit comments

Comments
 (0)