github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/create_app_manifest_command.go (about)

     1  package v2
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os"
     7  
     8  	"code.cloudfoundry.org/cli/actor/sharedaction"
     9  	"code.cloudfoundry.org/cli/actor/v2action"
    10  	"code.cloudfoundry.org/cli/actor/v2v3action"
    11  	"code.cloudfoundry.org/cli/actor/v3action"
    12  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    13  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
    14  	"code.cloudfoundry.org/cli/command"
    15  	"code.cloudfoundry.org/cli/command/flag"
    16  	"code.cloudfoundry.org/cli/command/translatableerror"
    17  	sharedV2 "code.cloudfoundry.org/cli/command/v2/shared"
    18  	sharedV3 "code.cloudfoundry.org/cli/command/v3/shared"
    19  	"code.cloudfoundry.org/cli/util/manifest"
    20  )
    21  
    22  //go:generate counterfeiter . CreateAppManifestActor
    23  
    24  type CreateAppManifestActor interface {
    25  	CreateApplicationManifestByNameAndSpace(appName string, spaceGUID string) (manifest.Application, v2v3action.Warnings, error)
    26  	WriteApplicationManifest(manifestApp manifest.Application, manifestPath string) error
    27  }
    28  
    29  type CreateAppManifestCommand struct {
    30  	RequiredArgs    flag.AppName `positional-args:"yes"`
    31  	FilePath        flag.Path    `short:"p" description:"Specify a path for file creation. If path not specified, manifest file is created in current working directory."`
    32  	usage           interface{}  `usage:"CF_NAME create-app-manifest APP_NAME [-p /path/to/<app-name>_manifest.yml]"`
    33  	relatedCommands interface{}  `related_commands:"apps, push"`
    34  
    35  	UI          command.UI
    36  	Config      command.Config
    37  	SharedActor command.SharedActor
    38  	Actor       CreateAppManifestActor
    39  }
    40  
    41  func (cmd *CreateAppManifestCommand) Setup(config command.Config, ui command.UI) error {
    42  	cmd.UI = ui
    43  	cmd.Config = config
    44  	sharedActor := sharedaction.NewActor(config)
    45  	cmd.SharedActor = sharedActor
    46  
    47  	ccClientV3, uaaClientV3, err := sharedV3.NewClients(config, ui, true)
    48  	if err != nil {
    49  		if v3Err, ok := err.(ccerror.V3UnexpectedResponseError); ok && v3Err.ResponseCode == http.StatusNotFound {
    50  			return translatableerror.MinimumAPIVersionNotMetError{MinimumVersion: ccversion.MinVersionV3}
    51  		}
    52  		return err
    53  	}
    54  	ccClientV2, uaaClientV2, err := sharedV2.NewClients(config, ui, true)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	v2Actor := v2action.NewActor(ccClientV2, uaaClientV2, config)
    59  	v3Actor := v3action.NewActor(ccClientV3, config, sharedActor, uaaClientV3)
    60  	cmd.Actor = v2v3action.NewActor(v2Actor, v3Actor)
    61  
    62  	return nil
    63  }
    64  
    65  func (cmd CreateAppManifestCommand) Execute(args []string) error {
    66  	err := cmd.SharedActor.CheckTarget(true, true)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	user, err := cmd.Config.CurrentUser()
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	cmd.UI.DisplayTextWithFlavor("Creating an app manifest from current settings of app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    77  		"AppName":   cmd.RequiredArgs.AppName,
    78  		"OrgName":   cmd.Config.TargetedOrganization().Name,
    79  		"SpaceName": cmd.Config.TargetedSpace().Name,
    80  		"Username":  user.Name,
    81  	})
    82  
    83  	manifestPath := cmd.FilePath.String()
    84  	if manifestPath == "" {
    85  		manifestPath = fmt.Sprintf(".%s%s_manifest.yml", string(os.PathSeparator), cmd.RequiredArgs.AppName)
    86  	}
    87  	manifestApp, warnings, err := cmd.Actor.CreateApplicationManifestByNameAndSpace(cmd.RequiredArgs.AppName, cmd.Config.TargetedSpace().GUID)
    88  	cmd.UI.DisplayWarnings(warnings)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	err = cmd.Actor.WriteApplicationManifest(manifestApp, manifestPath)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	cmd.UI.DisplayOK()
    98  	cmd.UI.DisplayText("Manifest file created successfully at {{.FilePath}}", map[string]interface{}{
    99  		"FilePath": manifestPath,
   100  	})
   101  
   102  	return nil
   103  }