11package instance
22
33import (
4+ "bytes"
45 "fmt"
56 "os"
67 "sort"
78 "strings"
9+ "text/template"
810
911 "github.com/docker/infrakit/cmd/cli/base"
1012 "github.com/docker/infrakit/pkg/cli"
@@ -150,8 +152,19 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
150152 Short : "Describe all managed instances across all groups, subject to filter" ,
151153 }
152154 tags := describe .Flags ().StringSlice ("tags" , []string {}, "Tags to filter" )
155+ properties := describe .Flags ().BoolP ("properties" , "p" , false , "Also returns current status/ properties" )
156+ propertiesTemplate := describe .Flags ().StringP ("view" , "v" , "{{.}}" , "Template to render properties" )
157+
158+ rawOutputFlags , rawOutput := base .RawOutput ()
159+ describe .Flags ().AddFlagSet (rawOutputFlags )
160+
153161 describe .RunE = func (cmd * cobra.Command , args []string ) error {
154162
163+ view , err := template .New ("describe-instances" ).Parse (* propertiesTemplate )
164+ if err != nil {
165+ return err
166+ }
167+
155168 filter := map [string ]string {}
156169 for _ , t := range * tags {
157170 p := strings .Split (t , "=" )
@@ -162,13 +175,28 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
162175 }
163176 }
164177
165- desc , err := instancePlugin .DescribeInstances (filter )
178+ desc , err := instancePlugin .DescribeInstances (filter , * properties )
166179 if err == nil {
167180
181+ rendered , err := rawOutput (os .Stdout , desc )
182+ if err != nil {
183+ return err
184+ }
185+
186+ if rendered {
187+ return nil
188+ }
189+
168190 if ! * quiet {
169- fmt .Printf ("%-30s\t %-30s\t %-s\n " , "ID" , "LOGICAL" , "TAGS" )
191+ if * properties {
192+ fmt .Printf ("%-30s\t %-30s\t %-30s\t %-s\n " , "ID" , "LOGICAL" , "TAGS" , "PROPERTIES" )
193+
194+ } else {
195+ fmt .Printf ("%-30s\t %-30s\t %-s\n " , "ID" , "LOGICAL" , "TAGS" )
196+ }
170197 }
171198 for _ , d := range desc {
199+
172200 logical := " - "
173201 if d .LogicalID != nil {
174202 logical = string (* d .LogicalID )
@@ -180,7 +208,12 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
180208 }
181209 sort .Strings (printTags )
182210
183- fmt .Printf ("%-30s\t %-30s\t %-s\n " , d .ID , logical , strings .Join (printTags , "," ))
211+ if * properties {
212+ fmt .Printf ("%-30s\t %-30s\t %-30s\t %-s\n " , d .ID , logical , strings .Join (printTags , "," ),
213+ renderProperties (d .Properties , view ))
214+ } else {
215+ fmt .Printf ("%-30s\t %-30s\t %-s\n " , d .ID , logical , strings .Join (printTags , "," ))
216+ }
184217 }
185218 }
186219
@@ -196,3 +229,18 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
196229
197230 return cmd
198231}
232+
233+ func renderProperties (properties * types.Any , view * template.Template ) string {
234+ var v interface {}
235+ err := properties .Decode (& v )
236+ if err != nil {
237+ return err .Error ()
238+ }
239+
240+ buff := new (bytes.Buffer )
241+ err = view .Execute (buff , v )
242+ if err != nil {
243+ return err .Error ()
244+ }
245+ return buff .String ()
246+ }
0 commit comments