Skip to content

Commit be171a5

Browse files
authored
Make version flag optional (#3355)
1 parent 885324b commit be171a5

File tree

3 files changed

+179
-18
lines changed

3 files changed

+179
-18
lines changed

command/v7/revision_command.go

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type RevisionCommand struct {
1414
BaseCommand
1515
usage interface{} `usage:"CF_NAME revision APP_NAME [--version VERSION]"`
1616
RequiredArgs flag.AppName `positional-args:"yes"`
17-
Version flag.Revision `long:"version" required:"true" description:"The integer representing the specific revision to show"`
17+
Version flag.Revision `long:"version" description:"The integer representing the specific revision to show"`
1818
relatedCommands interface{} `related_commands:"revisions, rollback"`
1919
}
2020

@@ -30,13 +30,23 @@ func (cmd RevisionCommand) Execute(_ []string) error {
3030
}
3131

3232
appName := cmd.RequiredArgs.AppName
33-
cmd.UI.DisplayTextWithFlavor("Showing revision {{.Version}} for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
34-
"AppName": appName,
35-
"OrgName": cmd.Config.TargetedOrganization().Name,
36-
"SpaceName": cmd.Config.TargetedSpace().Name,
37-
"Username": user.Name,
38-
"Version": cmd.Version.Value,
39-
})
33+
if cmd.Version.Value > 0 {
34+
cmd.UI.DisplayTextWithFlavor("Showing revision {{.Version}} for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
35+
"AppName": appName,
36+
"OrgName": cmd.Config.TargetedOrganization().Name,
37+
"SpaceName": cmd.Config.TargetedSpace().Name,
38+
"Username": user.Name,
39+
"Version": cmd.Version.Value,
40+
})
41+
} else {
42+
cmd.UI.DisplayTextWithFlavor("Showing revisions for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
43+
"AppName": appName,
44+
"OrgName": cmd.Config.TargetedOrganization().Name,
45+
"SpaceName": cmd.Config.TargetedSpace().Name,
46+
"Username": user.Name,
47+
})
48+
}
49+
4050
cmd.UI.DisplayNewline()
4151

4252
app, warnings, err := cmd.Actor.GetApplicationByNameAndSpace(appName, cmd.Config.TargetedSpace().GUID)
@@ -51,16 +61,33 @@ func (cmd RevisionCommand) Execute(_ []string) error {
5161
return err
5262
}
5363

54-
revision, warnings, err := cmd.Actor.GetRevisionByApplicationAndVersion(
55-
app.GUID,
56-
cmd.Version.Value,
57-
)
58-
cmd.UI.DisplayWarnings(warnings)
59-
if err != nil {
60-
return err
64+
if cmd.Version.Value > 0 {
65+
revision, warnings, err := cmd.Actor.GetRevisionByApplicationAndVersion(
66+
app.GUID,
67+
cmd.Version.Value,
68+
)
69+
cmd.UI.DisplayWarnings(warnings)
70+
if err != nil {
71+
return err
72+
}
73+
isDeployed := cmd.revisionDeployed(revision, deployedRevisions)
74+
75+
err = cmd.displayRevisionInfo(revision, isDeployed)
76+
if err != nil {
77+
return err
78+
}
79+
} else {
80+
for _, deployedRevision := range deployedRevisions {
81+
err = cmd.displayRevisionInfo(deployedRevision, true)
82+
if err != nil {
83+
return err
84+
}
85+
}
6186
}
62-
isDeployed := cmd.revisionDeployed(revision, deployedRevisions)
87+
return nil
88+
}
6389

90+
func (cmd RevisionCommand) displayRevisionInfo(revision resources.Revision, isDeployed bool) error {
6491
cmd.displayBasicRevisionInfo(revision, isDeployed)
6592
cmd.UI.DisplayNewline()
6693

@@ -80,13 +107,12 @@ func (cmd RevisionCommand) Execute(_ []string) error {
80107
cmd.displayEnvVarGroup(envVars)
81108
cmd.UI.DisplayNewline()
82109
}
83-
84110
return nil
85111
}
86112

87113
func (cmd RevisionCommand) displayBasicRevisionInfo(revision resources.Revision, isDeployed bool) {
88114
keyValueTable := [][]string{
89-
{"revision:", fmt.Sprintf("%d", cmd.Version.Value)},
115+
{"revision:", fmt.Sprintf("%d", revision.Version)},
90116
{"deployed:", strconv.FormatBool(isDeployed)},
91117
{"description:", revision.Description},
92118
{"deployable:", strconv.FormatBool(revision.Deployable)},

command/v7/revision_command_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,101 @@ var _ = Describe("revision Command", func() {
251251
})
252252
})
253253

254+
When("no revision version is provided", func() {
255+
BeforeEach(func() {
256+
deployedRevisions := []resources.Revision{
257+
{
258+
Version: 3,
259+
GUID: "A68F13F7-7E5E-4411-88E8-1FAC54F73F50",
260+
Description: "On a different note",
261+
CreatedAt: "2020-03-10T17:11:58Z",
262+
Deployable: true,
263+
Droplet: resources.Droplet{
264+
GUID: "droplet-guid",
265+
},
266+
Links: resources.APILinks{
267+
"environment_variables": resources.APILink{
268+
HREF: "revision-environment-variables-link-3",
269+
},
270+
},
271+
Metadata: &resources.Metadata{
272+
Labels: map[string]types.NullString{
273+
"label": types.NewNullString("foo3"),
274+
},
275+
Annotations: map[string]types.NullString{
276+
"annotation": types.NewNullString("foo3"),
277+
},
278+
},
279+
},
280+
{
281+
Version: 2,
282+
GUID: "A89F8259-D32B-491A-ABD6-F100AC42D74C",
283+
Description: "Something else",
284+
CreatedAt: "2020-03-08T12:43:30Z",
285+
Deployable: true,
286+
Droplet: resources.Droplet{
287+
GUID: "droplet-guid2",
288+
},
289+
Metadata: &resources.Metadata{},
290+
},
291+
}
292+
fakeActor.GetApplicationByNameAndSpaceReturns(resources.Application{GUID: "app-guid"}, nil, nil)
293+
fakeActor.GetApplicationRevisionsDeployedReturns(deployedRevisions, nil, nil)
294+
295+
environmentVariableGroup := v7action.EnvironmentVariableGroup{
296+
"foo": *types.NewFilteredString("bar3"),
297+
}
298+
fakeActor.GetEnvironmentVariableGroupByRevisionReturns(
299+
environmentVariableGroup,
300+
true,
301+
nil,
302+
nil,
303+
)
304+
cmd.Version = flag.Revision{NullInt: types.NullInt{Value: 0, IsSet: false}}
305+
})
306+
307+
It("displays all deployed revisions", func() {
308+
Expect(executeErr).ToNot(HaveOccurred())
309+
310+
Expect(testUI.Out).To(Say(`Showing revisions for app some-app in org some-org / space some-space as banana...`))
311+
Expect(testUI.Out).To(Say(`revision: 3`))
312+
Expect(testUI.Out).To(Say(`deployed: true`))
313+
Expect(testUI.Out).To(Say(`description: On a different note`))
314+
Expect(testUI.Out).To(Say(`deployable: true`))
315+
Expect(testUI.Out).To(Say(`revision GUID: A68F13F7-7E5E-4411-88E8-1FAC54F73F50`))
316+
Expect(testUI.Out).To(Say(`droplet GUID: droplet-guid`))
317+
Expect(testUI.Out).To(Say(`created on: 2020-03-10T17:11:58Z`))
318+
319+
Expect(testUI.Out).To(Say(`labels:`))
320+
Expect(testUI.Out).To(Say(`label: foo3`))
321+
322+
Expect(testUI.Out).To(Say(`annotations:`))
323+
Expect(testUI.Out).To(Say(`annotation: foo3`))
324+
325+
Expect(testUI.Out).To(Say(`application environment variables:`))
326+
Expect(testUI.Out).To(Say(`foo: bar3`))
327+
328+
Expect(testUI.Out).To(Say(`revision: 2`))
329+
Expect(testUI.Out).To(Say(`deployed: true`))
330+
Expect(testUI.Out).To(Say(`description: Something else`))
331+
Expect(testUI.Out).To(Say(`deployable: true`))
332+
Expect(testUI.Out).To(Say(`revision GUID: A89F8259-D32B-491A-ABD6-F100AC42D74C`))
333+
Expect(testUI.Out).To(Say(`droplet GUID: droplet-guid2`))
334+
Expect(testUI.Out).To(Say(`created on: 2020-03-08T12:43:30Z`))
335+
336+
Expect(testUI.Out).To(Say(`labels:`))
337+
Expect(testUI.Out).To(Say(`annotations:`))
338+
Expect(testUI.Out).To(Say(`application environment variables:`))
339+
Expect(testUI.Out).To(Say(`foo: bar3`))
340+
})
341+
})
342+
254343
When("there are no revisions available", func() {
255344
BeforeEach(func() {
256345
revision := resources.Revision{
257346
Version: 120,
258347
}
348+
cmd.Version = flag.Revision{NullInt: types.NullInt{Value: 120, IsSet: true}}
259349
fakeActor.GetRevisionByApplicationAndVersionReturns(
260350
revision,
261351
nil,

integration/v7/isolated/revision_command_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,50 @@ var _ = Describe("revision command", func() {
161161
Expect(session).Should(Say(`foo: bar1`))
162162
})
163163
})
164+
165+
When("the revision version is not mentioned", func() {
166+
BeforeEach(func() {
167+
helpers.WithHelloWorldApp(func(appDir string) {
168+
Eventually(helpers.CF("create-app", appName)).Should(Exit(0))
169+
Eventually(helpers.CF("push", appName, "-p", appDir)).Should(Exit(0))
170+
Eventually(helpers.CF("push", appName, "-p", appDir)).Should(Exit(0))
171+
Eventually(helpers.CF("push", appName, "-p", appDir, "--strategy", "canary")).Should(Exit(0))
172+
})
173+
})
174+
175+
It("shows all the deployed revisions", func() {
176+
session := helpers.CF("revision", appName)
177+
Eventually(session).Should(Exit(0))
178+
179+
Expect(session).Should(Say(
180+
fmt.Sprintf("Showing revisions for app %s in org %s / space %s as %s...", appName, orgName, spaceName, username),
181+
))
182+
Expect(session).Should(Say(`revision: 2`))
183+
Expect(session).Should(Say(`deployed: true`))
184+
Expect(session).Should(Say(`description: New droplet deployed`))
185+
Expect(session).Should(Say(`deployable: true`))
186+
Expect(session).Should(Say(`revision GUID: \S+\n`))
187+
Expect(session).Should(Say(`droplet GUID: \S+\n`))
188+
Expect(session).Should(Say(`created on: \S+\n`))
189+
190+
Expect(session).Should(Say(`labels:`))
191+
Expect(session).Should(Say(`annotations:`))
192+
Expect(session).Should(Say(`application environment variables:`))
193+
194+
Expect(session).Should(Say(`revision: 3`))
195+
Expect(session).Should(Say(`deployed: true`))
196+
Expect(session).Should(Say(`description: New droplet deployed`))
197+
Expect(session).Should(Say(`deployable: true`))
198+
Expect(session).Should(Say(`revision GUID: \S+\n`))
199+
Expect(session).Should(Say(`droplet GUID: \S+\n`))
200+
Expect(session).Should(Say(`created on: \S+\n`))
201+
202+
Expect(session).Should(Say(`labels:`))
203+
Expect(session).Should(Say(`annotations:`))
204+
Expect(session).Should(Say(`application environment variables:`))
205+
206+
Expect(session).ShouldNot(Say(`revision: 1`))
207+
})
208+
})
164209
})
165210
})

0 commit comments

Comments
 (0)