From b813d8e0a2c2305ac77f7102b75a0a4458818102 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Mon, 6 Jan 2020 13:49:58 +0800 Subject: [PATCH] refactor: [cmd] thinking in output --- cmd/coca_test.go | 2 +- cmd/root.go | 4 +++- cmd/testdata/version.txt | 0 cmd/version.go | 3 ++- cmd/version_test.go | 2 +- 5 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 cmd/testdata/version.txt diff --git a/cmd/coca_test.go b/cmd/coca_test.go index 059bf9d..1a4e85c 100644 --- a/cmd/coca_test.go +++ b/cmd/coca_test.go @@ -42,7 +42,7 @@ func executeActionCommandC(cmd string) (*cobra.Command, string, error) { } buf := new(bytes.Buffer) - rootCmd.SetOutput(buf) + rootCmd.SetOut(buf) rootCmd.SetArgs(args) c, err := rootCmd.ExecuteC() diff --git a/cmd/root.go b/cmd/root.go index 5c2c643..8d1549a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,9 +2,11 @@ package cmd import ( "github.com/spf13/cobra" + "os" ) var ( + output = os.Stdout rootCmd = &cobra.Command{ Use: "coca", Short: "A generator for Cobra based Applications", @@ -13,6 +15,6 @@ var ( ) func Execute() error { + rootCmd.SetOut(output) return rootCmd.Execute() } - diff --git a/cmd/testdata/version.txt b/cmd/testdata/version.txt new file mode 100644 index 0000000..e69de29 diff --git a/cmd/version.go b/cmd/version.go index 0fd66b7..38e353b 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -7,6 +7,7 @@ import ( ) func init() { + versionCmd.SetOut(output) rootCmd.AddCommand(versionCmd) } @@ -14,6 +15,6 @@ var versionCmd = &cobra.Command{ Use: "version", Short: "version", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Coca Version: " + config.VERSION + " -- HEAD") + fmt.Fprintf(output, "Coca Version: " + config.VERSION + " -- HEAD \n") }, } \ No newline at end of file diff --git a/cmd/version_test.go b/cmd/version_test.go index ad28fbb..cc2165e 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -8,7 +8,7 @@ func TestVersion(t *testing.T) { tests := []cmdTestCase{{ name: "version", cmd: "version", - golden: "", + golden: "testdata/version.txt", }} runTestCmd(t, tests) } \ No newline at end of file -- GitLab