code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/create_app_manifest_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"code.cloudfoundry.org/cli/command"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	"code.cloudfoundry.org/cli/command/translatableerror"
    12  )
    13  
    14  type CreateAppManifestCommand struct {
    15  	BaseCommand
    16  
    17  	RequiredArgs    flag.AppName `positional-args:"yes"`
    18  	FilePath        flag.Path    `short:"p" description:"Specify a path for file creation. If path not specified, manifest file is created in current working directory."`
    19  	usage           interface{}  `usage:"CF_NAME create-app-manifest APP_NAME [-p /path/to/<app-name>_manifest.yml]"`
    20  	relatedCommands interface{}  `related_commands:"apps, push"`
    21  
    22  	PWD string
    23  }
    24  
    25  func (cmd *CreateAppManifestCommand) Setup(config command.Config, ui command.UI) error {
    26  	err := cmd.BaseCommand.Setup(config, ui)
    27  	if err != nil {
    28  		return err
    29  	}
    30  	currentDir, err := os.Getwd()
    31  	cmd.PWD = currentDir
    32  
    33  	return err
    34  }
    35  
    36  func (cmd CreateAppManifestCommand) Execute(args []string) error {
    37  	err := cmd.SharedActor.CheckTarget(true, true)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	user, err := cmd.Config.CurrentUser()
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	appName := cmd.RequiredArgs.AppName
    48  	cmd.UI.DisplayTextWithFlavor("Creating an app manifest from current settings of app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    49  		"AppName":   appName,
    50  		"OrgName":   cmd.Config.TargetedOrganization().Name,
    51  		"SpaceName": cmd.Config.TargetedSpace().Name,
    52  		"Username":  user.Name,
    53  	})
    54  
    55  	spaceGUID := cmd.Config.TargetedSpace().GUID
    56  	manifestBytes, warnings, err := cmd.Actor.GetRawApplicationManifestByNameAndSpace(appName, spaceGUID)
    57  	cmd.UI.DisplayWarnings(warnings)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	var pathToYAMLFile string
    63  	if len(cmd.FilePath) > 0 {
    64  		pathToYAMLFile = cmd.FilePath.String()
    65  	} else {
    66  		pathToYAMLFile = filepath.Join(cmd.PWD, fmt.Sprintf("%s_manifest.yml", appName))
    67  	}
    68  
    69  	err = ioutil.WriteFile(pathToYAMLFile, manifestBytes, 0666)
    70  	if err != nil {
    71  		return translatableerror.ManifestCreationError{Err: err}
    72  	}
    73  
    74  	cmd.UI.DisplayText("Manifest file created successfully at {{.FilePath}}", map[string]interface{}{
    75  		"FilePath": pathToYAMLFile,
    76  	})
    77  	cmd.UI.DisplayOK()
    78  
    79  	return nil
    80  }