Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 0 additions & 92 deletions cmd/infrakit/base/output.go

This file was deleted.

130 changes: 7 additions & 123 deletions cmd/infrakit/base/template.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
package base

import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"strings"

"github.com/docker/infrakit/pkg/cli"
"github.com/docker/infrakit/pkg/discovery"
"github.com/docker/infrakit/pkg/template"
"github.com/ghodss/yaml"
"github.com/spf13/pflag"
)

// ProcessTemplateFunc is the function that processes the template at url and returns view or error.
type ProcessTemplateFunc func(url string) (rendered string, err error)

// ToJSONFunc converts the input buffer to json format
type ToJSONFunc func(in []byte) (json []byte, err error)

// FromJSONFunc converts json formatted input to output buffer
type FromJSONFunc func(json []byte) (out []byte, err error)

// ReadFromStdinIfElse checks condition and reads from stdin if true; otherwise it executes other.
func ReadFromStdinIfElse(
condition func() bool,
otherwise func() (string, error),
toJSON ToJSONFunc) (rendered string, err error) {
toJSON cli.ToJSONFunc) (rendered string, err error) {

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

// TemplateProcessor returns a flagset and a function for processing template input.
func TemplateProcessor(plugins func() discovery.Plugins) (*pflag.FlagSet, ToJSONFunc, FromJSONFunc, ProcessTemplateFunc) {

fs := pflag.NewFlagSet("template", pflag.ExitOnError)

globals := fs.StringSliceP("var", "v", []string{}, "key=value pairs of globally scoped variagbles")
yamlDoc := fs.BoolP("yaml", "y", false, "True if input is in yaml format; json is the default")
dump := fs.BoolP("dump", "x", false, "True to dump to output instead of executing")
singlePass := fs.BoolP("final", "f", false, "True to render template as the final pass")

return fs,
// ToJSONFunc
func(in []byte) (json []byte, err error) {

defer func() {

if *dump {
fmt.Println("Raw:")
fmt.Println(string(in))
fmt.Println("Converted")
fmt.Println(string(json))
os.Exit(0) // special for debugging
}
}()

if *yamlDoc {
json, err = yaml.YAMLToJSON(in)
return
}
json = in
return

},
// FromJSONFunc
func(json []byte) (out []byte, err error) {

defer func() {

if *dump {
fmt.Println("Raw:")
fmt.Println(string(json))
fmt.Println("Converted")
fmt.Println(string(out))
os.Exit(0) // special for debugging
}
}()

if *yamlDoc {
out, err = yaml.JSONToYAML(json)
return
}
out = json
return

},
// ProcessTemplateFunc
func(url string) (view string, err error) {

if !strings.Contains(url, "://") {
p := url
if dir, err := os.Getwd(); err == nil {
p = path.Join(dir, url)
}
url = "file://" + p
}

log.Debug("reading template", "url", url)
engine, err := template.NewTemplate(url, template.Options{MultiPass: !*singlePass})
if err != nil {
return
}

for _, global := range *globals {
kv := strings.SplitN(global, "=", 2)
if len(kv) != 2 {
log.Warn("bad format kv", "input", global)
continue
}
key := strings.TrimSpace(kv[0])
val := strings.TrimSpace(kv[1])
if key != "" && val != "" {
// Attempt to convert to int and bool types so that template operations
// are not only against strings.
if intVal, err := strconv.Atoi(val); err == nil {
engine.Global(key, intVal)
} else if boolVar, err := strconv.ParseBool(val); err == nil {
engine.Global(key, boolVar)
} else {
engine.Global(key, val)
}
}
}

cli.ConfigureTemplate(engine, plugins)

view, err = engine.Render(nil)
if err != nil {
return
}

log.Debug("rendered", "view", view)
if *dump {
fmt.Println("Final:")
fmt.Println(string(view))
os.Exit(0)
}
return
}
func TemplateProcessor(plugins func() discovery.Plugins) (*pflag.FlagSet, cli.ToJSONFunc, cli.FromJSONFunc, cli.ProcessTemplateFunc) {
services := cli.NewServices(plugins)
return services.ProcessTemplateFlags,
services.ToJSON,
services.FromJSON,
services.ProcessTemplate
}
78 changes: 37 additions & 41 deletions cmd/infrakit/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package instance
import (
"bytes"
"fmt"
"io"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -160,7 +161,7 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
tagsTemplate := describe.Flags().StringP("tags-view", "t", "*", "Template to render tags")
propertiesTemplate := describe.Flags().StringP("properties-view", "v", "{{.}}", "Template to render properties")

rawOutputFlags, rawOutput := base.RawOutput()
rawOutputFlags, rawOutput := cli.Output()
describe.Flags().AddFlagSet(rawOutputFlags)

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

desc, err := instancePlugin.DescribeInstances(filter, *properties)
if err == nil {

rendered, err := rawOutput(os.Stdout, desc)
if err != nil {
return err
}

if rendered {
return nil
}
if err != nil {
return err
}
return rawOutput(os.Stdout, desc,
func(io.Writer, interface{}) error {

if !*quiet {
if *properties {
fmt.Printf("%-30s\t%-30s\t%-30s\t%-s\n", "ID", "LOGICAL", "TAGS", "PROPERTIES")
if !*quiet {
if *properties {
fmt.Printf("%-30s\t%-30s\t%-30s\t%-s\n", "ID", "LOGICAL", "TAGS", "PROPERTIES")

} else {
fmt.Printf("%-30s\t%-30s\t%-s\n", "ID", "LOGICAL", "TAGS")
} else {
fmt.Printf("%-30s\t%-30s\t%-s\n", "ID", "LOGICAL", "TAGS")
}
}
}
for _, d := range desc {
for _, d := range desc {

logical := " - "
if d.LogicalID != nil {
logical = string(*d.LogicalID)
}
logical := " - "
if d.LogicalID != nil {
logical = string(*d.LogicalID)
}

tagViewBuff := ""
if *tagsTemplate == "*" {
// default -- this is a hack
printTags := []string{}
for k, v := range d.Tags {
printTags = append(printTags, fmt.Sprintf("%s=%s", k, v))
tagViewBuff := ""
if *tagsTemplate == "*" {
// default -- this is a hack
printTags := []string{}
for k, v := range d.Tags {
printTags = append(printTags, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(printTags)
tagViewBuff = strings.Join(printTags, ",")
} else {
tagViewBuff = renderTags(d.Tags, tagsView)
}
sort.Strings(printTags)
tagViewBuff = strings.Join(printTags, ",")
} else {
tagViewBuff = renderTags(d.Tags, tagsView)
}

if *properties {
fmt.Printf("%-30s\t%-30s\t%-30s\t%-s\n", d.ID, logical, tagViewBuff,
renderProperties(d.Properties, propertiesView))
} else {
fmt.Printf("%-30s\t%-30s\t%-s\n", d.ID, logical, tagViewBuff)
if *properties {
fmt.Printf("%-30s\t%-30s\t%-30s\t%-s\n", d.ID, logical, tagViewBuff,
renderProperties(d.Properties, propertiesView))
} else {
fmt.Printf("%-30s\t%-30s\t%-s\n", d.ID, logical, tagViewBuff)
}
}
}
}

return err
return nil
})
}

cmd.AddCommand(
Expand Down
Loading