Skip to content

Commit

Permalink
Make version flag optional
Browse files Browse the repository at this point in the history
  • Loading branch information
gururajsh committed Jan 9, 2025
1 parent 61197b9 commit b6e3ca5
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 18 deletions.
62 changes: 44 additions & 18 deletions command/v7/revision_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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)
Expand All @@ -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()

Expand All @@ -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)},
Expand Down
90 changes: 90 additions & 0 deletions command/v7/revision_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
45 changes: 45 additions & 0 deletions integration/v7/isolated/revision_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`))
})
})
})
})

0 comments on commit b6e3ca5

Please sign in to comment.