Skip to content

Commit 92a0958

Browse files
committed
feat: integrations list
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
1 parent 003ad2a commit 92a0958

File tree

6 files changed

+125
-5
lines changed

6 files changed

+125
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
escape-cli
1010
vendor/
1111
go.work
12+
main
1213

1314
# Env
1415
.env

main

-11.4 MB
Binary file not shown.

pkg/api/api.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"net/http"
78
"os"
89

@@ -22,7 +23,15 @@ func NewAPIClient() (*Client, error) {
2223
return NewClient(
2324
server,
2425
WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
25-
log.Trace("Sending request %s %s", req.Method, req.URL)
26+
log.Debug("Sending request %s %s", req.Method, req.URL)
27+
if req.Body != nil {
28+
clone := req.Clone(context.Background())
29+
body, err := io.ReadAll(clone.Body)
30+
if err != nil {
31+
return err
32+
}
33+
log.Trace("Body %s", string(body))
34+
}
2635
req.Header.Set("Authorization", fmt.Sprintf("Key %s", key))
2736
return nil
2837
}),

pkg/cli/integrations.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
9+
"github.com/Escape-Technologies/cli/pkg/api"
10+
"github.com/spf13/cobra"
11+
"gopkg.in/yaml.v3"
12+
)
13+
14+
var integrationsCmd = &cobra.Command{
15+
Use: "integrations",
16+
Short: "Integrations commands",
17+
}
18+
19+
var integrationsListCmd = &cobra.Command{
20+
Use: "list",
21+
Short: "List integrations",
22+
RunE: func(cmd *cobra.Command, args []string) error {
23+
client, err := api.NewAPIClient()
24+
if err != nil {
25+
return err
26+
}
27+
res, err := client.GetV1Integrations(context.Background())
28+
if err != nil {
29+
return err
30+
}
31+
integrations, err := api.ParseGetV1IntegrationsResponse(res)
32+
if err != nil {
33+
return err
34+
}
35+
switch output {
36+
case outputJSON:
37+
json.NewEncoder(os.Stdout).Encode(integrations.JSON200)
38+
case outputYAML:
39+
yaml.NewEncoder(os.Stdout).Encode(integrations.JSON200)
40+
default:
41+
if integrations.JSON200 == nil {
42+
fmt.Println("No integrations found")
43+
} else {
44+
for _, integration := range *integrations.JSON200 {
45+
fmt.Println(integration.Name)
46+
}
47+
}
48+
}
49+
return nil
50+
},
51+
}

pkg/cli/locations.go

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88

99
"github.com/Escape-Technologies/cli/pkg/api"
10+
"github.com/Escape-Technologies/cli/pkg/log"
1011
"github.com/spf13/cobra"
1112
"gopkg.in/yaml.v3"
1213
)
@@ -34,12 +35,66 @@ var locationsListCmd = &cobra.Command{
3435
}
3536
switch output {
3637
case outputJSON:
37-
json.NewEncoder(os.Stdout).Encode(locations.JSON200.Locations)
38+
json.NewEncoder(os.Stdout).Encode(locations.JSON200)
3839
case outputYAML:
39-
yaml.NewEncoder(os.Stdout).Encode(locations.JSON200.Locations)
40+
yaml.NewEncoder(os.Stdout).Encode(locations.JSON200)
4041
default:
41-
for _, location := range locations.JSON200.Locations {
42-
fmt.Println(location.Name)
42+
if locations.JSON200 == nil {
43+
fmt.Println("No locations found")
44+
} else {
45+
for _, location := range *locations.JSON200 {
46+
fmt.Println(location.Name)
47+
}
48+
}
49+
}
50+
return nil
51+
},
52+
}
53+
54+
var locationsDeleteCmd = &cobra.Command{
55+
Use: "del",
56+
Short: "Delete location",
57+
RunE: func(cmd *cobra.Command, args []string) error {
58+
if len(args) == 0 {
59+
return fmt.Errorf("location id is required")
60+
}
61+
log.Info("Deleting location %s", args[0])
62+
client, err := api.NewAPIClient()
63+
if err != nil {
64+
return err
65+
}
66+
res, err := client.DeleteV1LocationsId(context.Background(), args[0])
67+
if err != nil {
68+
return err
69+
}
70+
deleted, err := api.ParseDeleteV1LocationsIdResponse(res)
71+
if err != nil {
72+
return err
73+
}
74+
switch output {
75+
case outputJSON:
76+
if deleted.JSON200 != nil {
77+
json.NewEncoder(os.Stdout).Encode(deleted.JSON200)
78+
} else if deleted.JSON404 != nil {
79+
json.NewEncoder(os.Stdout).Encode(deleted.JSON404)
80+
} else {
81+
json.NewEncoder(os.Stdout).Encode(deleted.JSON400)
82+
}
83+
case outputYAML:
84+
if deleted.JSON200 != nil {
85+
yaml.NewEncoder(os.Stdout).Encode(deleted.JSON200)
86+
} else if deleted.JSON404 != nil {
87+
yaml.NewEncoder(os.Stdout).Encode(deleted.JSON404)
88+
} else {
89+
yaml.NewEncoder(os.Stdout).Encode(deleted.JSON400)
90+
}
91+
default:
92+
if deleted.JSON200 != nil {
93+
fmt.Println("Location deleted")
94+
} else if deleted.JSON404 != nil {
95+
fmt.Println("Location not found")
96+
} else {
97+
fmt.Println("Unknown error")
4398
}
4499
}
45100
return nil

pkg/cli/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func Run() error {
5757

5858
rootCmd.AddCommand(locationsCmd)
5959
locationsCmd.AddCommand(locationsListCmd)
60+
locationsCmd.AddCommand(locationsDeleteCmd)
61+
62+
rootCmd.AddCommand(integrationsCmd)
63+
integrationsCmd.AddCommand(integrationsListCmd)
6064

6165
return rootCmd.Execute()
6266
}

0 commit comments

Comments
 (0)