diff --git a/Makefile b/Makefile index faeccbf..fda4a4a 100644 --- a/Makefile +++ b/Makefile @@ -29,3 +29,7 @@ endif .PHONY: test test: go test ./... -test.v + +.PHONY: fmt +fmt: + go fmt ./... \ No newline at end of file diff --git a/kustomize/kustomize.go b/kustomize/kustomize.go index fa7a92c..18e0474 100644 --- a/kustomize/kustomize.go +++ b/kustomize/kustomize.go @@ -17,18 +17,21 @@ package kustomize import ( "bytes" "fmt" + "io/ioutil" + "path/filepath" + "regexp" + osruntime "runtime" + "strings" + "sync" + "github.com/TomOnTime/utfutil" "github.com/flanksource/commons/logger" "github.com/pkg/errors" log "github.com/sirupsen/logrus" - "io/ioutil" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" yamlutil "k8s.io/apimachinery/pkg/util/yaml" "k8s.io/cli-runtime/pkg/kustomize" - "path/filepath" - "regexp" - osruntime "runtime" "sigs.k8s.io/kustomize/pkg/constants" "sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/ifc" @@ -36,8 +39,6 @@ import ( "sigs.k8s.io/kustomize/pkg/patch" "sigs.k8s.io/kustomize/pkg/types" "sigs.k8s.io/yaml" - "strings" - "sync" ) // Manager define a manager that allow access to kustomize capabilities @@ -283,11 +284,11 @@ func (km *Manager) Kustomize(namespace string, objects ...runtime.Object) ([]run func GetUnstructuredObjects(data []byte) ([]runtime.Object, error) { utfData, err := BytesToUtf8Lf(data) - if err != nil { - return nil, fmt.Errorf("error converting to UTF %s", err) + if err != nil { + return nil, fmt.Errorf("error converting to UTF %v", err) } var items []runtime.Object - re := regexp.MustCompile("(?m)^---\\n") + re := regexp.MustCompile(`(?m)^---\n`) for _, chunk := range re.Split(utfData, -1) { if strings.TrimSpace(chunk) == "" { continue @@ -309,16 +310,15 @@ func GetUnstructuredObjects(data []byte) ([]runtime.Object, error) { func BytesToUtf8Lf(file []byte) (string, error) { decoded := utfutil.BytesReader(file, utfutil.UTF8) buf := new(bytes.Buffer) - bytesRead, err := buf.ReadFrom(decoded) + _, err := buf.ReadFrom(decoded) if err != nil { - logger.Errorf("error reading from buffer. bytesRead %v. err: %v", bytesRead, err) + logger.Errorf("error reading from buffer: %v", err) return "", err } val := buf.Bytes() // replace \r with \n -> solves for Mac but leaves \n\n for Windows - val = bytes.Replace(val, []byte{13}, []byte{10}, -1) + val = bytes.ReplaceAll(val, []byte{13}, []byte{10}) // replace \n\n with \n - val = bytes.Replace(val, []byte{10, 10}, []byte{10}, -1) + val = bytes.ReplaceAll(val, []byte{10, 10}, []byte{10}) return string(val), nil } - diff --git a/kustomize/kustomize_test.go b/kustomize/kustomize_test.go index 7c5be0f..557adef 100644 --- a/kustomize/kustomize_test.go +++ b/kustomize/kustomize_test.go @@ -17,7 +17,6 @@ import ( "testing" ) - type UTFWriteCloser interface { Write(p []byte) (n int, err error) Close() error @@ -68,13 +67,13 @@ func bytesWriter(b *bytes.Buffer, d utfutil.EncodingHint) io.Writer { func bytesToCrlfOrCr(file []byte, os string, hint utfutil.EncodingHint) (string, error) { val := file - val = bytes.Replace(val, []byte{13}, []byte{10}, -1) - val = bytes.Replace(val, []byte{10, 10}, []byte{10}, -1) + val = bytes.ReplaceAll(val, []byte{13}, []byte{10}) + val = bytes.ReplaceAll(val, []byte{10, 10}, []byte{10}) switch os { case "macos": - val = bytes.Replace(val, []byte{10}, []byte{13}, -1) + val = bytes.ReplaceAll(val, []byte{10}, []byte{13}) default: - val = bytes.Replace(val, []byte{10}, []byte{13, 10}, -1) + val = bytes.ReplaceAll(val, []byte{10}, []byte{13, 10}) } var buf bytes.Buffer writer := bytesWriter(&buf, hint) @@ -101,7 +100,6 @@ func getFileBuffer(filePath string) ([]byte, error) { return buf, nil } - func TestBytesToUtf8Lf(t *testing.T) { // If tests are breaking, first check that the file read is UTF-8 with LF endings @@ -121,7 +119,7 @@ func TestBytesToUtf8Lf(t *testing.T) { t.Errorf("error setting up test TestBytesToUtf8Lf: %v", err) return } - utf16BeCrMac, err := bytesToCrlfOrCr(data, "macos", utfutil.UTF16BE) + utf16BeCrMac, err := bytesToCrlfOrCr(data, "macos", utfutil.UTF16BE) if err != nil { t.Errorf("error setting up test TestBytesToUtf8Lf: %v", err) return @@ -204,13 +202,13 @@ func TestGetUnstructuredObjects(t *testing.T) { Object: map[string]interface{}{ "apiVersion": "v1", "data": map[string]interface{}{ - "logging.level.org.springframework": "DEBUG", + "logging.level.org.springframework": "DEBUG", "logging.level.org.springframework.web": "INFO", - "some-key": "value-from-spring", + "some-key": "value-from-spring", }, "kind": "ConfigMap", "metadata": map[string]interface{}{ - "name": "spring-defaults-spring", + "name": "spring-defaults-spring", "namespace": "default", }, }, @@ -220,12 +218,12 @@ func TestGetUnstructuredObjects(t *testing.T) { Object: map[string]interface{}{ "apiVersion": "v1", "data": map[string]interface{}{ - "application.properties": "some-key=new-value\nnew-key=diff-value\n", + "application.properties": "some-key=new-value\nnew-key=diff-value\n", }, "kind": "ConfigMap", "metadata": map[string]interface{}{ - "name": "sample", - "namespace": "default", + "name": "sample", + "namespace": "default", }, }, } @@ -246,7 +244,7 @@ func TestGetUnstructuredObjects(t *testing.T) { t.Errorf("error setting up test TestBytesToUtf8Lf: %v", err) return } - utf16BeCrMac, err := bytesToCrlfOrCr(data, "macos", utfutil.UTF16BE) + utf16BeCrMac, err := bytesToCrlfOrCr(data, "macos", utfutil.UTF16BE) if err != nil { t.Errorf("error setting up test TestBytesToUtf8Lf: %v", err) return @@ -257,61 +255,61 @@ func TestGetUnstructuredObjects(t *testing.T) { return } - items := []runtime.Object{ itemOne, itemTwo } + items := []runtime.Object{itemOne, itemTwo} type args struct { data []byte } tests := []struct { - name string - args args - want []runtime.Object + name string + args args + want []runtime.Object testIndexes []int - wantErr bool + wantErr bool }{ { name: "f(data)->[]runtime.Object{ ...items }", args: args{ data: data, }, - want: items, + want: items, testIndexes: []int{0, 1}, - wantErr: false, + wantErr: false, }, { name: "f(utf16LeCrMac)->[]runtime.Object{ ...items }", args: args{ data: []byte(utf16LeCrMac), }, - want: items, + want: items, testIndexes: []int{0, 1}, - wantErr: false, + wantErr: false, }, { name: "f(utf16BeCrMac)->[]runtime.Object{ ...items }", args: args{ data: []byte(utf16BeCrMac), }, - want: items, + want: items, testIndexes: []int{0, 1}, - wantErr: false, + wantErr: false, }, { name: "f(utf16LeCrlfDefault)->[]runtime.Object{ ...items }", args: args{ data: []byte(utf16LeCrlfDefault), }, - want: items, + want: items, testIndexes: []int{0, 1}, - wantErr: false, + wantErr: false, }, { name: "f(utf16BeCrlfDefault)->[]runtime.Object{ ...items }", args: args{ data: []byte(utf16BeCrlfDefault), }, - want: items, + want: items, testIndexes: []int{0, 1}, - wantErr: false, + wantErr: false, }, } for _, tt := range tests { @@ -329,4 +327,4 @@ func TestGetUnstructuredObjects(t *testing.T) { } }) } -} \ No newline at end of file +} diff --git a/utils.go b/utils.go index f457748..4ec6eac 100644 --- a/utils.go +++ b/utils.go @@ -746,4 +746,4 @@ func HasTaint(node v1.Node, name string) bool { } } return false -} \ No newline at end of file +}