From dead0aebd06e17bad4f14777c77b8a9183202dff Mon Sep 17 00:00:00 2001 From: Kazuki Suda Date: Thu, 23 Jan 2020 23:12:57 +0900 Subject: [PATCH] Stdin support --- README.md | 5 +++++ ksort.go | 25 ++++++++++++++++++++++--- ksort_test.go | 31 ++++++++++++++++++++++++++++++- testdata/configmap.yaml | 1 + testdata/deployment.yaml | 1 + 5 files changed, 59 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bdf723f..f3986b5 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,11 @@ Sort manifests contained the manifest file that is specified. $ ksort -f ./app.yaml ``` +Sort manifests passed into stdin. +``` +$ cat app.yaml | ksort -f- +``` + ## Installation You can download an archive file from [GitHub Releases](https://github.com/superbrothers/ksort/releases), then extract it and install a binary. diff --git a/ksort.go b/ksort.go index 63324d4..41a69bb 100644 --- a/ksort.go +++ b/ksort.go @@ -8,6 +8,7 @@ file that was distributed with this source code. package ksort import ( + "bufio" "errors" "flag" "fmt" @@ -40,9 +41,12 @@ using SortByKind function in Kubernetes Helm.` # Sort manifests contained the manifest file. ksort -f app.yaml - + # Sort manifests in uninstall order. - ksort -f ./manifests --delete` + ksort -f ./manifests --delete + + # Sort manifests passed into stdin. + cat app.yaml | ksort -f -` kindUnknown = "Unknown" ) @@ -132,6 +136,18 @@ func (o *options) run() error { contents := map[string]string{} for _, filename := range o.filenameOptions.Filenames { + if filename == "-" { + klog.V(2).Infof("Reading manifest from the standard input") + + var lines []string + scanner := bufio.NewScanner(o.In) + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + contents[""] = strings.Join(lines, "\n") + continue + } + klog.V(2).Infof("Walking the file tree rooted at %q", filename) err := filepath.Walk(filename, func(path string, info os.FileInfo, err error) error { @@ -202,7 +218,10 @@ func (o *options) run() error { i = len(manifests) - (i + 1) } - a[i] += fmt.Sprintf("# Source: %s\n", m.Name) + // If manifest data is read from stdin, m.Name is empty + if m.Name != "" { + a[i] += fmt.Sprintf("# Source: %s\n", m.Name) + } if m.Head.Kind == kindUnknown { a[i] += "# WARNING: It looks like that this file is not a manifest file\n" diff --git a/ksort_test.go b/ksort_test.go index 8fa0de2..251031c 100644 --- a/ksort_test.go +++ b/ksort_test.go @@ -24,10 +24,12 @@ func TestPrintVersionInformation(t *testing.T) { func TestCommand(t *testing.T) { tests := []struct { args []string + in string out string }{ { args: []string{"--filename", "testdata/rbac.yaml"}, + in: "", out: `# Source: testdata/rbac.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole @@ -55,6 +57,7 @@ metadata: }, { args: []string{"--filename", "testdata"}, + in: "", out: `# Source: testdata/configmap.yaml apiVersion: v1 kind: ConfigMap @@ -94,6 +97,7 @@ metadata: }, { args: []string{"--filename", "testdata/deployment.yaml", "--filename", "testdata/rbac.yaml"}, + in: "", out: `# Source: testdata/rbac.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole @@ -160,6 +164,7 @@ metadata: }, { args: []string{"--recursive", "--filename", "testdata"}, + in: "", out: `# Source: testdata/test-recursive/ns.yaml apiVersion: v1 kind: Namespace @@ -201,12 +206,36 @@ apiVersion: apps/v1 kind: Deployment metadata: name: deployment +`, + }, + { + args: []string{"--filename", "-"}, + in: `apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: ClusterRoleBinding +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: ClusterRole +`, + out: `apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: ClusterRole +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: ClusterRoleBinding `, }, } for i, tt := range tests { - streams, _, out, _ := genericclioptions.NewTestIOStreams() + streams, in, out, _ := genericclioptions.NewTestIOStreams() + in.WriteString(tt.in) cmd := NewCommand(streams) cmd.SetArgs(tt.args) diff --git a/testdata/configmap.yaml b/testdata/configmap.yaml index c0de517..349eeff 100644 --- a/testdata/configmap.yaml +++ b/testdata/configmap.yaml @@ -1,3 +1,4 @@ +--- apiVersion: v1 kind: ConfigMap metadata: diff --git a/testdata/deployment.yaml b/testdata/deployment.yaml index a3d2e3b..584716f 100644 --- a/testdata/deployment.yaml +++ b/testdata/deployment.yaml @@ -1,3 +1,4 @@ +--- apiVersion: apps/v1 kind: Deployment metadata: