diff --git a/cmd/fetch.go b/cmd/fetch.go index 402b40c..242a629 100644 --- a/cmd/fetch.go +++ b/cmd/fetch.go @@ -121,6 +121,27 @@ func init() { } help(cmd, s) }) + + // fetch tasks with status + fetchCmd.AddCommand(fetchTasksWithStatusCmd) + + fetchTasksWithStatusCmd.Flags().StringVarP(taskStatus, "status", "x", "", "Task Status") + fetchTasksWithStatusCmd.MarkFlagRequired("status") + fetchTasksWithStatusCmd.Flags().StringVarP(env, "environment", "e", "", "Aurora Environment") + fetchTasksWithStatusCmd.Flags().StringVarP(role, "role", "r", "", "Aurora Role") + fetchTasksWithStatusCmd.Flags().StringVarP(name, "name", "n", "", "Aurora Name") + + // Hijack help function to hide unnecessary global flags + fetchTasksWithStatusCmd.SetHelpFunc(func(cmd *cobra.Command, s []string) { + if cmd.HasInheritedFlags() { + cmd.InheritedFlags().VisitAll(func(f *pflag.Flag) { + if f.Name != "logLevel" { + f.Hidden = true + } + }) + } + help(cmd, s) + }) } var fetchCmd = &cobra.Command{ @@ -206,6 +227,13 @@ var fetchAvailCapacityCmd = &cobra.Command{ Run: fetchAvailCapacity, } +var fetchTasksWithStatusCmd = &cobra.Command{ + Use: "tasks", + Short: "Fetch tasks with status", + Long: `This command will return the list of tasks with a given status`, + Run: fetchTasksWithStatus, +} + func fetchTasksConfig(cmd *cobra.Command, args []string) { log.Infof("Fetching job configuration for [%s/%s/%s] \n", *env, *role, *name) @@ -476,3 +504,74 @@ func fetchAvailCapacity(cmd *cobra.Command, args []string) { fmt.Println(capacity) } } + +//fetchTasksWithStatus returns lists of tasks for a given set of status +func fetchTasksWithStatus(cmd *cobra.Command, args []string) { + status := *taskStatus + + log.Infof("Fetching tasks for role/environment/job:[%s/%s/%s] \n", *role, *env, *name) + log.Infof("Fetching tasks for a given status: %v \n", status) + + // This Query takes nil for values it shouldn't need to match against. + // This allows us to potentially avoid expensive calls for specific environments, roles, or job names. + if *env == "" { + env = nil + } + if *role == "" { + role = nil + } + if *name == "" { + name = nil + } + // role needs to be specified if env is specified + if env != nil { + if role == nil { + log.Fatalln("Role must be specified when env is specified.") + } + } + // role or env needs to be specified if name is specified + if name != nil { + if role == nil && env == nil { + log.Fatalln("Role or env must be specified when name is specified.") + } + } + + queryStatuses, err := scheduleStatusFromString(status) + if err != nil { + log.Fatalf("error: %+v", err) + } + + taskQuery := &aurora.TaskQuery{Environment: env, Role: role, JobName: name, Statuses: queryStatuses} + + tasks, err := client.GetTasksWithoutConfigs(taskQuery) + if err != nil { + log.Fatalf("error: %+v", err) + } + + if toJson { + taskStatus := strings.ToUpper(status) + // convert task lists to a list of task id like role-env-name-[instance-id] + taskIdsMap := map[string][]string{} + var taskIds []string + for _, task := range tasks { + taskIds = append(taskIds, task.AssignedTask.TaskId) + } + taskIdsMap[taskStatus] = taskIds + fmt.Println(internal.ToJSON(taskIdsMap)) + } else { + fmt.Printf("Tasks for status %s:\n", strings.ToUpper(status)) + for _, t := range tasks { + fmt.Println(t.AssignedTask.TaskId) + } + } +} + +// Convert status slice into ScheduleStatus slice +func scheduleStatusFromString(status string) ([]aurora.ScheduleStatus, error) { + scheduleStatus, err := aurora.ScheduleStatusFromString(strings.ToUpper(status)) + if err != nil { + return nil, err + } + result := []aurora.ScheduleStatus{scheduleStatus} + return result, nil +} diff --git a/cmd/root.go b/cmd/root.go index d2d0d71..cc72ac3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -50,6 +50,7 @@ var updateID string var monitor bool var timeout time.Duration var log = logrus.New() +var taskStatus = new(string) const australisVer = "v1.0.4"