github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/apply_manifest_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"os"
     5  
     6  	"code.cloudfoundry.org/cli/actor/sharedaction"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    11  	"code.cloudfoundry.org/cli/command/v7/shared"
    12  	"code.cloudfoundry.org/cli/util/manifestparser"
    13  	"code.cloudfoundry.org/clock"
    14  	"github.com/cloudfoundry/bosh-cli/director/template"
    15  )
    16  
    17  //go:generate counterfeiter . ApplyManifestActor
    18  
    19  type ApplyManifestActor interface {
    20  	SetSpaceManifest(spaceGUID string, rawManifest []byte) (v7action.Warnings, error)
    21  }
    22  
    23  type ApplyManifestCommand struct {
    24  	PathToManifest   flag.ManifestPathWithExistenceCheck `short:"f" description:"Path to app manifest"`
    25  	Vars             []template.VarKV                    `long:"var" description:"Variable key value pair for variable substitution, (e.g., name=app1); can specify multiple times"`
    26  	PathsToVarsFiles []flag.PathWithExistenceCheck       `long:"vars-file" description:"Path to a variable substitution file for manifest; can specify multiple times"`
    27  	usage            interface{}                         `usage:"CF_NAME apply-manifest -f APP_MANIFEST_PATH"`
    28  	relatedCommands  interface{}                         `related_commands:"create-app, create-app-manifest, push"`
    29  
    30  	UI              command.UI
    31  	Config          command.Config
    32  	SharedActor     command.SharedActor
    33  	Actor           ApplyManifestActor
    34  	ManifestLocator ManifestLocator
    35  	ManifestParser  ManifestParser
    36  	CWD             string
    37  }
    38  
    39  func (cmd *ApplyManifestCommand) Setup(config command.Config, ui command.UI) error {
    40  
    41  	cmd.UI = ui
    42  	cmd.Config = config
    43  	cmd.SharedActor = sharedaction.NewActor(config)
    44  
    45  	ccClient, _, err := shared.GetNewClientsAndConnectToCF(config, ui, "")
    46  	if err != nil {
    47  		return err
    48  	}
    49  	cmd.Actor = v7action.NewActor(ccClient, config, nil, nil, clock.NewClock())
    50  
    51  	cmd.ManifestLocator = manifestparser.NewLocator()
    52  	cmd.ManifestParser = manifestparser.ManifestParser{}
    53  
    54  	currentDir, err := os.Getwd()
    55  	cmd.CWD = currentDir
    56  
    57  	return err
    58  }
    59  
    60  func (cmd ApplyManifestCommand) Execute(args []string) error {
    61  	err := cmd.SharedActor.CheckTarget(true, true)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	user, err := cmd.Config.CurrentUser()
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	readPath := cmd.CWD
    72  	if cmd.PathToManifest != "" {
    73  		readPath = string(cmd.PathToManifest)
    74  	}
    75  
    76  	pathToManifest, exists, err := cmd.ManifestLocator.Path(readPath)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	if !exists {
    82  		return translatableerror.ManifestFileNotFoundInDirectoryError{PathToManifest: readPath}
    83  	}
    84  
    85  	cmd.UI.DisplayTextWithFlavor("Applying manifest {{.ManifestPath}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    86  		"ManifestPath": pathToManifest,
    87  		"OrgName":      cmd.Config.TargetedOrganization().Name,
    88  		"SpaceName":    cmd.Config.TargetedSpace().Name,
    89  		"Username":     user.Name,
    90  	})
    91  
    92  	var pathsToVarsFiles []string
    93  	for _, varFilePath := range cmd.PathsToVarsFiles {
    94  		pathsToVarsFiles = append(pathsToVarsFiles, string(varFilePath))
    95  	}
    96  
    97  	manifest, err := cmd.ManifestParser.InterpolateAndParse(pathToManifest, pathsToVarsFiles, cmd.Vars)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	rawManifest, err := cmd.ManifestParser.MarshalManifest(manifest)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	warnings, err := cmd.Actor.SetSpaceManifest(cmd.Config.TargetedSpace().GUID, rawManifest)
   108  	cmd.UI.DisplayWarnings(warnings)
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	cmd.UI.DisplayOK()
   114  
   115  	return nil
   116  }