-
Notifications
You must be signed in to change notification settings - Fork 3
/
list.go
139 lines (113 loc) · 3.13 KB
/
list.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package cmd
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"github.com/capeprivacy/cli/entities"
)
type ErrorMsg struct {
Error string `json:"error"`
}
type ErrServerForList struct {
statusCode int
message string
}
func (e ErrServerForList) Error() string {
return fmt.Sprintf("expected 200, got server response code when listing functions %d: %s", e.statusCode, e.message)
}
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Short: "List all deployed functions",
Long: "List all deployed functions for your user.\n",
RunE: func(cmd *cobra.Command, args []string) error {
err := list(cmd, args)
if _, ok := err.(UserError); !ok {
cmd.SilenceUsage = true
}
return err
},
}
func init() {
rootCmd.AddCommand(listCmd)
listCmd.PersistentFlags().IntP("limit", "", 50, "Limit the number of functions returned.")
listCmd.PersistentFlags().IntP("offset", "", 0, "Number of functions to skip before listing.")
}
func list(cmd *cobra.Command, args []string) error {
u := C.EnclaveHost
insecure := C.Insecure
limit, err := cmd.Flags().GetInt("limit")
if err != nil {
return err
}
offset, err := cmd.Flags().GetInt("offset")
if err != nil {
return err
}
if len(args) > 0 {
return UserError{Msg: "list does not take any arguments", Err: fmt.Errorf("invalid number of input arguments")}
}
t, err := authToken()
if err != nil {
return err
}
auth := entities.FunctionAuth{Type: entities.AuthenticationTypeUserToken, Token: t}
err = doList(u, insecure, auth, limit, offset)
if err != nil {
return fmt.Errorf("list failed: %w", err)
}
return nil
}
func doList(url string, insecure bool, auth entities.FunctionAuth, limit int, offset int) error { //nolint:gocognit
endpoint := fmt.Sprintf("%s/v1/functions?limit=%d&offset=%d", url, limit, offset)
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return err
}
var bearer = "Bearer " + auth.Token
req.Header.Add("Authorization", bearer)
res, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("cannot complete http request: %s", err)
}
if res.StatusCode != 200 {
var e ErrorMsg
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
return err
}
res.Body.Close()
return ErrServerForList{res.StatusCode, e.Error}
}
body, err := io.ReadAll(res.Body)
if err != nil {
return errors.New("could not read response body")
}
var deploymentNames []entities.DeploymentName
err = json.Unmarshal(body, &deploymentNames)
if err != nil {
return errors.New("malformed body in response")
}
if len(deploymentNames) == 0 {
fmt.Println("No functions deployed.")
return nil
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Name", "ID", "Created At"})
localTime, err := time.LoadLocation("Local")
if err != nil {
return err
}
for i, deployment := range deploymentNames {
t.AppendRow([]interface{}{i, deployment.Name, deployment.ID, deployment.CreatedAt.In(localTime).Format("Jan 02 2006 15:04")})
}
t.SetStyle(table.StyleLight)
t.Render()
return nil
}