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

     1  package v6
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"code.cloudfoundry.org/cli/actor/sharedaction"
     8  	"code.cloudfoundry.org/cli/actor/v2action"
     9  	"code.cloudfoundry.org/cli/actor/v2v3action"
    10  	"code.cloudfoundry.org/cli/actor/v3action"
    11  	"code.cloudfoundry.org/cli/command"
    12  	"code.cloudfoundry.org/cli/command/flag"
    13  	"code.cloudfoundry.org/cli/command/v6/shared"
    14  	"code.cloudfoundry.org/cli/util/manifest"
    15  )
    16  
    17  //go:generate counterfeiter . CreateAppManifestActor
    18  
    19  type CreateAppManifestActor interface {
    20  	CreateApplicationManifestByNameAndSpace(appName string, spaceGUID string) (manifest.Application, v2v3action.Warnings, error)
    21  	WriteApplicationManifest(manifestApp manifest.Application, manifestPath string) 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  }
    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  	ccClientV3, uaaClientV3, err := shared.NewV3BasedClients(config, ui, true)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	ccClientV2, uaaClientV2, err := shared.GetNewClientsAndConnectToCF(config, ui)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	v2Actor := v2action.NewActor(ccClientV2, uaaClientV2, config)
    51  	v3Actor := v3action.NewActor(ccClientV3, config, sharedActor, uaaClientV3)
    52  	cmd.Actor = v2v3action.NewActor(v2Actor, v3Actor)
    53  
    54  	return nil
    55  }
    56  
    57  func (cmd CreateAppManifestCommand) Execute(args []string) error {
    58  	err := cmd.SharedActor.CheckTarget(true, true)
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	user, err := cmd.Config.CurrentUser()
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    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":   cmd.RequiredArgs.AppName,
    70  		"OrgName":   cmd.Config.TargetedOrganization().Name,
    71  		"SpaceName": cmd.Config.TargetedSpace().Name,
    72  		"Username":  user.Name,
    73  	})
    74  
    75  	manifestPath := cmd.FilePath.String()
    76  	if manifestPath == "" {
    77  		manifestPath = fmt.Sprintf(".%s%s_manifest.yml", string(os.PathSeparator), cmd.RequiredArgs.AppName)
    78  	}
    79  	manifestApp, warnings, err := cmd.Actor.CreateApplicationManifestByNameAndSpace(cmd.RequiredArgs.AppName, cmd.Config.TargetedSpace().GUID)
    80  	cmd.UI.DisplayWarnings(warnings)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	err = cmd.Actor.WriteApplicationManifest(manifestApp, manifestPath)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	cmd.UI.DisplayOK()
    90  	cmd.UI.DisplayText("Manifest file created successfully at {{.FilePath}}", map[string]interface{}{
    91  		"FilePath": manifestPath,
    92  	})
    93  
    94  	return nil
    95  }