refactor: [cmd] thinking in output

上级 b8d317e2
...@@ -42,7 +42,7 @@ func executeActionCommandC(cmd string) (*cobra.Command, string, error) { ...@@ -42,7 +42,7 @@ func executeActionCommandC(cmd string) (*cobra.Command, string, error) {
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
rootCmd.SetOutput(buf) rootCmd.SetOut(buf)
rootCmd.SetArgs(args) rootCmd.SetArgs(args)
c, err := rootCmd.ExecuteC() c, err := rootCmd.ExecuteC()
......
...@@ -2,9 +2,11 @@ package cmd ...@@ -2,9 +2,11 @@ package cmd
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"os"
) )
var ( var (
output = os.Stdout
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "coca", Use: "coca",
Short: "A generator for Cobra based Applications", Short: "A generator for Cobra based Applications",
...@@ -13,6 +15,6 @@ var ( ...@@ -13,6 +15,6 @@ var (
) )
func Execute() error { func Execute() error {
rootCmd.SetOut(output)
return rootCmd.Execute() return rootCmd.Execute()
} }
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
) )
func init() { func init() {
versionCmd.SetOut(output)
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)
} }
...@@ -14,6 +15,6 @@ var versionCmd = &cobra.Command{ ...@@ -14,6 +15,6 @@ var versionCmd = &cobra.Command{
Use: "version", Use: "version",
Short: "version", Short: "version",
Run: func(cmd *cobra.Command, args []string) { 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
...@@ -8,7 +8,7 @@ func TestVersion(t *testing.T) { ...@@ -8,7 +8,7 @@ func TestVersion(t *testing.T) {
tests := []cmdTestCase{{ tests := []cmdTestCase{{
name: "version", name: "version",
cmd: "version", cmd: "version",
golden: "", golden: "testdata/version.txt",
}} }}
runTestCmd(t, tests) runTestCmd(t, tests)
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册