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

Stdin support #30

Merged
merged 1 commit into from
Jan 23, 2020
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 22 additions & 3 deletions ksort.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ file that was distributed with this source code.
package ksort

import (
"bufio"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down
31 changes: 30 additions & 1 deletion ksort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,6 +57,7 @@ metadata:
},
{
args: []string{"--filename", "testdata"},
in: "",
out: `# Source: testdata/configmap.yaml
apiVersion: v1
kind: ConfigMap
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -160,6 +164,7 @@ metadata:
},
{
args: []string{"--recursive", "--filename", "testdata"},
in: "",
out: `# Source: testdata/test-recursive/ns.yaml
apiVersion: v1
kind: Namespace
Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions testdata/configmap.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
Expand Down
1 change: 1 addition & 0 deletions testdata/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
Expand Down