From 252f186d477fd2e081d3f98c6fc900e1be0155d8 Mon Sep 17 00:00:00 2001 From: Luc DUZAN Date: Wed, 20 Mar 2024 16:43:46 +0100 Subject: [PATCH 1/2] Get kind from openapi --- README.md | 8 --- client/client.go | 90 ++++++++++++++----------------- cmd/apply.go | 6 +-- cmd/delete.go | 6 +-- cmd/get.go | 46 +++++++++++++--- cmd/root.go | 21 ++++++++ go.mod | 11 +++- go.sum | 127 +++++++++++++++++++++++++++++++++++++++++++- schema/schema.go | 45 ++++++++++++++++ utils/utils.go | 40 ++++++++++++++ utils/utils_test.go | 63 ++++++++++++++++++++++ 11 files changed, 389 insertions(+), 74 deletions(-) create mode 100644 schema/schema.go create mode 100644 utils/utils.go create mode 100644 utils/utils_test.go diff --git a/README.md b/README.md index 075d50a..1f93ec5 100644 --- a/README.md +++ b/README.md @@ -37,14 +37,6 @@ How to run integration test: First login to your teleport proxy, for example: ``` tsh login --proxy=teleport-01.prd.tooling.cdkt.dev --auth=github -``` - -``` -conduktor get application --cert $(tsh apps config --format=cert) --key $(tsh apps config --format=key) -``` - -Or: -``` export CDK_CERT=$(tsh apps config --format=cert) export CDK_KEY=$(tsh apps config --format=key) conduktor get application diff --git a/client/client.go b/client/client.go index f3994d5..690dd89 100644 --- a/client/client.go +++ b/client/client.go @@ -4,12 +4,12 @@ import ( "crypto/tls" "encoding/json" "fmt" - "os" - "strings" - "github.com/conduktor/ctl/printutils" "github.com/conduktor/ctl/resource" + "github.com/conduktor/ctl/utils" "github.com/go-resty/resty/v2" + "os" + "strings" ) type Client struct { @@ -18,16 +18,16 @@ type Client struct { client *resty.Client } -func Make(token string, baseUrl string, debug bool, key, cert string) Client { +func Make(token string, baseUrl string, debug bool, key, cert string) *Client { certificate, _ := tls.LoadX509KeyPair(cert, key) - return Client{ + return &Client{ token: token, - baseUrl: baseUrl + "/public/v1", + baseUrl: baseUrl, client: resty.New().SetDebug(debug).SetHeader("Authorization", "Bearer "+token).SetCertificates(certificate), } } -func MakeFromEnv(debug bool, key, cert string) Client { +func MakeFromEnv() *Client { token := os.Getenv("CDK_TOKEN") if token == "" { fmt.Fprintln(os.Stderr, "Please set CDK_TOKEN") @@ -38,16 +38,11 @@ func MakeFromEnv(debug bool, key, cert string) Client { fmt.Fprintln(os.Stderr, "Please set CDK_BASE_URL") os.Exit(2) } - finalKey := key - finalCert := cert - if finalKey == "" { - finalKey = os.Getenv("CDK_KEY") - } - if finalCert == "" { - finalCert = os.Getenv("CDK_CERT") - } + debug := strings.ToLower(os.Getenv("CDK_DEBUG")) == "true" + key := os.Getenv("CDK_KEY") + cert := os.Getenv("CDK_CERT") - return Make(token, baseUrl, debug, finalKey, finalCert) + return Make(token, baseUrl, debug, key, cert) } type UpsertResponse struct { @@ -64,8 +59,16 @@ func extractApiError(resp *resty.Response) string { } } +func (client *Client) publicV1Url() string { + return client.baseUrl + "/public/v1" +} + +func (client *Client) ActivateDebug() { + client.client.SetDebug(true) +} + func (client *Client) Apply(resource *resource.Resource, dryMode bool) (string, error) { - url := client.baseUrl + "/" + UpperCamelToKebab(resource.Kind) + url := client.publicV1Url() + "/" + utils.UpperCamelToKebab(resource.Kind) builder := client.client.R().SetBody(resource.Json) if dryMode { builder = builder.SetQueryParam("dryMode", "true") @@ -73,8 +76,7 @@ func (client *Client) Apply(resource *resource.Resource, dryMode bool) (string, resp, err := builder.Put(url) if err != nil { return "", err - } - if resp.IsError() { + } else if resp.IsError() { return "", fmt.Errorf(extractApiError(resp)) } bodyBytes := resp.Body() @@ -97,32 +99,33 @@ func printResponseAsYaml(bytes []byte) error { } func (client *Client) Get(kind string) error { - url := client.baseUrl + "/" + UpperCamelToKebab(kind) + url := client.publicV1Url() + "/" + utils.UpperCamelToKebab(kind) resp, err := client.client.R().Get(url) - if resp.IsError() { - return fmt.Errorf(extractApiError(resp)) - } if err != nil { return err + } else if resp.IsError() { + return fmt.Errorf(extractApiError(resp)) } return printResponseAsYaml(resp.Body()) } + func (client *Client) Describe(kind, name string) error { - url := client.baseUrl + "/" + UpperCamelToKebab(kind) + "/" + name + url := client.publicV1Url() + "/" + utils.UpperCamelToKebab(kind) + "/" + name resp, err := client.client.R().Get(url) - if resp.IsError() { - return fmt.Errorf("error describing resources %s/%s, got status code: %d:\n %s", kind, name, resp.StatusCode(), string(resp.Body())) - } if err != nil { return err + } else if resp.IsError() { + return fmt.Errorf("error describing resources %s/%s, got status code: %d:\n %s", kind, name, resp.StatusCode(), string(resp.Body())) } return printResponseAsYaml(resp.Body()) } func (client *Client) Delete(kind, name string) error { - url := client.baseUrl + "/" + UpperCamelToKebab(kind) + "/" + name + url := client.publicV1Url() + "/" + utils.UpperCamelToKebab(kind) + "/" + name resp, err := client.client.R().Delete(url) - if resp.IsError() { + if err != nil { + return err + } else if resp.IsError() { return fmt.Errorf(extractApiError(resp)) } else { fmt.Printf("%s/%s deleted\n", kind, name) @@ -131,26 +134,13 @@ func (client *Client) Delete(kind, name string) error { return err } -func UpperCamelToKebab(input string) string { - // Split the input string into words - words := make([]string, 0) - currentWord := "" - for _, char := range input { - if char >= 'A' && char <= 'Z' { - if currentWord != "" { - words = append(words, currentWord) - } - currentWord = string(char) - } else { - currentWord += string(char) - } - } - if currentWord != "" { - words = append(words, currentWord) +func (client *Client) GetOpenApi() ([]byte, error) { + url := client.baseUrl + "public/docs/docs.yaml" + resp, err := client.client.R().Get(url) + if err != nil { + return nil, err + } else if resp.IsError() { + return nil, fmt.Errorf(resp.String()) } - - // Join the words with hyphens - kebabCase := strings.ToLower(strings.Join(words, "-")) - - return kebabCase + return resp.Body(), nil } diff --git a/cmd/apply.go b/cmd/apply.go index 8b56e8f..50f729a 100644 --- a/cmd/apply.go +++ b/cmd/apply.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "github.com/conduktor/ctl/client" "github.com/conduktor/ctl/resource" "github.com/spf13/cobra" "os" @@ -26,9 +25,8 @@ var applyCmd = &cobra.Command{ } resources = append(resources, r...) } - client := client.MakeFromEnv(*debug, *key, *cert) for _, resource := range resources { - upsertResult, err := client.Apply(&resource, *dryRun) + upsertResult, err := apiClient.Apply(&resource, *dryRun) if err != nil { fmt.Fprintf(os.Stderr, "Could not apply resource %s/%s: %s\n", resource.Kind, resource.Name, err) os.Exit(1) @@ -52,7 +50,7 @@ func resourceForPath(path string) ([]resource.Resource, error) { } } -func init() { +func initApply() { rootCmd.AddCommand(applyCmd) // Here you will define your flags and configuration settings. diff --git a/cmd/delete.go b/cmd/delete.go index e6a5281..6eaacdb 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "github.com/conduktor/ctl/client" "github.com/spf13/cobra" "os" ) @@ -14,8 +13,7 @@ var deleteCmd = &cobra.Command{ Long: ``, Args: cobra.ExactArgs(2), Run: func(cmd *cobra.Command, args []string) { - client := client.MakeFromEnv(*debug, *key, *cert) - err := client.Delete(args[0], args[1]) + err := apiClient.Delete(args[0], args[1]) if err != nil { fmt.Fprintf(os.Stderr, "%s\n", err) os.Exit(1) @@ -23,6 +21,6 @@ var deleteCmd = &cobra.Command{ }, } -func init() { +func initDelete() { rootCmd.AddCommand(deleteCmd) } diff --git a/cmd/get.go b/cmd/get.go index 30e581c..5df9fa4 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -2,13 +2,16 @@ package cmd import ( "fmt" - "github.com/conduktor/ctl/client" "github.com/spf13/cobra" "os" ) -// applyCmd represents the apply command var getCmd = &cobra.Command{ + Use: "get", + Short: "get resource of a given kind", +} + +var getCmdWhenNoSchema = &cobra.Command{ Use: "get kind [name]", Short: "get resource of a given kind", Long: `If name not provided it will list all resource. For example: @@ -18,12 +21,11 @@ conduktor get application myapp will describe the application myapp`, Args: cobra.MatchAll(cobra.MinimumNArgs(1), cobra.MaximumNArgs(2)), Run: func(cmd *cobra.Command, args []string) { - client := client.MakeFromEnv(*debug, *key, *cert) var err error if len(args) == 1 { - err = client.Get(args[0]) + err = apiClient.Get(args[0]) } else if len(args) == 2 { - err = client.Describe(args[0], args[1]) + err = apiClient.Describe(args[0], args[1]) } if err != nil { fmt.Fprintf(os.Stderr, "%s\n", err) @@ -32,6 +34,38 @@ will describe the application myapp`, }, } -func init() { +func initGet() { rootCmd.AddCommand(getCmd) + if schemaClient == nil { + getCmd.AddCommand(getCmdWhenNoSchema) + return + } + tags, err := schemaClient.GetKind() + if err != nil { + fmt.Fprintf(os.Stderr, "Could not load kind from openapi: %s\n", err) + getCmd.AddCommand(getCmdWhenNoSchema) + return + } + + for _, tag := range tags { + tagCmd := &cobra.Command{ + Use: fmt.Sprintf("%s [name]", tag), + Short: "get resource of kind " + tag, + Args: cobra.MatchAll(cobra.MaximumNArgs(1)), + Long: `If name not provided it will list all resource`, + Run: func(cmd *cobra.Command, args []string) { + var err error + if len(args) == 0 { + err = apiClient.Get(tag) + } else if len(args) == 1 { + err = apiClient.Describe(tag, args[1]) + } + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + os.Exit(1) + } + }, + } + getCmd.AddCommand(tagCmd) + } } diff --git a/cmd/root.go b/cmd/root.go index f6fb9ce..86d8ac5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,9 @@ Copyright © 2024 NAME HERE package cmd import ( + "fmt" + "github.com/conduktor/ctl/client" + "github.com/conduktor/ctl/schema" "github.com/spf13/cobra" "os" ) @@ -11,6 +14,8 @@ import ( var debug *bool var key *string var cert *string +var apiClient *client.Client +var schemaClient *schema.Schema = nil // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ @@ -18,6 +23,11 @@ var rootCmd = &cobra.Command{ Short: "command line tools for conduktor", Long: `You need to define the CDK_TOKEN and CDK_BASE_URL environment variables to use this tool. You can also use the CDK_KEY,CDK_CERT instead of --key and --cert flags to use a certificate for tls authentication.`, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + if *debug { + apiClient.ActivateDebug() + } + }, // Uncomment the following line if your bare application // has an action associated with it: // Run: func(cmd *cobra.Command, args []string) { }, @@ -34,7 +44,18 @@ func Execute() { } func init() { + apiClient = client.MakeFromEnv() + openApi, err := apiClient.GetOpenApi() + if err == nil { + schemaClient, err = schema.New(openApi) + } + if err != nil { + fmt.Fprintf(os.Stderr, "Could not load server openapi: %s\n", err) + } debug = rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Show more information for debugging") key = rootCmd.PersistentFlags().String("key", "", "Set pem key for certificate authentication (useful for teleport)") cert = rootCmd.PersistentFlags().String("cert", "", "Set pem cert for certificate authentication (useful for teleport)") + initGet() + initDelete() + initApply() } diff --git a/go.mod b/go.mod index 94a3488..cc9fe67 100644 --- a/go.mod +++ b/go.mod @@ -5,14 +5,23 @@ go 1.22.0 require ( github.com/ghodss/yaml v1.0.0 github.com/go-resty/resty/v2 v2.11.0 + github.com/jarcoal/httpmock v1.3.1 + github.com/pb33f/libopenapi v0.15.14 github.com/spf13/cobra v1.8.0 gopkg.in/yaml.v3 v3.0.1 ) require ( + github.com/bahlo/generic-list-go v0.2.0 // indirect + github.com/buger/jsonparser v1.1.1 // indirect + github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/jarcoal/httpmock v1.3.1 // indirect + github.com/mailru/easyjson v0.7.7 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect + github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect + golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect golang.org/x/net v0.17.0 // indirect + golang.org/x/sync v0.6.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 5ff5ff0..10630e4 100644 --- a/go.sum +++ b/go.sum @@ -1,40 +1,141 @@ +github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= +github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= +github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= +github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dprotaso/go-yit v0.0.0-20191028211022-135eb7262960/go.mod h1:9HQzr9D/0PGwMEbC3d5AB7oi67+h4TsQqItC1GVYG58= +github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 h1:PRxIJD8XjimM5aTknUK9w6DHLDox2r2M3DI4i2pnd3w= +github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936/go.mod h1:ttYvX5qlB+mlV1okblJqcSMtR4c52UKxDiX9GRBS8+Q= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-resty/resty/v2 v2.11.0 h1:i7jMfNOJYMp69lq7qozJP+bjgzfAzeOhuGlyDrqxT/8= github.com/go-resty/resty/v2 v2.11.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww= github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g= +github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.10.2/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc= +github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= +github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c= +github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= +github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw= +github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro= +github.com/pb33f/libopenapi v0.15.14 h1:A0fn45jbthDyFGXfu5bYIZVsWyPI6hJYm3wG143MT8o= +github.com/pb33f/libopenapi v0.15.14/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= +github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk= +github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ= +github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= +github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/exp v0.0.0-20240213143201-ec583247a57a h1:HinSgX1tJRX3KsL//Gxynpw5CTOAIPhgL4W8PNiIpVE= +golang.org/x/exp v0.0.0-20240213143201-ec583247a57a/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= +golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -43,20 +144,44 @@ golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20191026110619-0b21df46bc1d/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/schema/schema.go b/schema/schema.go new file mode 100644 index 0000000..abab414 --- /dev/null +++ b/schema/schema.go @@ -0,0 +1,45 @@ +package schema + +import ( + "github.com/conduktor/ctl/utils" + "github.com/pb33f/libopenapi" + v3high "github.com/pb33f/libopenapi/datamodel/high/v3" + "golang.org/x/exp/slices" + "strings" +) + +type Schema struct { + doc *libopenapi.DocumentModel[v3high.Document] +} + +func New(schema []byte) (*Schema, error) { + doc, err := libopenapi.NewDocument(schema) + if err != nil { + return nil, err + } + v3Model, errors := doc.BuildV3Model() + if len(errors) > 0 { + return nil, errors[0] + } + + return &Schema{ + doc: v3Model, + }, nil +} + +func (s *Schema) GetKind() ([]string, error) { + result := make([]string, 0) + for path := s.doc.Model.Paths.PathItems.First(); path != nil; path = path.Next() { + if path.Value().Get != nil { + for _, tag := range path.Value().Get.Tags { + if strings.HasPrefix(tag, "self-serve-") { + newTag := utils.KebabToUpperCamel(strings.TrimPrefix(tag, "self-serve-")) + if !slices.Contains(result, newTag) { + result = append(result, newTag) + } + } + } + } + } + return result, nil +} diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..4b61b11 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,40 @@ +package utils + +import "strings" + +func UpperCamelToKebab(input string) string { + // Split the input string into words + words := make([]string, 0) + currentWord := "" + for _, char := range input { + if char >= 'A' && char <= 'Z' { + if currentWord != "" { + words = append(words, currentWord) + } + currentWord = string(char) + } else { + currentWord += string(char) + } + } + if currentWord != "" { + words = append(words, currentWord) + } + + // Join the words with hyphens + kebabCase := strings.ToLower(strings.Join(words, "-")) + + return kebabCase +} + +func KebabToUpperCamel(input string) string { + // Split the input string into words + words := strings.Split(input, "-") + + // Capitalize the first letter of each word + upperCamelCase := "" + for _, word := range words { + upperCamelCase += strings.Title(word) + } + + return upperCamelCase +} diff --git a/utils/utils_test.go b/utils/utils_test.go new file mode 100644 index 0000000..00ca4c7 --- /dev/null +++ b/utils/utils_test.go @@ -0,0 +1,63 @@ +package utils + +import ( + "testing" +) + +func TestUpperCamelToKebab(t *testing.T) { + t.Run("converts upper camel case to kebab case", func(t *testing.T) { + got := UpperCamelToKebab("UpperCamelCase") + want := "upper-camel-case" + + if got != want { + t.Errorf("got %q, want %q", got, want) + } + }) + + t.Run("handles single word", func(t *testing.T) { + got := UpperCamelToKebab("Word") + want := "word" + + if got != want { + t.Errorf("got %q, want %q", got, want) + } + }) + + t.Run("handles empty string", func(t *testing.T) { + got := UpperCamelToKebab("") + want := "" + + if got != want { + t.Errorf("got %q, want %q", got, want) + } + }) +} + +func TestKebabToUpperCamel(t *testing.T) { + t.Run("converts kebab case to upper camel case", func(t *testing.T) { + got := KebabToUpperCamel("kebab-case-to-upper") + want := "KebabCaseToUpper" + + if got != want { + t.Errorf("got %q, want %q", got, want) + } + }) + + t.Run("handles single word", func(t *testing.T) { + got := KebabToUpperCamel("word") + want := "Word" + + if got != want { + t.Errorf("got %q, want %q", got, want) + } + }) + + t.Run("handles empty string", func(t *testing.T) { + got := KebabToUpperCamel("") + want := "" + + if got != want { + t.Errorf("got %q, want %q", got, want) + } + }) +} From 6e9429d4b7dd4b5970f9d84d7f8ae3ea6129d6ae Mon Sep 17 00:00:00 2001 From: Luc DUZAN Date: Wed, 20 Mar 2024 17:09:23 +0100 Subject: [PATCH 2/2] Fail on incomplete command --- client/client.go | 2 +- cmd/get.go | 12 +++++++++--- cmd/root.go | 5 +++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/client/client.go b/client/client.go index 690dd89..cd5a993 100644 --- a/client/client.go +++ b/client/client.go @@ -135,7 +135,7 @@ func (client *Client) Delete(kind, name string) error { } func (client *Client) GetOpenApi() ([]byte, error) { - url := client.baseUrl + "public/docs/docs.yaml" + url := client.baseUrl + "/public/docs/docs.yaml" resp, err := client.client.R().Get(url) if err != nil { return nil, err diff --git a/cmd/get.go b/cmd/get.go index 5df9fa4..19c2b73 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -9,6 +9,12 @@ import ( var getCmd = &cobra.Command{ Use: "get", Short: "get resource of a given kind", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + // Root command does nothing + cmd.Help() + os.Exit(1) + }, } var getCmdWhenNoSchema = &cobra.Command{ @@ -35,17 +41,17 @@ will describe the application myapp`, } func initGet() { - rootCmd.AddCommand(getCmd) if schemaClient == nil { - getCmd.AddCommand(getCmdWhenNoSchema) + rootCmd.AddCommand(getCmdWhenNoSchema) return } tags, err := schemaClient.GetKind() if err != nil { fmt.Fprintf(os.Stderr, "Could not load kind from openapi: %s\n", err) - getCmd.AddCommand(getCmdWhenNoSchema) + rootCmd.AddCommand(getCmdWhenNoSchema) return } + rootCmd.AddCommand(getCmd) for _, tag := range tags { tagCmd := &cobra.Command{ diff --git a/cmd/root.go b/cmd/root.go index 86d8ac5..e40f985 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -28,6 +28,11 @@ You can also use the CDK_KEY,CDK_CERT instead of --key and --cert flags to use a apiClient.ActivateDebug() } }, + Run: func(cmd *cobra.Command, args []string) { + // Root command does nothing + cmd.Help() + os.Exit(1) + }, // Uncomment the following line if your bare application // has an action associated with it: // Run: func(cmd *cobra.Command, args []string) { },