github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/revisions_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
     8  	"github.com/LukasHeimann/cloudfoundrycli/v8/command"
     9  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/flag"
    10  	"github.com/LukasHeimann/cloudfoundrycli/v8/resources"
    11  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/ui"
    12  )
    13  
    14  //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . RevisionsActor
    15  
    16  type RevisionsActor interface {
    17  	GetRevisionsByApplicationNameAndSpace(appName string, spaceGUID string) ([]resources.Revision, v7action.Warnings, error)
    18  }
    19  
    20  type RevisionsCommand struct {
    21  	RequiredArgs flag.EnvironmentArgs `positional-args:"yes"`
    22  	usage        interface{}          `usage:"CF_NAME revisions APP_NAME"`
    23  
    24  	BaseCommand
    25  	relatedCommands interface{} `related_commands:"rollback"`
    26  }
    27  
    28  func (cmd RevisionsCommand) Execute(_ []string) error {
    29  	cmd.UI.DisplayWarning(command.ExperimentalWarning)
    30  	cmd.UI.DisplayNewline()
    31  
    32  	err := cmd.SharedActor.CheckTarget(true, true)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	user, err := cmd.Actor.GetCurrentUser()
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	appName := cmd.RequiredArgs.AppName
    43  	cmd.UI.DisplayTextWithFlavor("Getting revisions for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    44  		"AppName":   appName,
    45  		"OrgName":   cmd.Config.TargetedOrganization().Name,
    46  		"SpaceName": cmd.Config.TargetedSpace().Name,
    47  		"Username":  user.Name,
    48  	})
    49  
    50  	app, warnings, err := cmd.Actor.GetApplicationByNameAndSpace(appName, cmd.Config.TargetedSpace().GUID)
    51  	cmd.UI.DisplayWarnings(warnings)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	appGUID := app.GUID
    57  	revisionsFeature, warnings, err := cmd.Actor.GetAppFeature(appGUID, "revisions")
    58  	cmd.UI.DisplayWarnings(warnings)
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	if !revisionsFeature.Enabled {
    64  		cmd.UI.DisplayWarning("Warning: Revisions for app '{{.AppName}}' are disabled. Updates to the app will not create new revisions.",
    65  			map[string]interface{}{
    66  				"AppName": appName,
    67  			})
    68  	}
    69  
    70  	if app.Stopped() {
    71  		cmd.UI.DisplayNewline()
    72  		cmd.UI.DisplayText(fmt.Sprintf("Info: this app is in a stopped state. It is not possible to determine which revision is currently deployed."))
    73  	}
    74  
    75  	cmd.UI.DisplayNewline()
    76  
    77  	revisions, warnings, err := cmd.Actor.GetRevisionsByApplicationNameAndSpace(
    78  		appName,
    79  		cmd.Config.TargetedSpace().GUID,
    80  	)
    81  	cmd.UI.DisplayWarnings(warnings)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	if len(revisions) == 0 {
    87  		cmd.UI.DisplayText("No revisions found")
    88  		return nil
    89  	}
    90  
    91  	revisionsDeployed, warnings, err := cmd.Actor.GetApplicationRevisionsDeployed(appGUID)
    92  	cmd.UI.DisplayWarnings(warnings)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	table := [][]string{{
    98  		"revision",
    99  		"description",
   100  		"deployable",
   101  		"revision guid",
   102  		"created at",
   103  	}}
   104  
   105  	for _, revision := range revisions {
   106  		table = append(table,
   107  			[]string{decorateVersionWithDeployed(revision, revisionsDeployed),
   108  				revision.Description,
   109  				strconv.FormatBool(revision.Deployable),
   110  				revision.GUID,
   111  				revision.CreatedAt,
   112  			})
   113  	}
   114  
   115  	if len(revisionsDeployed) > 1 {
   116  		cmd.UI.DisplayText("Info: this app is in the middle of a rolling deployment. More than one revision is deployed.")
   117  		cmd.UI.DisplayNewline()
   118  	}
   119  
   120  	cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
   121  
   122  	return nil
   123  }
   124  
   125  func decorateVersionWithDeployed(revision resources.Revision, deployedRevisions []resources.Revision) string {
   126  	for _, revDeployed := range deployedRevisions {
   127  		if revDeployed.GUID == revision.GUID {
   128  			return strconv.Itoa(revision.Version) + "(deployed)"
   129  		}
   130  	}
   131  	return strconv.Itoa(revision.Version)
   132  }