-
Notifications
You must be signed in to change notification settings - Fork 3
/
delete.go
80 lines (63 loc) · 1.84 KB
/
delete.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
package cmd
import (
"fmt"
"net/http"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/capeprivacy/cli/entities"
)
// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Use: "delete function_id",
Short: "Delete a deployed function",
Long: "Delete a deployed function, takes function id.\n",
RunE: func(cmd *cobra.Command, args []string) error {
err := delete(cmd, args)
if _, ok := err.(UserError); !ok {
cmd.SilenceUsage = true
}
return err
},
}
func init() {
rootCmd.AddCommand(deleteCmd)
}
func delete(cmd *cobra.Command, args []string) error {
u := C.EnclaveHost
insecure := C.Insecure
if len(args) != 1 {
return UserError{Msg: "you must pass a function ID", Err: fmt.Errorf("invalid number of input arguments")}
}
functionID := args[0]
t, err := authToken()
if err != nil {
return err
}
auth := entities.FunctionAuth{Type: entities.AuthenticationTypeAuth0, Token: t}
err = doDelete(u, functionID, insecure, auth)
if err != nil {
return fmt.Errorf("delete failed: %w", err)
}
return nil
}
func doDelete(url string, functionID string, insecure bool, auth entities.FunctionAuth) error { //nolint:gocognit
endpoint := fmt.Sprintf("%s/v1/functions/%s", url, functionID)
req, err := http.NewRequest("DELETE", endpoint, nil)
if err != nil {
return fmt.Errorf("cannot create http request: %s", 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 {
if res.StatusCode == 404 {
return fmt.Errorf("function ID not found: %s", functionID)
}
return fmt.Errorf("expected 200, got server response code %d", res.StatusCode)
}
log.Infof("Success! Function %s deleted.\n", functionID)
return nil
}