Skip to content

Commit

Permalink
fix: bump schema and add locations crud
Browse files Browse the repository at this point in the history
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
  • Loading branch information
QuentinN42 committed Feb 12, 2025
1 parent bd1830d commit dbe5d18
Show file tree
Hide file tree
Showing 9 changed files with 13,081 additions and 574 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Locations List",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "cmd/main.go",
"envFile": "${workspaceFolder}/.env",
"args": [
"locations",
"list"
]
}
]
}
12,147 changes: 12,146 additions & 1 deletion assets/public-api.json

Large diffs are not rendered by default.

1,236 changes: 732 additions & 504 deletions pkg/api/generated.go

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pkg/cli/integrations_akamai.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"context"
"fmt"

"github.com/Escape-Technologies/cli/pkg/api"
Expand All @@ -21,7 +20,7 @@ var integrationsAkamaiList = &cobra.Command{
if err != nil {
return err
}
integrations, err := client.GetV1IntegrationsAkamaiWithResponse(context.Background())
integrations, err := client.GetV1IntegrationsAkamaiWithResponse(cmd.Context())
if err != nil {
return err
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/cli/integrations_kubernetes.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cli

import (
"context"
"fmt"

"github.com/Escape-Technologies/cli/pkg/api"
"github.com/google/uuid"
"github.com/spf13/cobra"
)

Expand All @@ -23,7 +23,7 @@ var integrationsKubernetesList = &cobra.Command{
if err != nil {
return err
}
integrations, err := client.GetV1IntegrationsKubernetesWithResponse(context.Background())
integrations, err := client.GetV1IntegrationsKubernetesWithResponse(cmd.Context())
if err != nil {
return err
}
Expand All @@ -49,12 +49,16 @@ var integrationsKubernetesDelete = &cobra.Command{
Args: cobra.ExactArgs(1),
Short: "Delete integration given an id",
RunE: func(cmd *cobra.Command, args []string) error {
id := args[0]
idString := args[0]
id, err := uuid.Parse(idString)
if err != nil {
return fmt.Errorf("invalid UUID format: %w", err)
}
client, err := api.NewAPIClient()
if err != nil {
return err
}
res, err := client.DeleteV1IntegrationsKubernetesIdWithResponse(context.Background(), id)
res, err := client.DeleteV1IntegrationsKubernetesIdWithResponse(cmd.Context(), id)
if err != nil {
return err
}
Expand Down
198 changes: 153 additions & 45 deletions pkg/cli/locations.go
Original file line number Diff line number Diff line change
@@ -1,97 +1,205 @@
package cli

import (
"context"
"encoding/json"
"fmt"
"os"
"strings"

"github.com/Escape-Technologies/cli/pkg/api"
"github.com/Escape-Technologies/cli/pkg/log"
"github.com/google/uuid"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

var locationsCmd = &cobra.Command{
Use: "locations",
Short: "Locations commands",
Use: "locations",
Aliases: []string{"loc"},
Short: "Locations commands",
}

var locationsListCmd = &cobra.Command{
Use: "list",
Short: "List locations",
Use: "list",
Aliases: []string{"ls"},
Short: "List locations",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := api.NewAPIClient()
if err != nil {
return err
}
res, err := client.GetV1Locations(context.Background())
locations, err := client.ListLocationsWithResponse(cmd.Context())
if err != nil {
return err
}
locations, err := api.ParseGetV1LocationsResponse(res)
if err != nil {
return err
}
switch output {
case outputJSON:
json.NewEncoder(os.Stdout).Encode(locations.JSON200)
case outputYAML:
yaml.NewEncoder(os.Stdout).Encode(locations.JSON200)
default:
print(locations.JSON200, func() {
if locations.JSON200 == nil {
fmt.Println("No locations found")
} else {
for _, location := range *locations.JSON200 {
fmt.Println(location.Name)
size := len(*location.Type)
fmt.Printf("%s%s%s\n", *location.Type, strings.Repeat(" ", 10-size), *location.Name)
}
}
}
})
return nil
},
}

var locationsDeleteCmd = &cobra.Command{
Use: "del",
Short: "Delete location",
Use: "del",
Aliases: []string{"delete"},
Args: cobra.ExactArgs(1),
Short: "Delete location",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("location id is required")
idString := args[0]
id, err := uuid.Parse(idString)
if err != nil {
return fmt.Errorf("invalid UUID format: %w", err)
}
log.Info("Deleting location %s", args[0])
client, err := api.NewAPIClient()
if err != nil {
return err
}
res, err := client.DeleteV1LocationsId(context.Background(), args[0])
log.Debug("Deleting response %s", res.Status)
deleted, err := client.DeleteLocationWithResponse(cmd.Context(), id)
if err != nil {
return err
}
deleted, err := api.ParseDeleteV1LocationsIdResponse(res)
if deleted.JSON200 != nil {
print(deleted.JSON200, func() {
fmt.Println("Location deleted")
})
} else if deleted.JSON404 != nil {
print(deleted.JSON404, func() {
fmt.Printf("Location %s not found: %s", idString, deleted.JSON404.Message)
})
} else if deleted.JSON400 != nil {
print(deleted.JSON400, func() {
fmt.Printf("Unable to delete location: %s", deleted.JSON400.Message)
})
} else {
print(unknowError, func() {
fmt.Printf("Unable to delete location: Unknown error")
})
}
return nil
},
}

var locationsGetCmd = &cobra.Command{
Use: "get",
Aliases: []string{"read"},
Args: cobra.ExactArgs(1),
Short: "Get location",
RunE: func(cmd *cobra.Command, args []string) error {
idString := args[0]
id, err := uuid.Parse(idString)
if err != nil {
return fmt.Errorf("invalid UUID format: %w", err)
}
log.Info("Getting location %s", args[0])
client, err := api.NewAPIClient()
if err != nil {
return err
}
ok := true
var data interface{}
if deleted.JSON200 != nil {
data = deleted.JSON200
} else if deleted.JSON404 != nil {
ok = false
data = deleted.JSON404
log.Info("Location %s not found: %s", args[0], deleted.JSON404.Message)
location, err := client.GetLocationWithResponse(cmd.Context(), id)
if err != nil {
return err
}
if location.JSON200 != nil {
print(location.JSON200, func() {
fmt.Println("Location:", *location.JSON200.Name)
fmt.Println("Type:", *location.JSON200.Type)
fmt.Println("Id:", *location.JSON200.Id)
})
} else if location.JSON404 != nil {
print(location.JSON404, func() {
fmt.Printf("Location %s not found: %s", idString, location.JSON404.Message)
})
} else {
ok = false
data = deleted.JSON400
log.Info("Error deleting location %s: %s", args[0], deleted.JSON400.Message)
print(unknowError, func() {
fmt.Printf("Unable to get location: Unknown error")
})
}
print(data, func() {
if ok {
fmt.Println("Location deleted")
} else {
fmt.Println("unable to delete location")
}
return nil
},
}

var locationsCreateCmd = &cobra.Command{
Use: "create",
Aliases: []string{"new"},
Args: cobra.ExactArgs(1),
Short: "Create a new location with the given name",
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
log.Info("Deleting location %s", args[0])
client, err := api.NewAPIClient()
if err != nil {
return err
}
location, err := client.CreateLocationWithResponse(cmd.Context(), api.CreateLocationJSONRequestBody{
Name: name,
})
if err != nil {
return err
}
if location.JSON200 != nil {
print(location.JSON200, func() {
fmt.Println("Location:", *location.JSON200.Name)
fmt.Println("Type:", *location.JSON200.Type)
fmt.Println("Id:", *location.JSON200.Id)
})
} else if location.JSON400 != nil {
print(location.JSON400, func() {
fmt.Printf("Unable to create location: %s", location.JSON400.Message)
})
} else if location.JSON500 != nil {
print(location.JSON500, func() {
fmt.Printf("Unable to create location: %s", location.JSON500.Message)
})
} else {
print(unknowError, func() {
fmt.Printf("Unable to upsert location: Unknown error")
})
}
return nil
},
}

var locationsUpsertCmd = &cobra.Command{
Use: "upsert",
Args: cobra.ExactArgs(1),
Short: "Get or create a location by it's name",
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
log.Info("Upserting location %s", args[0])
client, err := api.NewAPIClient()
if err != nil {
return err
}
location, err := client.UpsertLocationWithResponse(cmd.Context(), api.UpsertLocationJSONRequestBody{
Name: name,
})
if err != nil {
return err
}
if location.JSON200 != nil {
print(location.JSON200, func() {
fmt.Println("Location:", *location.JSON200.Name)
fmt.Println("Type:", *location.JSON200.Type)
fmt.Println("Id:", *location.JSON200.Id)
})
} else if location.JSON400 != nil {
print(location.JSON400, func() {
fmt.Printf("Unable to upsert location: %s", location.JSON400.Message)
})
} else if location.JSON500 != nil {
print(location.JSON500, func() {
fmt.Printf("Unable to upsert location: %s", location.JSON500.Message)
})
} else {
print(unknowError, func() {
fmt.Printf("Unable to upsert location: Unknown error")
})
}
return nil
},
}
9 changes: 9 additions & 0 deletions pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ const (

var output outputT = outputPretty

var unknowError = struct {
Error string `json:"error"`
}{
Error: "An unknown error occurred",
}

func print(data any, pretty func()) {
switch output {
case outputJSON:
Expand Down Expand Up @@ -74,6 +80,9 @@ func Run() error {
rootCmd.AddCommand(locationsCmd)
locationsCmd.AddCommand(locationsListCmd)
locationsCmd.AddCommand(locationsDeleteCmd)
locationsCmd.AddCommand(locationsGetCmd)
locationsCmd.AddCommand(locationsCreateCmd)
locationsCmd.AddCommand(locationsUpsertCmd)

// Integrations
rootCmd.AddCommand(integrationsCmd)
Expand Down
21 changes: 7 additions & 14 deletions pkg/cli/scan.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package cli

import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/Escape-Technologies/cli/pkg/api"
"github.com/google/uuid"
"github.com/oapi-codegen/runtime/types"
"github.com/spf13/cobra"
)

Expand All @@ -18,25 +16,20 @@ var startScanCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Example: "escape-cli start-scan 123e4567-e89b-12d3-a456-426614174001",
RunE: func(cmd *cobra.Command, args []string) error {
applicationID := args[0]
fmt.Printf("Triggering scan for application %s\n\n", applicationID)

client, err := api.NewAPIClient()
applicationIdString := args[0]
fmt.Printf("Triggering scan for application %s\n\n", applicationIdString)
applicationId, err := uuid.Parse(applicationIdString)
if err != nil {
return fmt.Errorf("failed to create API client: %w", err)
return fmt.Errorf("invalid UUID format: %w", err)
}

parsedUUID, err := uuid.Parse(applicationID)
client, err := api.NewAPIClient()
if err != nil {
return fmt.Errorf("invalid UUID format: %w", err)
}
applicationId := types.UUID(parsedUUID)
params := &api.PostApplicationsIdStartScanParams{
ContentType: api.PostApplicationsIdStartScanParamsContentTypeApplicationjson,
return fmt.Errorf("failed to create API client: %w", err)
}

body := api.PostApplicationsIdStartScanJSONRequestBody{}
scan, err := client.PostApplicationsIdStartScanWithResponse(context.Background(), applicationId, params, body)
scan, err := client.PostApplicationsIdStartScanWithResponse(cmd.Context(), applicationId, body)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit dbe5d18

Please sign in to comment.