Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use KebabCase in path #17

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package client
import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/conduktor/ctl/printutils"
"github.com/conduktor/ctl/resource"
"github.com/go-resty/resty/v2"
"os"
)

type Client struct {
Expand Down Expand Up @@ -43,7 +45,7 @@ type UpsertResponse struct {
}

func (client *Client) Apply(resource *resource.Resource, dryMode bool) (string, error) {
url := client.baseUrl + "/" + resource.Kind
url := client.baseUrl + "/" + UpperCamelToKebab(resource.Kind)
builder := client.client.R().SetBody(resource.Json)
if dryMode {
builder = builder.SetQueryParam("dryMode", "true")
Expand All @@ -53,7 +55,7 @@ func (client *Client) Apply(resource *resource.Resource, dryMode bool) (string,
return "", err
}
if resp.IsError() {
return "", fmt.Errorf("Error applying resource %s/%s, got status code: %d:\n %s", resource.Kind, resource.Name, resp.StatusCode(), string(resp.Body()))
return "", fmt.Errorf("error applying resource %s/%s, got status code: %d:\n %s", resource.Kind, resource.Name, resp.StatusCode(), string(resp.Body()))
}
bodyBytes := resp.Body()
var upsertResponse UpsertResponse
Expand Down Expand Up @@ -84,21 +86,21 @@ func printResponseAsYaml(bytes []byte) error {
}

func (client *Client) Get(kind string) error {
url := client.baseUrl + "/" + kind
url := client.baseUrl + "/" + UpperCamelToKebab(kind)
resp, err := client.client.R().Get(url)
if resp.IsError() {
return fmt.Errorf("Error listing resources of kind %s, got status code: %d:\n %s", kind, resp.StatusCode(), string(resp.Body()))
return fmt.Errorf("error listing resources of kind %s, got status code: %d:\n %s", kind, resp.StatusCode(), string(resp.Body()))
}
if err != nil {
return err
}
return printResponseAsYaml(resp.Body())
}
func (client *Client) Describe(kind, name string) error {
url := client.baseUrl + "/" + kind + "/" + name
url := client.baseUrl + "/" + 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()))
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
Expand All @@ -107,13 +109,37 @@ func (client *Client) Describe(kind, name string) error {
}

func (client *Client) Delete(kind, name string) error {
url := client.baseUrl + "/" + kind + "/" + name
url := client.baseUrl + "/" + UpperCamelToKebab(kind) + "/" + name
resp, err := client.client.R().Delete(url)
if resp.IsError() {
return fmt.Errorf("Error deleting resources %s/%s, got status code: %d:\n %s", kind, name, resp.StatusCode(), string(resp.Body()))
return fmt.Errorf("error deleting resources %s/%s, got status code: %d:\n %s", kind, name, resp.StatusCode(), string(resp.Body()))
} else {
fmt.Printf("%s/%s deleted\n", kind, name)
}

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)
}

// Join the words with hyphens
kebabCase := strings.ToLower(strings.Join(words, "-"))

return kebabCase
}
75 changes: 65 additions & 10 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package client

import (
"testing"

"github.com/conduktor/ctl/resource"
"github.com/jarcoal/httpmock"
"testing"
)

func TestApplyShouldWork(t *testing.T) {
Expand All @@ -18,7 +19,7 @@ func TestApplyShouldWork(t *testing.T) {

topic := resource.Resource{
Json: []byte(`{"yolo": "data"}`),
Kind: "topic",
Kind: "Topic",
Name: "toto",
Version: "v1",
}
Expand Down Expand Up @@ -53,7 +54,7 @@ func TestApplyWithDryModeShouldWork(t *testing.T) {

topic := resource.Resource{
Json: []byte(`{"yolo": "data"}`),
Kind: "topic",
Kind: "Topic",
Name: "toto",
Version: "v1",
}
Expand Down Expand Up @@ -91,7 +92,7 @@ func TestApplyShouldFailIfNo2xx(t *testing.T) {

topic := resource.Resource{
Json: []byte(`{"yolo": "data"}`),
Kind: "topic",
Kind: "Topic",
Name: "toto",
Version: "v1",
}
Expand Down Expand Up @@ -132,7 +133,61 @@ func TestGetShouldWork(t *testing.T) {
responder,
)

err = client.Get("application")
err = client.Get("Application")
if err != nil {
t.Error(err)
}
}

func TestGetShouldApplyCaseTransformation(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
responder, err := httpmock.NewJsonResponder(200, "[]")
if err != nil {
panic(err)
}

httpmock.RegisterMatcherResponderWithQuery(
"GET",
"http://baseUrl/api/application-instance",
nil,
httpmock.HeaderIs("Authorization", "Bearer "+token),
responder,
)

err = client.Get("ApplicationInstance")
if err != nil {
t.Error(err)
}
}

func TestGetShouldKeepCase(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
responder, err := httpmock.NewJsonResponder(200, "[]")
if err != nil {
panic(err)
}

httpmock.RegisterMatcherResponderWithQuery(
"GET",
"http://baseUrl/api/application-instance",
nil,
httpmock.HeaderIs("Authorization", "Bearer "+token),
responder,
)

err = client.Get("application-instance")
if err != nil {
t.Error(err)
}
Expand All @@ -159,7 +214,7 @@ func TestGetShouldFailIfN2xx(t *testing.T) {
responder,
)

err = client.Get("application")
err = client.Get("Application")
if err == nil {
t.Failed()
}
Expand All @@ -186,7 +241,7 @@ func TestDescribeShouldWork(t *testing.T) {
responder,
)

err = client.Describe("application", "yo")
err = client.Describe("Application", "yo")
if err != nil {
t.Error(err)
}
Expand All @@ -213,7 +268,7 @@ func TestDescribeShouldFailIfNo2xx(t *testing.T) {
responder,
)

err = client.Describe("application", "yo")
err = client.Describe("Application", "yo")
if err == nil {
t.Failed()
}
Expand All @@ -240,7 +295,7 @@ func TestDeleteShouldWork(t *testing.T) {
responder,
)

err = client.Delete("application", "yo")
err = client.Delete("Application", "yo")
if err != nil {
t.Error(err)
}
Expand All @@ -266,7 +321,7 @@ func TestDeleteShouldFailOnNot2XX(t *testing.T) {
responder,
)

err = client.Delete("application", "yo")
err = client.Delete("Application", "yo")
if err == nil {
t.Fail()
}
Expand Down
7 changes: 4 additions & 3 deletions printutils/printYaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package printutils

import (
"fmt"
yaml "gopkg.in/yaml.v3"
"io"
"slices"

yaml "gopkg.in/yaml.v3"
)

func printKeyYaml(w io.Writer, key string, data interface{}) error {
Expand Down Expand Up @@ -50,9 +51,9 @@ func printResource(w io.Writer, data interface{}) error {
// take a interface that can be a resource or multiple resource
// and print it as the content of a file we could use for an apply
func PrintResourceLikeYamlFile(w io.Writer, data interface{}) error {
switch data.(type) {
switch dataType := data.(type) {
case []interface{}:
for _, d := range data.([]interface{}) {
for _, d := range dataType {
fmt.Fprintln(w, "---")
err := printResource(w, d)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions printutils/printYaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestPrintResourceLikeYamlOnSingleResource(t *testing.T) {
resourceFromBe := `{"spec": "someSpec", "version": "v4", "kind": "gelato", "metadata": "arancia"}`
resourceFromBe := `{"spec": "someSpec", "version": "v4", "kind": "Gelato", "metadata": "arancia"}`
var data interface{}
err := json.Unmarshal([]byte(resourceFromBe), &data)
if err != nil {
Expand All @@ -18,7 +18,7 @@ func TestPrintResourceLikeYamlOnSingleResource(t *testing.T) {
PrintResourceLikeYamlFile(&output, data)
expected := strings.TrimSpace(`
version: v4
kind: gelato
kind: Gelato
metadata: arancia
spec: someSpec`)
got := strings.TrimSpace(output.String())
Expand Down Expand Up @@ -52,7 +52,7 @@ cat`)
}

func TestPrintResourceLikeYamlOnMultileResources(t *testing.T) {
resourceFromBe := `{"spec": "someSpec", "version": "v4", "newKind": "gelato", "metadata": "arancia"}`
resourceFromBe := `{"spec": "someSpec", "version": "v4", "newKind": "Gelato", "metadata": "arancia"}`
var data interface{}
err := json.Unmarshal([]byte(resourceFromBe), &data)
if err != nil {
Expand All @@ -64,7 +64,7 @@ func TestPrintResourceLikeYamlOnMultileResources(t *testing.T) {
version: v4
metadata: arancia
spec: someSpec
newKind: gelato
newKind: Gelato
`)
got := strings.TrimSpace(output.String())
if got != expected {
Expand Down
5 changes: 3 additions & 2 deletions resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"encoding/json"
"fmt"
yamlJson "github.com/ghodss/yaml"
yaml "gopkg.in/yaml.v3"
"io"
"os"
"path/filepath"
"strings"

yamlJson "github.com/ghodss/yaml"
yaml "gopkg.in/yaml.v3"
)

type Resource struct {
Expand Down
Loading