diff --git a/pkg/kando/process_client_create.go b/pkg/kando/process_client_create.go index 5d56018d2e..d4e85cfe90 100644 --- a/pkg/kando/process_client_create.go +++ b/pkg/kando/process_client_create.go @@ -16,6 +16,7 @@ package kando import ( "fmt" + "io" "github.com/spf13/cobra" "google.golang.org/protobuf/encoding/protojson" @@ -34,6 +35,10 @@ func newProcessClientCreateCommand() *cobra.Command { } func runProcessClientCreate(cmd *cobra.Command, args []string) error { + return runProcessClientCreateWithOutput(cmd.OutOrStdout(), cmd, args) +} + +func runProcessClientCreateWithOutput(out io.Writer, cmd *cobra.Command, args []string) error { addr, err := processAddressFlagValue(cmd) if err != nil { return err @@ -41,11 +46,14 @@ func runProcessClientCreate(cmd *cobra.Command, args []string) error { asJSON := processAsJSONFlagValue(cmd) cmd.SilenceUsage = true p, err := kanx.CreateProcess(cmd.Context(), addr, args[0], args[1:]) - if !asJSON { - fmt.Printf("Created process: %v\n", p) - return err + if asJSON { + buf, err := protojson.Marshal(p) + if err != nil { + return err + } + fmt.Fprintln(out, string(buf)) + } else { + fmt.Fprintln(out, "Process: ", p.String()) } - buf, err := protojson.Marshal(p) - fmt.Println(string(buf)) return err } diff --git a/pkg/kando/process_client_get.go b/pkg/kando/process_client_get.go index d6059e60a3..bbe7d33fb6 100644 --- a/pkg/kando/process_client_get.go +++ b/pkg/kando/process_client_get.go @@ -15,9 +15,8 @@ package kando import ( - "context" "fmt" - "os" + "io" "strconv" "github.com/spf13/cobra" @@ -37,7 +36,10 @@ func newProcessClientGetCommand() *cobra.Command { } func runProcessClientGet(cmd *cobra.Command, args []string) error { - cmd.SetContext(context.Background()) + return runProcessClientGetWithOutput(cmd.OutOrStdout(), cmd, args) +} + +func runProcessClientGetWithOutput(out io.Writer, cmd *cobra.Command, args []string) error { pid, err := strconv.Atoi(args[0]) if err != nil { return err @@ -52,7 +54,6 @@ func runProcessClientGet(cmd *cobra.Command, args []string) error { if err != nil { return err } - out := os.Stdout if asJSON { buf, err := protojson.Marshal(p) if err != nil { diff --git a/pkg/kando/process_client_list.go b/pkg/kando/process_client_list.go index d6e3bbf51d..5101bc1ce1 100644 --- a/pkg/kando/process_client_list.go +++ b/pkg/kando/process_client_list.go @@ -17,7 +17,6 @@ package kando import ( "fmt" "io" - "os" "github.com/spf13/cobra" "google.golang.org/protobuf/encoding/protojson" @@ -36,7 +35,7 @@ func newProcessClientListCommand() *cobra.Command { } func runProcessClientList(cmd *cobra.Command, _args []string) error { - return runProcessClientListWithOutput(os.Stdout, cmd) + return runProcessClientListWithOutput(cmd.OutOrStdout(), cmd) } func runProcessClientListWithOutput(out io.Writer, cmd *cobra.Command) error { diff --git a/pkg/kando/process_client_output.go b/pkg/kando/process_client_output.go index df12827ace..0017bcc20d 100644 --- a/pkg/kando/process_client_output.go +++ b/pkg/kando/process_client_output.go @@ -15,8 +15,7 @@ package kando import ( - "context" - "os" + "io" "strconv" "github.com/spf13/cobra" @@ -35,7 +34,10 @@ func newProcessClientOutputCommand() *cobra.Command { } func runProcessClientOutput(cmd *cobra.Command, args []string) error { - cmd.SetContext(context.Background()) + return runProcessClientOutputWithOutput(cmd.OutOrStdout(), cmd, args) +} + +func runProcessClientOutputWithOutput(out io.Writer, cmd *cobra.Command, args []string) error { pid, err := strconv.Atoi(args[0]) if err != nil { return err @@ -45,5 +47,5 @@ func runProcessClientOutput(cmd *cobra.Command, args []string) error { return err } cmd.SilenceUsage = true - return kanx.Stdout(cmd.Context(), addr, int64(pid), os.Stdout) + return kanx.Stdout(cmd.Context(), addr, int64(pid), out) } diff --git a/pkg/kando/process_client_signal.go b/pkg/kando/process_client_signal.go index d803fa4cec..8fdddd8fc9 100644 --- a/pkg/kando/process_client_signal.go +++ b/pkg/kando/process_client_signal.go @@ -17,7 +17,6 @@ package kando import ( "fmt" "io" - "os" "strconv" "github.com/spf13/cobra" @@ -37,7 +36,7 @@ func newProcessClientSignalCommand() *cobra.Command { } func runProcessClientSignal(cmd *cobra.Command, args []string) error { - return runProcessClientSignalWithOutput(os.Stdout, cmd, args) + return runProcessClientSignalWithOutput(cmd.OutOrStdout(), cmd, args) } func runProcessClientSignalWithOutput(out io.Writer, cmd *cobra.Command, args []string) error {