github.com/arunkumar7540/cli@v6.45.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/actor/sharedaction"
    10  	"code.cloudfoundry.org/cli/actor/v7action"
    11  	"code.cloudfoundry.org/cli/command"
    12  	"code.cloudfoundry.org/cli/command/flag"
    13  	"code.cloudfoundry.org/cli/command/translatableerror"
    14  	"code.cloudfoundry.org/cli/command/v7/shared"
    15  )
    16  
    17  //go:generate counterfeiter . CreateAppManifestActor
    18  
    19  type CreateAppManifestActor interface {
    20  	GetRawApplicationManifestByNameAndSpace(appName string, spaceGUID string) ([]byte, v7action.Warnings, error)
    21  }
    22  
    23  type CreateAppManifestCommand struct {
    24  	RequiredArgs    flag.AppName `positional-args:"yes"`
    25  	FilePath        flag.Path    `short:"p" description:"Specify a path for file creation. If path not specified, manifest file is created in current working directory."`
    26  	usage           interface{}  `usage:"CF_NAME create-app-manifest APP_NAME [-p /path/to/<app-name>_manifest.yml]"`
    27  	relatedCommands interface{}  `related_commands:"apps, push"`
    28  
    29  	UI          command.UI
    30  	Config      command.Config
    31  	SharedActor command.SharedActor
    32  	Actor       CreateAppManifestActor
    33  	PWD         string
    34  }
    35  
    36  func (cmd *CreateAppManifestCommand) Setup(config command.Config, ui command.UI) error {
    37  	cmd.UI = ui
    38  	cmd.Config = config
    39  	sharedActor := sharedaction.NewActor(config)
    40  	cmd.SharedActor = sharedActor
    41  
    42  	ccClient, uaaClient, err := shared.NewClients(config, ui, true, "")
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	cmd.Actor = v7action.NewActor(ccClient, config, sharedActor, uaaClient)
    48  
    49  	currentDir, err := os.Getwd()
    50  	cmd.PWD = currentDir
    51  
    52  	return err
    53  }
    54  
    55  func (cmd CreateAppManifestCommand) Execute(args []string) error {
    56  	err := cmd.SharedActor.CheckTarget(true, true)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	user, err := cmd.Config.CurrentUser()
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	appName := cmd.RequiredArgs.AppName
    67  	cmd.UI.DisplayTextWithFlavor("Creating an app manifest from current settings of app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    68  		"AppName":   appName,
    69  		"OrgName":   cmd.Config.TargetedOrganization().Name,
    70  		"SpaceName": cmd.Config.TargetedSpace().Name,
    71  		"Username":  user.Name,
    72  	})
    73  
    74  	spaceGUID := cmd.Config.TargetedSpace().GUID
    75  	manifestBytes, warnings, err := cmd.Actor.GetRawApplicationManifestByNameAndSpace(appName, spaceGUID)
    76  	cmd.UI.DisplayWarnings(warnings)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	var pathToYAMLFile string
    82  	if len(cmd.FilePath) > 0 {
    83  		pathToYAMLFile = cmd.FilePath.String()
    84  	} else {
    85  		pathToYAMLFile = filepath.Join(cmd.PWD, fmt.Sprintf("%s_manifest.yml", appName))
    86  	}
    87  
    88  	err = ioutil.WriteFile(pathToYAMLFile, manifestBytes, 0666)
    89  	if err != nil {
    90  		return translatableerror.ManifestCreationError{Err: err}
    91  	}
    92  
    93  	cmd.UI.DisplayText("Manifest file created successfully at {{.FilePath}}", map[string]interface{}{
    94  		"FilePath": pathToYAMLFile,
    95  	})
    96  	cmd.UI.DisplayOK()
    97  
    98  	return nil
    99  }