Skip to content

Commit

Permalink
Create job
Browse files Browse the repository at this point in the history
  • Loading branch information
wendorf committed Oct 15, 2021
1 parent b160a5b commit 45f830a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
71 changes: 71 additions & 0 deletions cmd/createjob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"

"github.com/renderinc/cli/pkg/http"
"github.com/renderinc/cli/pkg/table"
"github.com/spf13/cobra"
)

type Job struct {
Id string `json:"id"`
ServiceId string `json:"serviceId"`
StartCommand string `json:"startCommand"`
PlanId string `json:"planId"`
CreatedAt string `json:"createdAt"`
StartedAt string `json:"startedAt"`
FinishedAt string `json:"finishedAt"`
Status string `json:"status"`
}

type CursorJob struct {
Cursor string `json:"cursor"`
Job Job `json:"job"`
}

type CreateJobRequest struct {
StartCommand string `json:"startCommand"`
}

// createJobCmd represents the createJob command
var createJobCmd = &cobra.Command{
Use: "create-job",
Short: "Create job by service id and start command",
Long: `It's hack day!'`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return errors.New("provide only one argument")
}

return nil
},
Run: func(cmd *cobra.Command, args []string) {
serviceId := args[0]
startCommand := args[1]
body := CreateJobRequest{StartCommand: startCommand}
bodyBytes, err := json.Marshal(body)
if err != nil {
panic(err)
}
jsonString, err := http.Create(fmt.Sprintf("services/%s/jobs", serviceId), bodyBytes)
if err != nil {
panic(err)
}

var job Job
if err := json.Unmarshal(jsonString, &job); err != nil {
panic(err)
}

if err := table.Print([]string{"Id", "ServiceId", "StartCommand", "CreatedAt", "FinishedAt", "Status"}, []Job{job}); err != nil {
panic(err)
}
},
}

func init() {
rootCmd.AddCommand(createJobCmd)
}
24 changes: 24 additions & 0 deletions pkg/http/request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -28,5 +29,28 @@ func Request(endpoint string) ([]byte, error) {
return nil, fmt.Errorf("reading request for endpoint %s: %w", endpoint, err)
}

return jsonString, nil
}

func Create(endpoint string, body []byte) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("POST", fmt.Sprintf("https://api.render.com/v1/%s", endpoint), bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("creating request for endpoint %s: %w", endpoint, err)
}

req.Header.Set("Accept", "application/json")
api := viper.GetString("api")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", api))
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("requesting endpoint %s: %w", endpoint, err)
}

jsonString, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("reading request for endpoint %s: %w", endpoint, err)
}

return jsonString, nil
}

0 comments on commit 45f830a

Please sign in to comment.