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

add -k #12

Merged
merged 3 commits into from
Feb 28, 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
3 changes: 3 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func MakeFromEnv(debug bool) Client {
func (client *Client) Apply(resource *resource.Resource) (string, error) {
url := client.baseUrl + "/" + resource.Kind
resp, err := client.client.R().SetBody(resource.Json).Put(url)
if err != nil {
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()))
}
Expand Down
38 changes: 32 additions & 6 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ import (
"os"
)

var filePath *string
var filePath *[]string

// applyCmd represents the apply command
var applyCmd = &cobra.Command{
Use: "apply",
Short: "upsert a resource on Conduktor",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
resources, error := resource.FromFile(*filePath)
if error != nil {
fmt.Fprintf(os.Stderr, "%s\n", error)
os.Exit(1)
var resources []resource.Resource = make([]resource.Resource, 0)
for _, path := range *filePath {
r, err := resourceForPath(path)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
resources = append(resources, r...)
}
client := client.MakeFromEnv(*debug)
for _, resource := range resources {
Expand All @@ -34,12 +38,34 @@ var applyCmd = &cobra.Command{
},
}

func resourceForPath(path string) ([]resource.Resource, error) {
directory, err := isDirectory(path)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
if directory {
return resource.FromFolder(path)
} else {
return resource.FromFile(path)
}
}

func init() {
rootCmd.AddCommand(applyCmd)

// Here you will define your flags and configuration settings.
filePath = applyCmd.
PersistentFlags().StringP("file", "f", "", "Specify the file to apply")
PersistentFlags().StringArrayP("file", "f", make([]string, 0, 0), "Specify the files to apply")

applyCmd.MarkPersistentFlagRequired("file")
}

func isDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}

return fileInfo.IsDir(), err
}
21 changes: 21 additions & 0 deletions resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
yaml "gopkg.in/yaml.v3"
"io"
"os"
"path/filepath"
"strings"
)

type Resource struct {
Expand Down Expand Up @@ -40,6 +42,25 @@ func FromFile(path string) ([]Resource, error) {
return FromByte(data)
}

func FromFolder(path string) ([]Resource, error) {
dirEntry, err := os.ReadDir(path)
if err != nil {
return nil, err
}
var result = make([]Resource, 0, 0)
for _, entry := range dirEntry {
if !entry.IsDir() && (strings.HasSuffix(entry.Name(), ".yml") || strings.HasSuffix(entry.Name(), ".yaml")) {
resources, err := FromFile(filepath.Join(path, entry.Name()))
result = append(result, resources...)
if err != nil {
return nil, err
}
}
}

return result, nil
}

func FromByte(data []byte) ([]Resource, error) {
reader := bytes.NewReader(data)
var yamlData interface{}
Expand Down
92 changes: 53 additions & 39 deletions resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ import (
"testing"
)

func checkResource(t *testing.T, result, expected Resource) {
if result.Name != expected.Name {
t.Errorf("Expected name %s got %s", expected.Name, result.Name)
}

if result.Kind != expected.Kind {
t.Errorf("Expected kind %s got %s", expected.Kind, result.Kind)
}

if result.Version != expected.Version {
t.Errorf("Expected version %s got %s", expected.Version, result.Version)
}

if string(result.Json) != string(expected.Json) {
t.Errorf("Expected json:\n%s got\n%s", string(expected.Json), string(result.Json))
}
}

func TestFromByteForOneResourceWithValidResource(t *testing.T) {
yamlByte := []byte(`
# comment
Expand All @@ -29,59 +47,55 @@ metadata:
t.Errorf("results expected1 of length 2, got length %d", len(results))
}

result1 := results[0]
expected1 := Resource{
checkResource(t, results[0], Resource{
Version: "v1",
Kind: "Topic",
Name: "abc.myTopic",
Json: []byte(`{"kind":"Topic","metadata":{"name":"abc.myTopic"},"spec":{"replicationFactor":1},"version":"v1"}`),
}

if result1.Name != expected1.Name {
t.Errorf("Expected name %s got %s", expected1.Name, result1.Name)
}
})

if result1.Kind != expected1.Kind {
t.Errorf("Expected name %s got %s", expected1.Kind, result1.Kind)
}

if result1.Version != expected1.Version {
t.Errorf("Expected name %s got %s", expected1.Version, result1.Version)
}

expectedJsonString1 := string(expected1.Json)
resultJsonString1 := string(result1.Json)
if expectedJsonString1 != resultJsonString1 {
t.Errorf("\nExpected json:\n%s got:\n%s", expectedJsonString1, resultJsonString1)
}

result2 := results[1]
expected2 := Resource{
checkResource(t, results[1], Resource{
Version: "v2",
Kind: "ConsumerGroup",
Name: "cg1",
Json: []byte(`{"kind":"ConsumerGroup","metadata":{"name":"cg1"},"version":"v2"}`),
}
})
}

if result2.Name != expected2.Name {
t.Errorf("Expected name %s got %s", expected2.Name, result2.Name)
func TestFromFolder(t *testing.T) {
resources, err := FromFolder("yamls")
if err != nil {
t.Fatal(err)
}

if result2.Kind != expected2.Kind {
t.Errorf("Expected name %s got %s", expected2.Kind, result2.Kind)
if len(resources) != 4 {
t.Fatalf("Expected to read 4 resources, readed %d", len(resources))
}

if result2.Version != expected2.Version {
t.Errorf("Expected name %s got %s", expected2.Version, result2.Version)
}
checkResource(t, resources[0], Resource{
Version: "v1",
Kind: "a",
Name: "a",
Json: []byte(`{"kind":"a","metadata":{"name":"a"},"spec":{"data":"data"},"version":"v1"}`),
})

expectedJsonString2 := string(expected2.Json)
resultJsonString2 := string(result2.Json)
if expectedJsonString2 != resultJsonString2 {
t.Errorf("\nExpected json:\n%s got:\n%s", expectedJsonString2, resultJsonString2)
}
}
checkResource(t, resources[1], Resource{
Version: "v1",
Kind: "a",
Name: "b",
Json: []byte(`{"kind":"a","metadata":{"name":"b"},"spec":{"data":"data2"},"version":"v1"}`),
})

func TestFromByte(t *testing.T) {
checkResource(t, resources[2], Resource{
Version: "v1",
Kind: "b",
Name: "a",
Json: []byte(`{"kind":"b","metadata":{"name":"a"},"spec":{"data":"yo"},"version":"v1"}`),
})

checkResource(t, resources[3], Resource{
Version: "v1",
Kind: "b",
Name: "b",
Json: []byte(`{"kind":"b","metadata":{"name":"b"},"spec":{"data":"lo"},"version":"v1"}`),
})
}
14 changes: 14 additions & 0 deletions resource/yamls/a.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
version: v1
kind: a
metadata:
name: a
spec:
data: data
---
version: v1
kind: a
metadata:
name: b
spec:
data: data2
14 changes: 14 additions & 0 deletions resource/yamls/b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
version: v1
kind: b
metadata:
name: a
spec:
data: yo
---
version: v1
kind: b
metadata:
name: b
spec:
data: lo
6 changes: 6 additions & 0 deletions resource/yamls/other.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: v1
kind: other
metadata:
name: other
spec:
data: other
Loading