diff --git a/.gitignore b/.gitignore index e60aea8..3418cb0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ files/ yamls.yaml split-kube-yamls -split-kube-yamls.exe \ No newline at end of file +split-kube-yamls.exe +*/__debug_bin* \ No newline at end of file diff --git a/helpers/helpers.go b/helpers/helpers.go index acd5be3..72b3e35 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -29,9 +29,20 @@ func splitStr(s string) []string { } for _, str := range strArr { var each string + if strings.Contains(str, "kind: List") { + continue + } for _, s := range strings.Split(str, "\n") { each += strings.TrimPrefix(s, " ") + "\n" } + found := true + beforeStr := "" + for found { + beforeStr, found = strings.CutSuffix(each, "\n") + if found { + each = beforeStr + } + } result = append(result, "apiVersion:"+each) } } else { @@ -55,39 +66,12 @@ func GetYamlKindName(y string) (string, string, error) { return strings.ToLower(r.Kind), r.Metadata.Name, nil } -func GetKindAndNameFromYaml(y string) (string, string) { - kind := "" - name := "" - - s := bufio.NewScanner(strings.NewReader(y)) - for s.Scan() { - if s.Text() == "" { - break - } - s := s.Text() - b, a, found := strings.Cut(s, ":") - if found && strings.Trim(strings.ToLower(b), " ") == "kind" { - kind = strings.Trim(a, " ") - } - if found && strings.Trim(strings.ToLower(b), " ") == "name" { - name = strings.Trim(a, " ") - } - if kind != "" && name != "" { - break - } - } - - return kind, name - -} - func ReadStdin() []string { s := bufio.NewScanner(os.Stdin) var str string var l []string for s.Scan() { str += "\n" + s.Text() - } l = splitStr(str) diff --git a/helpers/helpers_test.go b/helpers/helpers_test.go new file mode 100644 index 0000000..e069a14 --- /dev/null +++ b/helpers/helpers_test.go @@ -0,0 +1,244 @@ +package helpers + +import ( + "reflect" + "testing" +) + +func TestSplitStr(t *testing.T) { + type args struct { + s string + } + //t1 + t1_input := args{`apiVersion: apps/v1 +kind: Deployment +metadata: + creationTimestamp: null + labels: + app: test-app + name: test-app +spec: + replicas: 1 + selector: + matchLabels: + app: test-app + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + app: test-app + spec: + containers: + - image: test-app:v1.0 + name: test-app + resources: {} + ports: + - containerPort: 8080 + protocol: TCP + name: api + volumeMounts: + - mountPath: /go/bin/confFile + name: config + volumes: + - name: config + configMap: + name: api-config + items: + - key: config.yaml + path: config.yaml +status: {} +--- +apiVersion: v1 +kind: Service +metadata: + creationTimestamp: null + labels: + app: test-app + name: test-app +spec: + ports: + - port: 8080 + protocol: TCP + targetPort: 8080 + selector: + app: test-app +status: + loadBalancer: {}`, + } + t1_expected := []string{`apiVersion: apps/v1 +kind: Deployment +metadata: + creationTimestamp: null + labels: + app: test-app + name: test-app +spec: + replicas: 1 + selector: + matchLabels: + app: test-app + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + app: test-app + spec: + containers: + - image: test-app:v1.0 + name: test-app + resources: {} + ports: + - containerPort: 8080 + protocol: TCP + name: api + volumeMounts: + - mountPath: /go/bin/confFile + name: config + volumes: + - name: config + configMap: + name: api-config + items: + - key: config.yaml + path: config.yaml +status: {}`, `apiVersion: v1 +kind: Service +metadata: + creationTimestamp: null + labels: + app: test-app + name: test-app +spec: + ports: + - port: 8080 + protocol: TCP + targetPort: 8080 + selector: + app: test-app +status: + loadBalancer: {}`} + + //t2 + t2_input := args{`apiVersion: v1 +kind: List +items: +- apiVersion: v1 + kind: Service + metadata: + name: list-service-test + spec: + ports: + - protocol: TCP + port: 80 + selector: + app: list-deployment-test +- apiVersion: apps/v1 + kind: Deployment + metadata: + name: list-deployment-test + labels: + app: list-deployment-test + spec: + replicas: 1 + selector: + matchLabels: + app: list-deployment-test + template: + metadata: + labels: + app: list-deployment-test + spec: + containers: + - name: nginx + image: nginx`} + t2_expected := []string{`apiVersion: v1 +kind: Service +metadata: + name: list-service-test +spec: + ports: + - protocol: TCP + port: 80 + selector: + app: list-deployment-test`, `apiVersion: apps/v1 +kind: Deployment +metadata: + name: list-deployment-test + labels: + app: list-deployment-test +spec: + replicas: 1 + selector: + matchLabels: + app: list-deployment-test + template: + metadata: + labels: + app: list-deployment-test + spec: + containers: + - name: nginx + image: nginx`} + + tests := []struct { + name string + args args + want []string + }{ + {"t1_Does split into two parts", t1_input, t1_expected}, + {"t2_Does split List into two parts", t2_input, t2_expected}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := splitStr(tt.args.s); !reflect.DeepEqual(got, tt.want) { + t.Errorf("splitStr() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestGetYamlKindName(t *testing.T) { + type args struct { + y string + } + t1_input := `apiVersion: v1 +kind: Service +metadata: + name: list-service-test +spec: + ports: + - protocol: TCP + port: 80 + selector: + app: list-deployment-test` + t1_expected_kind := "service" + t1_expected_name := "list-service-test" + + tests := []struct { + name string + args args + want string + want1 string + wantErr bool + }{ + {"returns kind list and name list-service-test", args{t1_input}, t1_expected_kind, t1_expected_name, false}, + {"returns err when no yaml supplied", args{"bogus yaml string"}, "", "", true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, got1, err := GetYamlKindName(tt.args.y) + if (err != nil) != tt.wantErr { + t.Errorf("GetYamlKindName() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("GetYamlKindName() got = %v, want %v", got, tt.want) + } + if got1 != tt.want1 { + t.Errorf("GetYamlKindName() got1 = %v, want %v", got1, tt.want1) + } + }) + } +} diff --git a/main.go b/main.go index 0f0978e..b673ac9 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ func main() { if tO == "" { fmt.Printf("Output dir not supplied, specify with `-o path/to/dir`, quitting.\n") - os.Exit(2) + os.Exit(1) } else { //read os-specific path to os-agnostic path o = filepath.ToSlash(tO) @@ -47,7 +47,7 @@ func main() { c, err := helpers.ReadYamlFileToStringArr(fn) if err != nil { fmt.Printf("unable to read file %v, quitting\n", fn) - os.Exit(2) + os.Exit(3) } y = c } @@ -62,7 +62,7 @@ func main() { if len(y) < 2 { fmt.Printf("no yaml supplied\n") - os.Exit(2) + os.Exit(4) } for _, str := range y { @@ -84,5 +84,6 @@ func main() { fmt.Printf("failed to write file %v, error: %v\n", file, werr) } } + os.Exit(0) }