From 350848d4c9594c2d2f924d55a16df9ab7d9685f8 Mon Sep 17 00:00:00 2001 From: Shwetha Gururaj Date: Thu, 9 Jan 2025 14:32:03 -0500 Subject: [PATCH] Make version flag optional --- command/v7/revision_command.go | 62 +++++++++---- command/v7/revision_command_test.go | 90 +++++++++++++++++++ .../v7/isolated/revision_command_test.go | 45 ++++++++++ 3 files changed, 179 insertions(+), 18 deletions(-) diff --git a/command/v7/revision_command.go b/command/v7/revision_command.go index c9e23af546..0d32e84eac 100644 --- a/command/v7/revision_command.go +++ b/command/v7/revision_command.go @@ -14,7 +14,7 @@ type RevisionCommand struct { BaseCommand usage interface{} `usage:"CF_NAME revision APP_NAME [--version VERSION]"` RequiredArgs flag.AppName `positional-args:"yes"` - Version flag.Revision `long:"version" required:"true" description:"The integer representing the specific revision to show"` + Version flag.Revision `long:"version" description:"The integer representing the specific revision to show"` relatedCommands interface{} `related_commands:"revisions, rollback"` } @@ -30,13 +30,23 @@ func (cmd RevisionCommand) Execute(_ []string) error { } appName := cmd.RequiredArgs.AppName - cmd.UI.DisplayTextWithFlavor("Showing revision {{.Version}} for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{ - "AppName": appName, - "OrgName": cmd.Config.TargetedOrganization().Name, - "SpaceName": cmd.Config.TargetedSpace().Name, - "Username": user.Name, - "Version": cmd.Version.Value, - }) + if cmd.Version.Value > 0 { + cmd.UI.DisplayTextWithFlavor("Showing revision {{.Version}} for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{ + "AppName": appName, + "OrgName": cmd.Config.TargetedOrganization().Name, + "SpaceName": cmd.Config.TargetedSpace().Name, + "Username": user.Name, + "Version": cmd.Version.Value, + }) + } else { + cmd.UI.DisplayTextWithFlavor("Showing revisions for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{ + "AppName": appName, + "OrgName": cmd.Config.TargetedOrganization().Name, + "SpaceName": cmd.Config.TargetedSpace().Name, + "Username": user.Name, + }) + } + cmd.UI.DisplayNewline() app, warnings, err := cmd.Actor.GetApplicationByNameAndSpace(appName, cmd.Config.TargetedSpace().GUID) @@ -51,16 +61,33 @@ func (cmd RevisionCommand) Execute(_ []string) error { return err } - revision, warnings, err := cmd.Actor.GetRevisionByApplicationAndVersion( - app.GUID, - cmd.Version.Value, - ) - cmd.UI.DisplayWarnings(warnings) - if err != nil { - return err + if cmd.Version.Value > 0 { + revision, warnings, err := cmd.Actor.GetRevisionByApplicationAndVersion( + app.GUID, + cmd.Version.Value, + ) + cmd.UI.DisplayWarnings(warnings) + if err != nil { + return err + } + isDeployed := cmd.revisionDeployed(revision, deployedRevisions) + + err = cmd.displayRevisionInfo(revision, isDeployed) + if err != nil { + return err + } + } else { + for _, deployedRevision := range deployedRevisions { + err = cmd.displayRevisionInfo(deployedRevision, true) + if err != nil { + return err + } + } } - isDeployed := cmd.revisionDeployed(revision, deployedRevisions) + return nil +} +func (cmd RevisionCommand) displayRevisionInfo(revision resources.Revision, isDeployed bool) error { cmd.displayBasicRevisionInfo(revision, isDeployed) cmd.UI.DisplayNewline() @@ -80,13 +107,12 @@ func (cmd RevisionCommand) Execute(_ []string) error { cmd.displayEnvVarGroup(envVars) cmd.UI.DisplayNewline() } - return nil } func (cmd RevisionCommand) displayBasicRevisionInfo(revision resources.Revision, isDeployed bool) { keyValueTable := [][]string{ - {"revision:", fmt.Sprintf("%d", cmd.Version.Value)}, + {"revision:", fmt.Sprintf("%d", revision.Version)}, {"deployed:", strconv.FormatBool(isDeployed)}, {"description:", revision.Description}, {"deployable:", strconv.FormatBool(revision.Deployable)}, diff --git a/command/v7/revision_command_test.go b/command/v7/revision_command_test.go index 659ec9e8ad..5b7cb03a00 100644 --- a/command/v7/revision_command_test.go +++ b/command/v7/revision_command_test.go @@ -251,11 +251,101 @@ var _ = Describe("revision Command", func() { }) }) + When("no revision version is provided", func() { + BeforeEach(func() { + deployedRevisions := []resources.Revision{ + { + Version: 3, + GUID: "A68F13F7-7E5E-4411-88E8-1FAC54F73F50", + Description: "On a different note", + CreatedAt: "2020-03-10T17:11:58Z", + Deployable: true, + Droplet: resources.Droplet{ + GUID: "droplet-guid", + }, + Links: resources.APILinks{ + "environment_variables": resources.APILink{ + HREF: "revision-environment-variables-link-3", + }, + }, + Metadata: &resources.Metadata{ + Labels: map[string]types.NullString{ + "label": types.NewNullString("foo3"), + }, + Annotations: map[string]types.NullString{ + "annotation": types.NewNullString("foo3"), + }, + }, + }, + { + Version: 2, + GUID: "A89F8259-D32B-491A-ABD6-F100AC42D74C", + Description: "Something else", + CreatedAt: "2020-03-08T12:43:30Z", + Deployable: true, + Droplet: resources.Droplet{ + GUID: "droplet-guid2", + }, + Metadata: &resources.Metadata{}, + }, + } + fakeActor.GetApplicationByNameAndSpaceReturns(resources.Application{GUID: "app-guid"}, nil, nil) + fakeActor.GetApplicationRevisionsDeployedReturns(deployedRevisions, nil, nil) + + environmentVariableGroup := v7action.EnvironmentVariableGroup{ + "foo": *types.NewFilteredString("bar3"), + } + fakeActor.GetEnvironmentVariableGroupByRevisionReturns( + environmentVariableGroup, + true, + nil, + nil, + ) + cmd.Version = flag.Revision{NullInt: types.NullInt{Value: 0, IsSet: false}} + }) + + It("displays all deployed revisions", func() { + Expect(executeErr).ToNot(HaveOccurred()) + + Expect(testUI.Out).To(Say(`Showing revisions for app some-app in org some-org / space some-space as banana...`)) + Expect(testUI.Out).To(Say(`revision: 3`)) + Expect(testUI.Out).To(Say(`deployed: true`)) + Expect(testUI.Out).To(Say(`description: On a different note`)) + Expect(testUI.Out).To(Say(`deployable: true`)) + Expect(testUI.Out).To(Say(`revision GUID: A68F13F7-7E5E-4411-88E8-1FAC54F73F50`)) + Expect(testUI.Out).To(Say(`droplet GUID: droplet-guid`)) + Expect(testUI.Out).To(Say(`created on: 2020-03-10T17:11:58Z`)) + + Expect(testUI.Out).To(Say(`labels:`)) + Expect(testUI.Out).To(Say(`label: foo3`)) + + Expect(testUI.Out).To(Say(`annotations:`)) + Expect(testUI.Out).To(Say(`annotation: foo3`)) + + Expect(testUI.Out).To(Say(`application environment variables:`)) + Expect(testUI.Out).To(Say(`foo: bar3`)) + + Expect(testUI.Out).To(Say(`revision: 2`)) + Expect(testUI.Out).To(Say(`deployed: true`)) + Expect(testUI.Out).To(Say(`description: Something else`)) + Expect(testUI.Out).To(Say(`deployable: true`)) + Expect(testUI.Out).To(Say(`revision GUID: A89F8259-D32B-491A-ABD6-F100AC42D74C`)) + Expect(testUI.Out).To(Say(`droplet GUID: droplet-guid2`)) + Expect(testUI.Out).To(Say(`created on: 2020-03-08T12:43:30Z`)) + + Expect(testUI.Out).To(Say(`labels:`)) + Expect(testUI.Out).To(Say(`annotations:`)) + Expect(testUI.Out).To(Say(`application environment variables:`)) + Expect(testUI.Out).To(Say(`foo: bar3`)) + }) + }) + When("there are no revisions available", func() { BeforeEach(func() { revision := resources.Revision{ Version: 120, } + cmd.Version = flag.Revision{NullInt: types.NullInt{Value: 120, IsSet: true}} fakeActor.GetRevisionByApplicationAndVersionReturns( revision, nil, diff --git a/integration/v7/isolated/revision_command_test.go b/integration/v7/isolated/revision_command_test.go index 87b5c19dc4..1be53f0f1d 100644 --- a/integration/v7/isolated/revision_command_test.go +++ b/integration/v7/isolated/revision_command_test.go @@ -161,5 +161,50 @@ var _ = Describe("revision command", func() { Expect(session).Should(Say(`foo: bar1`)) }) }) + + When("the revision version is not mentioned", func() { + BeforeEach(func() { + helpers.WithHelloWorldApp(func(appDir string) { + Eventually(helpers.CF("create-app", appName)).Should(Exit(0)) + Eventually(helpers.CF("push", appName, "-p", appDir)).Should(Exit(0)) + Eventually(helpers.CF("push", appName, "-p", appDir)).Should(Exit(0)) + Eventually(helpers.CF("push", appName, "-p", appDir, "--strategy", "canary")).Should(Exit(0)) + }) + }) + + It("shows all the deployed revisions", func() { + session := helpers.CF("revision", appName) + Eventually(session).Should(Exit(0)) + + Expect(session).Should(Say( + fmt.Sprintf("Showing revisions for app %s in org %s / space %s as %s...", appName, orgName, spaceName, username), + )) + Expect(session).Should(Say(`revision: 2`)) + Expect(session).Should(Say(`deployed: true`)) + Expect(session).Should(Say(`description: New droplet deployed`)) + Expect(session).Should(Say(`deployable: true`)) + Expect(session).Should(Say(`revision GUID: \S+\n`)) + Expect(session).Should(Say(`droplet GUID: \S+\n`)) + Expect(session).Should(Say(`created on: \S+\n`)) + + Expect(session).Should(Say(`labels:`)) + Expect(session).Should(Say(`annotations:`)) + Expect(session).Should(Say(`application environment variables:`)) + + Expect(session).Should(Say(`revision: 3`)) + Expect(session).Should(Say(`deployed: true`)) + Expect(session).Should(Say(`description: New droplet deployed`)) + Expect(session).Should(Say(`deployable: true`)) + Expect(session).Should(Say(`revision GUID: \S+\n`)) + Expect(session).Should(Say(`droplet GUID: \S+\n`)) + Expect(session).Should(Say(`created on: \S+\n`)) + + Expect(session).Should(Say(`labels:`)) + Expect(session).Should(Say(`annotations:`)) + Expect(session).Should(Say(`application environment variables:`)) + + Expect(session).ShouldNot(Say(`revision: 1`)) + }) + }) }) })