github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/command/v3/run_task_command.go (about)

     1  package v3
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/sharedaction"
     5  	"code.cloudfoundry.org/cli/actor/v3action"
     6  	"code.cloudfoundry.org/cli/command"
     7  	"code.cloudfoundry.org/cli/command/flag"
     8  	"code.cloudfoundry.org/cli/command/v3/shared"
     9  )
    10  
    11  //go:generate counterfeiter . RunTaskActor
    12  
    13  type RunTaskActor interface {
    14  	GetApplicationByNameAndSpace(appName string, spaceGUID string) (v3action.Application, v3action.Warnings, error)
    15  	RunTask(appGUID string, command string, name string, memory uint64, disk uint64) (v3action.Task, v3action.Warnings, error)
    16  	CloudControllerAPIVersion() string
    17  }
    18  
    19  type RunTaskCommand struct {
    20  	RequiredArgs    flag.RunTaskArgs `positional-args:"yes"`
    21  	Disk            flag.Megabytes   `short:"k" description:"Disk limit (e.g. 256M, 1024M, 1G)"`
    22  	Memory          flag.Megabytes   `short:"m" description:"Memory limit (e.g. 256M, 1024M, 1G)"`
    23  	Name            string           `long:"name" description:"Name to give the task (generated if omitted)"`
    24  	usage           interface{}      `usage:"CF_NAME run-task APP_NAME COMMAND [-k DISK] [-m MEMORY] [--name TASK_NAME]\n\nTIP:\n   Use 'cf logs' to display the logs of the app and all its tasks. If your task name is unique, grep this command's output for the task name to view task-specific logs.\n\nEXAMPLES:\n   CF_NAME run-task my-app \"bundle exec rake db:migrate\" --name migrate"`
    25  	relatedCommands interface{}      `related_commands:"logs, tasks, terminate-task"`
    26  
    27  	UI          command.UI
    28  	Config      command.Config
    29  	SharedActor command.SharedActor
    30  	Actor       RunTaskActor
    31  }
    32  
    33  func (cmd *RunTaskCommand) Setup(config command.Config, ui command.UI) error {
    34  	cmd.UI = ui
    35  	cmd.Config = config
    36  	cmd.SharedActor = sharedaction.NewActor()
    37  
    38  	client, err := shared.NewClients(config, ui)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	cmd.Actor = v3action.NewActor(client)
    43  
    44  	return nil
    45  }
    46  
    47  func (cmd RunTaskCommand) Execute(args []string) error {
    48  	err := command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), "3.0.0")
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	err = cmd.SharedActor.CheckTarget(cmd.Config, true, true)
    54  	if err != nil {
    55  		return shared.HandleError(err)
    56  	}
    57  
    58  	space := cmd.Config.TargetedSpace()
    59  
    60  	user, err := cmd.Config.CurrentUser()
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	application, warnings, err := cmd.Actor.GetApplicationByNameAndSpace(cmd.RequiredArgs.AppName, space.GUID)
    66  	cmd.UI.DisplayWarnings(warnings)
    67  	if err != nil {
    68  		return shared.HandleError(err)
    69  	}
    70  
    71  	cmd.UI.DisplayTextWithFlavor("Creating task for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{
    72  		"AppName":     cmd.RequiredArgs.AppName,
    73  		"OrgName":     cmd.Config.TargetedOrganization().Name,
    74  		"SpaceName":   space.Name,
    75  		"CurrentUser": user.Name,
    76  	})
    77  
    78  	task, warnings, err := cmd.Actor.RunTask(application.GUID, cmd.RequiredArgs.Command, cmd.Name, cmd.Memory.Size, cmd.Disk.Size)
    79  	cmd.UI.DisplayWarnings(warnings)
    80  	if err != nil {
    81  		return shared.HandleError(err)
    82  	}
    83  
    84  	cmd.UI.DisplayOK()
    85  	cmd.UI.DisplayNewline()
    86  	cmd.UI.DisplayText(`Task has been submitted successfully for execution.
    87  Task name:   {{.TaskName}}
    88  Task id:     {{.TaskSequenceID}}`,
    89  		map[string]interface{}{
    90  			"TaskName":       task.Name,
    91  			"TaskSequenceID": task.SequenceID,
    92  		})
    93  
    94  	return nil
    95  }