github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v6/v3_create_app_command.go (about) 1 package v6 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/actionerror" 5 "code.cloudfoundry.org/cli/actor/sharedaction" 6 "code.cloudfoundry.org/cli/actor/v3action" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 9 "code.cloudfoundry.org/cli/command" 10 "code.cloudfoundry.org/cli/command/flag" 11 "code.cloudfoundry.org/cli/command/v6/shared" 12 ) 13 14 //go:generate counterfeiter . V3CreateAppActor 15 16 type V3CreateAppActor interface { 17 CloudControllerAPIVersion() string 18 CreateApplicationInSpace(app v3action.Application, spaceGUID string) (v3action.Application, v3action.Warnings, error) 19 } 20 21 type V3CreateAppCommand struct { 22 RequiredArgs flag.AppName `positional-args:"yes"` 23 AppType flag.AppType `long:"app-type" choice:"buildpack" choice:"docker" description:"App lifecycle type to stage and run the app" default:"buildpack"` 24 usage interface{} `usage:"CF_NAME v3-create-app APP_NAME [--app-type (buildpack | docker)]"` 25 26 UI command.UI 27 Config command.Config 28 SharedActor command.SharedActor 29 Actor V3CreateAppActor 30 } 31 32 func (cmd *V3CreateAppCommand) Setup(config command.Config, ui command.UI) error { 33 cmd.UI = ui 34 cmd.Config = config 35 cmd.SharedActor = sharedaction.NewActor(config) 36 37 client, _, err := shared.NewV3BasedClients(config, ui, true, "") 38 if err != nil { 39 return err 40 } 41 cmd.Actor = v3action.NewActor(client, config, nil, nil) 42 43 return nil 44 } 45 46 func (cmd V3CreateAppCommand) Execute(args []string) error { 47 cmd.UI.DisplayWarning(command.ExperimentalWarning) 48 49 err := command.MinimumCCAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionApplicationFlowV3) 50 if err != nil { 51 return err 52 } 53 54 err = cmd.SharedActor.CheckTarget(true, true) 55 if err != nil { 56 return err 57 } 58 59 user, err := cmd.Config.CurrentUser() 60 if err != nil { 61 return err 62 } 63 64 cmd.UI.DisplayTextWithFlavor("Creating V3 app {{.AppName}} in org {{.CurrentOrg}} / space {{.CurrentSpace}} as {{.CurrentUser}}...", map[string]interface{}{ 65 "AppName": cmd.RequiredArgs.AppName, 66 "CurrentSpace": cmd.Config.TargetedSpace().Name, 67 "CurrentOrg": cmd.Config.TargetedOrganization().Name, 68 "CurrentUser": user.Name, 69 }) 70 71 _, warnings, err := cmd.Actor.CreateApplicationInSpace( 72 v3action.Application{ 73 Name: cmd.RequiredArgs.AppName, 74 LifecycleType: constant.AppLifecycleType(cmd.AppType), 75 }, 76 cmd.Config.TargetedSpace().GUID, 77 ) 78 cmd.UI.DisplayWarnings(warnings) 79 if err != nil { 80 switch err.(type) { 81 case actionerror.ApplicationAlreadyExistsError: 82 cmd.UI.DisplayWarning("App {{.AppName}} already exists", map[string]interface{}{ 83 "AppName": cmd.RequiredArgs.AppName, 84 }) 85 default: 86 return err 87 } 88 } 89 90 cmd.UI.DisplayOK() 91 92 return nil 93 }