Skip to content

Commit

Permalink
test: D: Fixup usage of Cobra APIs in preparation for command line te…
Browse files Browse the repository at this point in the history
…sting. (#3326)

Signed-off-by: Aaron Alpar <[email protected]>
  • Loading branch information
aaron-kasten authored Jan 14, 2025
1 parent 64845d4 commit 9dd5e00
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
18 changes: 13 additions & 5 deletions pkg/kando/process_client_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package kando

import (
"fmt"
"io"

"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
Expand All @@ -34,18 +35,25 @@ 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
}
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
}
9 changes: 5 additions & 4 deletions pkg/kando/process_client_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
package kando

import (
"context"
"fmt"
"os"
"io"
"strconv"

"github.com/spf13/cobra"
Expand All @@ -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
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/kando/process_client_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package kando
import (
"fmt"
"io"
"os"

"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
Expand All @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions pkg/kando/process_client_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
package kando

import (
"context"
"os"
"io"
"strconv"

"github.com/spf13/cobra"
Expand All @@ -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
Expand All @@ -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)
}
3 changes: 1 addition & 2 deletions pkg/kando/process_client_signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package kando
import (
"fmt"
"io"
"os"
"strconv"

"github.com/spf13/cobra"
Expand All @@ -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 {
Expand Down

0 comments on commit 9dd5e00

Please sign in to comment.