Skip to content

Commit

Permalink
Add increases command to CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Feb 3, 2024
1 parent 8f7ef1b commit 89bb8e3
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
88 changes: 88 additions & 0 deletions cmd/increases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/self-actuated/actuated-cli/pkg"
"github.com/spf13/cobra"
)

func makeIncreases() *cobra.Command {
cmd := &cobra.Command{
Use: "increases",
Short: "Get job increases for an organisation",
}

cmd.RunE = runIncreasesE

cmd.Flags().Bool("json", false, "Request output in JSON format")
cmd.Flags().Bool("staff", false, "Request staff increases")
cmd.Flags().Int("days", 30, "The number of days to look back for increases")

return cmd
}

func runIncreasesE(cmd *cobra.Command, args []string) error {

var owner string
if len(args) == 1 {
owner = strings.TrimSpace(args[0])
}

pat, err := getPat(cmd)
if err != nil {
return err
}

staff, err := cmd.Flags().GetBool("staff")
if err != nil {
return err
}

requestJson, err := cmd.Flags().GetBool("json")
if err != nil {
return err
}

if len(pat) == 0 {
return fmt.Errorf("pat is required")
}

c := pkg.NewClient(http.DefaultClient, os.Getenv("ACTUATED_URL"))
days, err := cmd.Flags().GetInt("days")
if err != nil {
return err
}

startDate := time.Now().Add(-1 * time.Duration(days) * 24 * time.Hour)

res, status, err := c.GetBuildIncreases(pat, owner, startDate, staff, requestJson)
if err != nil {
return err
}

if status != http.StatusOK {
return fmt.Errorf("unexpected status code: %d, message: %s", status, string(res))
}

if requestJson {

var prettyJSON bytes.Buffer
err := json.Indent(&prettyJSON, []byte(res), "", " ")
if err != nil {
return err
}
res = prettyJSON.String()
}

fmt.Println(res)

return nil

}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ https://github.com/self-actuated/actuated-cli
root.AddCommand(makeAgentLogs())
root.AddCommand(makeLogs())
root.AddCommand(makeUpgrade())
root.AddCommand(makeIncreases())

root.AddCommand(makeSSH())

Expand Down
55 changes: 55 additions & 0 deletions pkg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,61 @@ func (c *Client) ListJobs(patStr string, owner string, staff bool, json bool) (s
return string(body), res.StatusCode, nil
}

func (c *Client) GetBuildIncreases(patStr string, owner string, startDate time.Time, staff bool, json bool) (string, int, error) {

u, _ := url.Parse(c.baseURL)
u.Path = "/api/v1/job-increases"
q := u.Query()

if staff {
q.Set("staff", "1")
}

if len(owner) > 0 {
q.Set("owner", owner)
}
q.Add("startDate", startDate.Format("2006-01-02"))
log.Printf("Date: %s", startDate.Format("2006-01-02"))
u.RawQuery = q.Encode()

req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return "", http.StatusBadRequest, err
}

if json {
req.Header.Set("Accept", "application/json")
}

req.Header.Set("Authorization", "Bearer "+patStr)

if os.Getenv("DEBUG") == "1" {
sanitised := http.Header{}
for k, v := range req.Header {

if k == "Authorization" {
v = []string{"redacted"}
}
sanitised[k] = v
}

fmt.Printf("URL %s\nHeaders: %v\n", u.String(), sanitised)
}

res, err := c.httpClient.Do(req)
if err != nil {
return "", http.StatusServiceUnavailable, err
}

var body []byte
if res.Body != nil {
defer res.Body.Close()
body, _ = io.ReadAll(res.Body)
}

return string(body), res.StatusCode, nil
}

func (c *Client) ListRunners(patStr string, owner string, staff, images, json bool) (string, int, error) {

u, _ := url.Parse(c.baseURL)
Expand Down

0 comments on commit 89bb8e3

Please sign in to comment.