Skip to content

Commit 4c88d32

Browse files
committed
refactor: use IOStreams for command output instead of os.Stdout and log
Signed-off-by: karthik balasubramanian <karthikbalasubramanian08@gmail.com>
1 parent fc3ebd1 commit 4c88d32

6 files changed

Lines changed: 14 additions & 15 deletions

File tree

pkg/shp/cmd/build/upload.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package build // nolint:revive
22

33
import (
44
"fmt"
5-
"log"
65
"os"
76
"path"
87
"time"
@@ -38,8 +37,9 @@ type UploadCommand struct {
3837

3938
sourceBundleImage string // image to be used as the source bundle
4039

41-
pw *reactor.PodWatcher // pod-watcher instance
42-
follower *follower.Follower // follower instance
40+
ioStreams *genericclioptions.IOStreams // io streams for user-facing output
41+
pw *reactor.PodWatcher // pod-watcher instance
42+
follower *follower.Follower // follower instance
4343
}
4444

4545
const (
@@ -186,7 +186,7 @@ func (u *UploadCommand) createBuildRun(p *params.Params) (*buildv1beta1.BuildRun
186186
flags.SanitizeBuildRunSpec(&br.Spec)
187187

188188
ns := p.Namespace()
189-
log.Printf("Creating a BuildRun for '%s/%s' Build...", ns, u.buildRefName)
189+
fmt.Fprintf(u.ioStreams.Out, "Creating a BuildRun for '%s/%s' Build...\n", ns, u.buildRefName)
190190
clientset, err := p.ShipwrightClientSet()
191191
if err != nil {
192192
return nil, err
@@ -197,7 +197,7 @@ func (u *UploadCommand) createBuildRun(p *params.Params) (*buildv1beta1.BuildRun
197197
if err != nil {
198198
return nil, err
199199
}
200-
log.Printf("BuildRun '%s' created!", br.GetName())
200+
fmt.Fprintf(u.ioStreams.Out, "BuildRun '%s' created!\n", br.GetName())
201201
return br, nil
202202
}
203203

@@ -207,7 +207,7 @@ func (u *UploadCommand) performDataStreaming(target *streamer.Target) error {
207207
return nil
208208
}
209209

210-
fmt.Fprintf(os.Stdout, "Streaming %q to the Build POD %q ...\n", u.sourceDir, target.Pod)
210+
fmt.Fprintf(u.ioStreams.Out, "Streaming %q to the Build POD %q ...\n", u.sourceDir, target.Pod)
211211
// creates an in-memory tarball with source directory data, and ready to start data streaming
212212
tarball, err := streamer.NewTar(u.sourceDir)
213213
if err != nil {
@@ -280,6 +280,7 @@ func (u *UploadCommand) onPodModifiedEventBundling(pod *corev1.Pod) error {
280280
// Run executes the primary business logic of this subcommand, by starting to watch over the build
281281
// pod status and react accordingly.
282282
func (u *UploadCommand) Run(p *params.Params, ioStreams *genericclioptions.IOStreams) error {
283+
u.ioStreams = ioStreams
283284
// creating a BuildRun with settings for the local source upload
284285
br, err := u.createBuildRun(p)
285286
if err != nil {

pkg/shp/cmd/buildrun/list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package buildrun
22

33
import (
44
"fmt"
5-
"os"
65
"text/tabwriter"
76
"time"
87

@@ -61,7 +60,7 @@ func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams
6160
// TODO: Support multiple output formats here, not only tabwriter
6261
// find out more in kubectl libraries and use them
6362

64-
writer := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0)
63+
writer := tabwriter.NewWriter(io.Out, 0, 8, 2, '\t', 0)
6564
columnNames := "NAME\tSTATUS\tAGE"
6665
columnTemplate := "%s\t%s\t%s\n"
6766

pkg/shp/cmd/buildstrategy/list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package buildstrategy
22

33
import (
44
"fmt"
5-
"os"
65
"text/tabwriter"
76
"time"
87

@@ -50,7 +49,7 @@ func (c *ListCommand) Validate() error {
5049

5150
// Run executes list sub-command logic
5251
func (c *ListCommand) Run(p *params.Params, io *genericclioptions.IOStreams) error {
53-
w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0)
52+
w := tabwriter.NewWriter(io.Out, 0, 8, 2, '\t', 0)
5453
if !c.noHeader {
5554
fmt.Fprintln(w, "NAME\tAGE")
5655
}

pkg/shp/cmd/clusterbuildstrategy/list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package clusterbuildstrategy
22

33
import (
44
"fmt"
5-
"os"
65
"text/tabwriter"
76
"time"
87

@@ -49,7 +48,7 @@ func (c *ListCommand) Validate() error {
4948

5049
// Run executes list sub-command logic
5150
func (c *ListCommand) Run(p *params.Params, io *genericclioptions.IOStreams) error {
52-
w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0)
51+
w := tabwriter.NewWriter(io.Out, 0, 8, 2, '\t', 0)
5352
if !c.noHeader {
5453
fmt.Fprintln(w, "NAME\tAGE")
5554
}

pkg/shp/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var rootCmd = &cobra.Command{
2525
func NewCmdSHP(ioStreams *genericclioptions.IOStreams) *cobra.Command {
2626
p := params.NewParams()
2727
p.AddFlags(rootCmd.PersistentFlags())
28-
rootCmd.AddCommand(version.Command())
28+
rootCmd.AddCommand(version.Command(ioStreams))
2929
rootCmd.AddCommand(build.Command(p, ioStreams))
3030
rootCmd.AddCommand(buildrun.Command(p, ioStreams))
3131
rootCmd.AddCommand(buildstrategy.Command(p, ioStreams))

pkg/shp/cmd/version/version.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"fmt"
55

66
"github.com/spf13/cobra"
7+
"k8s.io/cli-runtime/pkg/genericclioptions"
78
)
89

910
var version string
1011

1112
// Command returns Version subcommand of Shipwright CLI
1213
// for retrieving the shp version
13-
func Command() *cobra.Command {
14+
func Command(ioStreams *genericclioptions.IOStreams) *cobra.Command {
1415
command := &cobra.Command{
1516
Use: "version",
1617
Aliases: []string{"v"},
@@ -23,7 +24,7 @@ func Command() *cobra.Command {
2324
version = "development"
2425
}
2526

26-
fmt.Printf("version: %s\n", version)
27+
fmt.Fprintf(ioStreams.Out, "version: %s\n", version)
2728
},
2829
}
2930
return command

0 commit comments

Comments
 (0)