github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/commands/jobs/list.go (about) 1 package jobs 2 3 import ( 4 "fmt" 5 "sort" 6 "time" 7 8 "github.com/Sirupsen/logrus" 9 "github.com/daticahealth/cli/commands/services" 10 "github.com/daticahealth/cli/models" 11 "github.com/olekukonko/tablewriter" 12 ) 13 14 type SortedJobs []models.Job 15 16 func (jobs SortedJobs) Len() int { 17 return len(jobs) 18 } 19 20 func (jobs SortedJobs) Swap(i, j int) { 21 jobs[i], jobs[j] = jobs[j], jobs[i] 22 } 23 24 func (jobs SortedJobs) Less(i, j int) bool { 25 return jobs[i].Type < jobs[j].Type 26 } 27 28 func CmdList(svcName string, ij IJobs, is services.IServices) error { 29 service, err := is.RetrieveByLabel(svcName) 30 if err != nil { 31 return err 32 } 33 if service == nil { 34 return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"datica services list\" command.", svcName) 35 } 36 37 jbs, err := ij.List(service.ID) 38 if err != nil { 39 return err 40 } 41 42 if jbs == nil || len(*jbs) == 0 { 43 logrus.Println("No releases found") 44 return nil 45 } 46 47 sort.Sort(SortedJobs(*jbs)) 48 const dateForm = "2006-01-02T15:04:05" 49 data := [][]string{{"Job Id", "Status", "Created At", "Type", "Target"}} 50 for _, j := range *jbs { 51 id := j.ID 52 53 t, _ := time.Parse(dateForm, j.CreatedAt) 54 data = append(data, []string{id, j.Status, t.Local().Format(time.ANSIC), j.Type, j.Target}) 55 } 56 57 table := tablewriter.NewWriter(logrus.StandardLogger().Out) 58 table.SetBorder(false) 59 table.SetRowLine(false) 60 table.SetCenterSeparator("") 61 table.SetColumnSeparator("") 62 table.SetRowSeparator("") 63 table.AppendBulk(data) 64 table.Render() 65 66 return nil 67 } 68 69 func (j *SJobs) List(svcID string) (*[]models.Job, error) { 70 headers := j.Settings.HTTPManager.GetHeaders(j.Settings.SessionToken, j.Settings.Version, j.Settings.Pod, j.Settings.UsersID) 71 resp, statusCode, err := j.Settings.HTTPManager.Get(nil, 72 fmt.Sprintf("%s%s/environments/%s/services/%s/jobs", 73 j.Settings.PaasHost, j.Settings.PaasHostVersion, j.Settings.EnvironmentID, svcID), headers) 74 if err != nil { 75 return nil, err 76 } 77 var jbs []models.Job 78 err = j.Settings.HTTPManager.ConvertResp(resp, statusCode, &jbs) 79 if err != nil { 80 return nil, err 81 } 82 return &jbs, nil 83 }