Skip to content

Commit

Permalink
Fix to print version information with version flag (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
superbrothers authored May 22, 2019
1 parent 15e79e4 commit 763a2d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
7 changes: 2 additions & 5 deletions ksort.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ import (
"k8s.io/klog"
)

var (
printVersion = false
)

const (
ksortLong = `When installing manifests, they should be sorted in a proper order by Kind.
For example, Namespace object must be in the first place when installing them.
Expand Down Expand Up @@ -79,6 +75,8 @@ func newOptions(streams genericclioptions.IOStreams) *options {
func NewCommand(streams genericclioptions.IOStreams) *cobra.Command {
o := newOptions(streams)

printVersion := false

cmd := &cobra.Command{
Use: "ksort -f FILENAME",
Short: "ksort sorts manfest files in a proper order by Kind.",
Expand All @@ -105,7 +103,6 @@ func NewCommand(streams genericclioptions.IOStreams) *cobra.Command {
}

o.filenameFlags.AddFlags(cmd.Flags())
cmd.MarkFlagRequired("filename")
cmd.Flags().BoolVarP(&o.delete, "delete", "d", o.delete, "Sort manifests in uninstall order")
cmd.Flags().BoolVar(&printVersion, "version", printVersion, "Print the version and exit")
cmd.Flags().AddGoFlagSet(flag.CommandLine)
Expand Down
15 changes: 15 additions & 0 deletions ksort_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
package ksort

import (
"strings"
"testing"

"k8s.io/cli-runtime/pkg/genericclioptions"
)

func TestPrintVersionInformation(t *testing.T) {
streams, _, _, errOut := genericclioptions.NewTestIOStreams()
cmd := NewCommand(streams)
cmd.SetArgs([]string{"--version"})

if err := cmd.Execute(); err != nil {
t.Errorf("cmd.Execute() => %q", err)
}

if !strings.Contains(errOut.String(), "&ksort.info{") {
t.Errorf("expect to include version information, but got %q", errOut)
}
}

func TestCommand(t *testing.T) {
tests := []struct {
args []string
Expand Down

0 comments on commit 763a2d7

Please sign in to comment.