-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.go
50 lines (42 loc) · 1.03 KB
/
jobs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package cmd
import (
"encoding/json"
"errors"
"fmt"
"github.com/renderinc/cli/pkg/http"
"github.com/renderinc/cli/pkg/table"
"github.com/spf13/cobra"
)
// jobsCmd represents the jobs command
var jobsCmd = &cobra.Command{
Use: "jobs",
Short: "Get jobs by service id",
Long: `It's hack day!'`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("provide only one argument")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
serviceId := args[0]
jsonString, err := http.Request(fmt.Sprintf("services/%s/jobs", serviceId))
if err != nil {
panic(err)
}
var cursorJobs []CursorJob
if err := json.Unmarshal(jsonString, &cursorJobs); err != nil {
panic(err)
}
var jobs []Job
for _, cursorJob := range cursorJobs {
jobs = append(jobs, cursorJob.Job)
}
if err := table.Print([]string{"Id", "ServiceId", "StartCommand", "CreatedAt", "FinishedAt", "Status"}, jobs); err != nil {
panic(err)
}
},
}
func init() {
rootCmd.AddCommand(jobsCmd)
}