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