1
1
package instance
2
2
3
3
import (
4
+ "bytes"
4
5
"fmt"
5
6
"os"
6
7
"sort"
7
8
"strings"
9
+ "text/template"
8
10
9
11
"github.com/docker/infrakit/cmd/cli/base"
10
12
"github.com/docker/infrakit/pkg/cli"
@@ -150,8 +152,19 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
150
152
Short : "Describe all managed instances across all groups, subject to filter" ,
151
153
}
152
154
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
+
153
161
describe .RunE = func (cmd * cobra.Command , args []string ) error {
154
162
163
+ view , err := template .New ("describe-instances" ).Parse (* propertiesTemplate )
164
+ if err != nil {
165
+ return err
166
+ }
167
+
155
168
filter := map [string ]string {}
156
169
for _ , t := range * tags {
157
170
p := strings .Split (t , "=" )
@@ -162,13 +175,28 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
162
175
}
163
176
}
164
177
165
- desc , err := instancePlugin .DescribeInstances (filter )
178
+ desc , err := instancePlugin .DescribeInstances (filter , * properties )
166
179
if err == nil {
167
180
181
+ rendered , err := rawOutput (os .Stdout , desc )
182
+ if err != nil {
183
+ return err
184
+ }
185
+
186
+ if rendered {
187
+ return nil
188
+ }
189
+
168
190
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
+ }
170
197
}
171
198
for _ , d := range desc {
199
+
172
200
logical := " - "
173
201
if d .LogicalID != nil {
174
202
logical = string (* d .LogicalID )
@@ -180,7 +208,12 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
180
208
}
181
209
sort .Strings (printTags )
182
210
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
+ }
184
217
}
185
218
}
186
219
@@ -196,3 +229,18 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
196
229
197
230
return cmd
198
231
}
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