diff --git a/cmd/af/cmd/hello.go b/cmd/af/cmd/hello.go new file mode 100644 index 0000000..6a1b6ab --- /dev/null +++ b/cmd/af/cmd/hello.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var helloCmd = &cobra.Command{ + Use: "hello", + Short: "Print a greeting", + Run: func(cmd *cobra.Command, args []string) { + fmt.Fprintln(cmd.OutOrStdout(), "hello world") + }, +} + +func init() { + rootCmd.AddCommand(helloCmd) +} diff --git a/cmd/af/cmd/hello_test.go b/cmd/af/cmd/hello_test.go new file mode 100644 index 0000000..c8afa93 --- /dev/null +++ b/cmd/af/cmd/hello_test.go @@ -0,0 +1,20 @@ +package cmd + +import ( + "bytes" + "testing" +) + +func TestHelloCmdPrintsHelloWorld(t *testing.T) { + var buf bytes.Buffer + helloCmd.SetOut(&buf) + t.Cleanup(func() { helloCmd.SetOut(nil) }) + + helloCmd.Run(helloCmd, nil) + + got := buf.String() + want := "hello world\n" + if got != want { + t.Fatalf("hello output = %q, want %q", got, want) + } +}