github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/commands/jobs/contract.go (about)

     1  package jobs
     2  
     3  import (
     4  	"github.com/Sirupsen/logrus"
     5  	"github.com/daticahealth/cli/commands/services"
     6  	"github.com/daticahealth/cli/config"
     7  	"github.com/daticahealth/cli/lib/auth"
     8  	"github.com/daticahealth/cli/lib/prompts"
     9  	"github.com/daticahealth/cli/models"
    10  	"github.com/jault3/mow.cli"
    11  )
    12  
    13  var Cmd = models.Command{
    14  	Name:      "jobs",
    15  	ShortHelp: "Perform operations on a service's jobs",
    16  	LongHelp:  "The <code>jobs</code> command allows you to manage jobs for your service(s).  The jobs command cannot be run directly but has subcommands.",
    17  	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
    18  		return func(cmd *cli.Cmd) {
    19  			cmd.CommandLong(ListSubCmd.Name, ListSubCmd.ShortHelp, ListSubCmd.LongHelp, ListSubCmd.CmdFunc(settings))
    20  			cmd.CommandLong(StartSubCmd.Name, StartSubCmd.ShortHelp, StartSubCmd.LongHelp, StartSubCmd.CmdFunc(settings))
    21  			cmd.CommandLong(StopSubCmd.Name, StopSubCmd.ShortHelp, StopSubCmd.LongHelp, StopSubCmd.CmdFunc(settings))
    22  		}
    23  	},
    24  }
    25  
    26  var ListSubCmd = models.Command{
    27  	Name:      "list",
    28  	ShortHelp: "List all jobs for a service",
    29  	LongHelp: "<code>jobs list</code> prints out a list of all jobs in your environment and their current status. " +
    30  		"Here is a sample command:\n\n" +
    31  		"<pre>\ndatica -E \"<your_env_name>\" jobs list <your_service_name>\n</pre>",
    32  	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
    33  		return func(subCmd *cli.Cmd) {
    34  			serviceName := subCmd.StringArg("SERVICE_NAME", "", "The name of the service to list jobs for")
    35  			subCmd.Action = func() {
    36  				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
    37  					logrus.Fatal(err.Error())
    38  				}
    39  				if err := config.CheckRequiredAssociation(settings); err != nil {
    40  					logrus.Fatal(err.Error())
    41  				}
    42  				err := CmdList(*serviceName, New(settings), services.New(settings))
    43  				if err != nil {
    44  					logrus.Fatal(err.Error())
    45  				}
    46  			}
    47  		}
    48  	},
    49  }
    50  
    51  var StartSubCmd = models.Command{
    52  	Name:      "start",
    53  	ShortHelp: "Start a specific job within a service",
    54  	LongHelp: "<code>jobs start</code> will start a job that is configured but not currently running within a given service. " +
    55  		"This command is useful for granual control of your services and their workers, tasks, etc. " +
    56  		"<pre>\ndatica -E \"<your_env_name>\" jobs start <your_service_name> <your_job_id>\n</pre>",
    57  	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
    58  		return func(subCmd *cli.Cmd) {
    59  			serviceName := subCmd.StringArg("SERVICE_NAME", "", "The name of the service to list jobs for")
    60  			jobID := subCmd.StringArg("JOB_ID", "", "The job ID for the job in service to be started")
    61  			subCmd.Action = func() {
    62  				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
    63  					logrus.Fatal(err.Error())
    64  				}
    65  				if err := config.CheckRequiredAssociation(settings); err != nil {
    66  					logrus.Fatal(err.Error())
    67  				}
    68  				err := CmdStart(*jobID, *serviceName, New(settings), services.New(settings))
    69  				if err != nil {
    70  					logrus.Fatal(err.Error())
    71  				}
    72  			}
    73  		}
    74  	},
    75  }
    76  
    77  var StopSubCmd = models.Command{
    78  	Name:      "stop",
    79  	ShortHelp: "Stop a specific job within a service",
    80  	LongHelp: "<code>jobs stop</code> will shut down a running job within a given service. " +
    81  		"This command is useful for granular control of your services and their workers, tasks, etc. " +
    82  		"<pre>\ndatica -E \"<your_env_name>\" jobs stop <your_service_name> <your_job_id>\n</pre>",
    83  	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
    84  		return func(subCmd *cli.Cmd) {
    85  			serviceName := subCmd.StringArg("SERVICE_NAME", "", "The name of the service to list jobs for")
    86  			jobID := subCmd.StringArg("JOB_ID", "", "The job ID for the job in service to be stopped")
    87  			force := subCmd.BoolOpt("f force", false, "Allow this command to be executed without prompting to confirm")
    88  
    89  			subCmd.Action = func() {
    90  				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
    91  					logrus.Fatal(err.Error())
    92  				}
    93  				if err := config.CheckRequiredAssociation(settings); err != nil {
    94  					logrus.Fatal(err.Error())
    95  				}
    96  				err := CmdStop(*jobID, *serviceName, New(settings), services.New(settings), *force, prompts.New())
    97  				if err != nil {
    98  					logrus.Fatal(err.Error())
    99  				}
   100  			}
   101  			subCmd.Spec = "[SERVICE_NAME] [JOB_ID] [-f]"
   102  
   103  		}
   104  	},
   105  }
   106  
   107  // IJobs describes the jobs commands
   108  type IJobs interface {
   109  	List(svcID string) (*[]models.Job, error)
   110  	Start(jobID string, svcID string) error
   111  	Stop(jobID string, svcID string) error
   112  }
   113  
   114  // SJobs is a concrete implementation of IJobs
   115  type SJobs struct {
   116  	Settings *models.Settings
   117  }
   118  
   119  // New generates a new instance of IJobs
   120  func New(settings *models.Settings) IJobs {
   121  	return &SJobs{
   122  		Settings: settings,
   123  	}
   124  }