/
show.go
241 lines (212 loc) · 6.63 KB
/
show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2017 Canonical Ltd.
// Licensed under the GPLv3, see LICENCE file for details.
package cmd
import (
"fmt"
"io"
"strings"
"time"
"github.com/gosuri/uitable"
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/plans-client/api/wireformat"
)
const showPlanDoc = `
show-plan displays detailed information about the plan
Example
show-plan canonical/landscape-default
returns details of the canonical/landscape-default plan.
`
const showPlanPurpose = "show plan details"
var _ cmd.Command = (*ShowCommand)(nil)
// ShowCommand returns plan details
type ShowCommand struct {
baseCommand
out cmd.Output
PlanURL string
ShowContent bool
OnlyDefinition bool
}
// NewShowCommand creates a new ShowCommand.
func NewShowCommand() cmd.Command {
return WrapPlugin(&ShowCommand{})
}
// SetFlags implements Command.SetFlags.
func (c *ShowCommand) SetFlags(f *gnuflag.FlagSet) {
c.baseCommand.ServiceURL = defaultServiceURL()
c.baseCommand.SetFlags(f)
c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
"json": cmd.FormatJson,
"yaml": cmd.FormatYaml,
"tabular": formatTabular,
})
f.BoolVar(&c.ShowContent, "content", false, "show plan definition")
f.BoolVar(&c.OnlyDefinition, "definition", false, "show only the plan definition")
}
// Description returns a one-line description of the command.
func (c *ShowCommand) Description() string {
return showPlanPurpose
}
// Info implements Command.Info.
func (c *ShowCommand) Info() *cmd.Info {
return &cmd.Info{
Name: "show-plan",
Args: "<plan url>",
Purpose: showPlanPurpose,
Doc: showPlanDoc,
}
}
// Init implements Command.Init.
func (c *ShowCommand) Init(args []string) error {
if len(args) < 1 {
return errors.New("missing plan url")
}
planURL, args := args[0], args[1:]
c.PlanURL = planURL
if err := cmd.CheckEmpty(args); err != nil {
return errors.Errorf("unknown command line arguments: " + strings.Join(args, ","))
}
return nil
}
// Run implements Command.Run.
func (c *ShowCommand) Run(ctx *cmd.Context) error {
defer c.Close()
client, err := c.NewClient()
if err != nil {
return errors.Annotate(err, "failed to create an http client")
}
apiClient, err := newClient(c.ServiceURL, client)
if err != nil {
return errors.Annotate(err, "failed to create a plan API client")
}
plan, err := apiClient.GetPlanDetails(c.PlanURL)
if err != nil {
return errors.Annotatef(err, "failed to retrieve plan %v details", c.PlanURL)
}
if c.OnlyDefinition {
fmt.Fprintf(ctx.Stdout, "%v\n%v", plan.Plan.Id, plan.Plan.Definition)
return nil
}
err = c.out.Write(ctx, fromWire(c.ShowContent, plan))
if err != nil {
return errors.Trace(err)
}
return nil
}
func fromWire(showContent bool, plan *wireformat.PlanDetails) *planDetails {
p := planDetails{
ID: plan.Plan.Id,
Created: eventFromWire(plan.Created),
Charms: make([]charmDetails, len(plan.Charms)),
EffectiveTime: plan.Plan.EffectiveTime,
}
if showContent {
p.Definition = plan.Plan.Definition
p.PlanDescription = plan.Plan.PlanDescription
p.PlanPrice = plan.Plan.PlanPrice
}
if plan.Released != nil {
e := eventFromWire(*plan.Released)
p.Released = &e
}
for i, ch := range plan.Charms {
p.Charms[i] = charmDetails{
CharmURL: ch.CharmURL,
Attached: eventFromWire(ch.Attached),
EffectiveSince: ch.EffectiveSince,
Default: ch.Default,
Events: make([]eventDetails, len(ch.Events)),
}
for j, e := range ch.Events {
p.Charms[i].Events[j] = eventFromWire(e)
}
}
return &p
}
type planDetails struct {
ID string `json:"id" yaml:"id"`
Created eventDetails `json:"created" yaml:"created"`
Released *eventDetails `json:"released,omitempty" yaml:"released,omitempty"`
Definition string `json:"definition,omitempty" yaml:"definition,omitempty"`
PlanDescription string `json:"description,omitempty" yaml:"description"`
PlanPrice string `json:"price,omitempty" yaml:"price,omitempty"`
Charms []charmDetails `json:"charms,omitempty" yaml:"charms,omitempty"`
EffectiveTime *time.Time `json:"effective-time,omitempty" yaml:"effective-time,omitempty"`
}
type charmDetails struct {
CharmURL string `json:"charm" yaml:"charm"`
Attached eventDetails `json:"attached" yaml:"attached"`
EffectiveSince *time.Time `json:"effective-since,omitempty" yaml:"effective-since,omitempty"`
Default bool `json:"default" yaml:"default"`
Events []eventDetails `json:"events,omitempty" yaml:"events,omitempty"`
}
type eventDetails struct {
User string `json:"user" yaml:"user"`
Type string `json:"type" yaml:"type"`
Time time.Time `json:"time" yaml:"time"`
}
func eventFromWire(event wireformat.Event) eventDetails {
return eventDetails{
User: event.User,
Type: event.Type,
Time: event.Time,
}
}
func formatTabular(w io.Writer, value interface{}) error {
plan, ok := value.(*planDetails)
if !ok {
return errors.Errorf("expected value of type %T, got %T", plan, value)
}
table := uitable.New()
table.MaxColWidth = 50
table.Wrap = true
for _, col := range []int{1, 2, 3, 4, 5} {
table.RightAlign(col)
}
table.AddRow("PLAN")
table.AddRow(plan.ID)
table.AddRow("", "CREATED BY", "TIME")
table.AddRow("", plan.Created.User, plan.Created.Time)
if plan.Released != nil {
table.AddRow("", "RELEASED BY", "TIME")
table.AddRow("", plan.Released.User, plan.Released.Time)
}
if plan.EffectiveTime != nil {
table.AddRow("", "", "EFFECTIVE")
table.AddRow("", "", plan.EffectiveTime)
}
if plan.PlanDescription != "" {
table.AddRow("", "DESCRIPTION", plan.PlanDescription)
}
if plan.PlanPrice != "" {
table.AddRow("", "PRICE", plan.PlanPrice)
}
if plan.Definition != "" {
table.AddRow("", "DEFINITION", plan.Definition)
}
if len(plan.Charms) > 0 {
table.AddRow("CHARMS")
for _, charm := range plan.Charms {
if charm.EffectiveSince != nil {
table.AddRow("CHARM", "ATTACHED BY", "TIME", "DEFAULT", "EFFECTIVE SINCE")
table.AddRow(charm.CharmURL, charm.Attached.User, charm.Attached.Time, charm.Default, charm.EffectiveSince)
} else {
table.AddRow("CHARM", "ATTACHED BY", "TIME", "DEFAULT", "")
table.AddRow(charm.CharmURL, charm.Attached.User, charm.Attached.Time, charm.Default, "")
}
if len(charm.Events) > 0 {
table.AddRow("", "EVENTS")
table.AddRow("", "", "BY", "TYPE", "TIME")
for _, event := range charm.Events {
table.AddRow("", "", event.User, event.Type, event.Time)
}
}
}
}
_, err := w.Write(table.Bytes())
if err != nil {
return errors.Annotatef(err, "failed to print table")
}
return nil
}