diff --git a/pkg/shp/bundle/bundle.go b/pkg/shp/bundle/bundle.go index 9d9ad717c..693eaa565 100644 --- a/pkg/shp/bundle/bundle.go +++ b/pkg/shp/bundle/bundle.go @@ -46,7 +46,7 @@ func GetSourceBundleImage(ctx context.Context, client buildclientset.Interface, // it to the given registry. For this to work, it relies on valid and working // container registry access credentials and tokens to be available in the // local system, for example logins done by `docker login` or similar. -func Push(ctx context.Context, io *genericclioptions.IOStreams, localDirectory string, targetImage string) (name.Digest, error) { +func Push(ctx context.Context, ioStreams *genericclioptions.IOStreams, localDirectory string, targetImage string) (name.Digest, error) { tag, err := name.NewTag(targetImage) if err != nil { return name.Digest{}, err @@ -80,7 +80,7 @@ func Push(ctx context.Context, io *genericclioptions.IOStreams, localDirectory s if progress == nil { progress = progressbar.NewOptions(int(update.Total), - progressbar.OptionSetWriter(io.ErrOut), + progressbar.OptionSetWriter(ioStreams.ErrOut), progressbar.OptionEnableColorCodes(true), progressbar.OptionShowBytes(true), progressbar.OptionSetWidth(15), @@ -93,7 +93,7 @@ func Push(ctx context.Context, io *genericclioptions.IOStreams, localDirectory s BarStart: "[", BarEnd: "]"}), progressbar.OptionOnCompletion(func() { - fmt.Fprintln(io.Out) + fmt.Fprintln(ioStreams.Out) }), ) defer progress.Close() @@ -108,7 +108,7 @@ func Push(ctx context.Context, io *genericclioptions.IOStreams, localDirectory s } }() - fmt.Fprintf(io.Out, "Bundling %q as %q ...\n", localDirectory, targetImage) + fmt.Fprintf(ioStreams.Out, "Bundling %q as %q ...\n", localDirectory, targetImage) digest, err := buildbundle.PackAndPush( tag, localDirectory, diff --git a/pkg/shp/cmd/build/create.go b/pkg/shp/cmd/build/create.go index c731ce26d..387649ca0 100644 --- a/pkg/shp/cmd/build/create.go +++ b/pkg/shp/cmd/build/create.go @@ -56,7 +56,7 @@ func (c *CreateCommand) Validate() error { } // Run executes the creation of a new Build instance using flags to fill up the details. -func (c *CreateCommand) Run(params *params.Params, io *genericclioptions.IOStreams) error { +func (c *CreateCommand) Run(params *params.Params, ioStreams *genericclioptions.IOStreams) error { b := &buildv1beta1.Build{ ObjectMeta: metav1.ObjectMeta{ Name: c.name, @@ -75,7 +75,7 @@ func (c *CreateCommand) Run(params *params.Params, io *genericclioptions.IOStrea // print warning with regards to source bundle image being used if b.Spec.Source.OCIArtifact != nil && b.Spec.Source.OCIArtifact.Image != "" { - fmt.Fprintf(io.Out, "Build %q uses a source bundle image, which means source code will be transferred to a container registry. It is advised to use private images to ensure the security of the source code being uploaded.\n", c.name) + fmt.Fprintf(ioStreams.Out, "Build %q uses a source bundle image, which means source code will be transferred to a container registry. It is advised to use private images to ensure the security of the source code being uploaded.\n", c.name) } } @@ -106,7 +106,7 @@ func (c *CreateCommand) Run(params *params.Params, io *genericclioptions.IOStrea if _, err := clientset.ShipwrightV1beta1().Builds(params.Namespace()).Create(c.cmd.Context(), b, metav1.CreateOptions{}); err != nil { return err } - fmt.Fprintf(io.Out, "Created build %q\n", c.name) + fmt.Fprintf(ioStreams.Out, "Created build %q\n", c.name) return nil } diff --git a/pkg/shp/cmd/build/delete.go b/pkg/shp/cmd/build/delete.go index 5a478c663..06158e69c 100644 --- a/pkg/shp/cmd/build/delete.go +++ b/pkg/shp/cmd/build/delete.go @@ -53,7 +53,7 @@ func (c *DeleteCommand) Validate() error { } // Run contains main logic of delete subcommand -func (c *DeleteCommand) Run(params *params.Params, io *genericclioptions.IOStreams) error { +func (c *DeleteCommand) Run(params *params.Params, ioStreams *genericclioptions.IOStreams) error { clientset, err := params.ShipwrightClientSet() if err != nil { return err @@ -72,12 +72,12 @@ func (c *DeleteCommand) Run(params *params.Params, io *genericclioptions.IOStrea for _, buildrun := range brList.Items { if err := clientset.ShipwrightV1beta1().BuildRuns(params.Namespace()).Delete(c.cmd.Context(), buildrun.Name, v1.DeleteOptions{}); err != nil { - fmt.Fprintf(io.ErrOut, "Error deleting BuildRun %q: %v\n", buildrun.Name, err) + fmt.Fprintf(ioStreams.ErrOut, "Error deleting BuildRun %q: %v\n", buildrun.Name, err) } } } - fmt.Fprintf(io.Out, "Build deleted %q\n", c.name) + fmt.Fprintf(ioStreams.Out, "Build deleted %q\n", c.name) return nil } diff --git a/pkg/shp/cmd/build/list.go b/pkg/shp/cmd/build/list.go index c053be735..0a49fda47 100644 --- a/pkg/shp/cmd/build/list.go +++ b/pkg/shp/cmd/build/list.go @@ -50,12 +50,12 @@ func (c *ListCommand) Validate() error { } // Run contains main logic of List subcommand of Build -func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams) error { +func (c *ListCommand) Run(params *params.Params, ioStreams *genericclioptions.IOStreams) error { // TODO: Support multiple output formats here, not only tabwriter // find out more in kubectl libraries and use them // Initialize tabwriter for command output - writer := tabwriter.NewWriter(io.Out, 0, 8, 2, '\t', 0) + writer := tabwriter.NewWriter(ioStreams.Out, 0, 8, 2, '\t', 0) columnNames := "NAME\tOUTPUT\tSTATUS" columnTemplate := "%s\t%s\t%s\n" @@ -72,7 +72,7 @@ func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams _, err = k8sclient.CoreV1().Namespaces().Get(c.cmd.Context(), params.Namespace(), metav1.GetOptions{}) if err != nil { if k8serrors.IsNotFound(err) { - fmt.Fprintf(io.Out, "Namespace '%s' not found. Please ensure that the namespace exists and try again.\n", params.Namespace()) + fmt.Fprintf(ioStreams.Out, "Namespace '%s' not found. Please ensure that the namespace exists and try again.\n", params.Namespace()) return nil } return err @@ -82,7 +82,7 @@ func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams return err } if len(buildList.Items) == 0 { - fmt.Fprintf(io.Out, "No builds found in namespace '%s'. Please create a build or verify the namespace.\n", params.Namespace()) + fmt.Fprintf(ioStreams.Out, "No builds found in namespace '%s'. Please create a build or verify the namespace.\n", params.Namespace()) return nil } diff --git a/pkg/shp/cmd/build/upload.go b/pkg/shp/cmd/build/upload.go index 7abe3dd3e..f75bea9b1 100644 --- a/pkg/shp/cmd/build/upload.go +++ b/pkg/shp/cmd/build/upload.go @@ -2,7 +2,6 @@ package build // nolint:revive import ( "fmt" - "log" "os" "path" "time" @@ -38,8 +37,9 @@ type UploadCommand struct { sourceBundleImage string // image to be used as the source bundle - pw *reactor.PodWatcher // pod-watcher instance - follower *follower.Follower // follower instance + ioStreams *genericclioptions.IOStreams // io streams for user-facing output + pw *reactor.PodWatcher // pod-watcher instance + follower *follower.Follower // follower instance } const ( @@ -100,7 +100,9 @@ func (u *UploadCommand) extractArgs(args []string) error { } // Complete instantiate the dependencies for the log following and the data streaming. -func (u *UploadCommand) Complete(p *params.Params, _ *genericclioptions.IOStreams, args []string) error { +func (u *UploadCommand) Complete(p *params.Params, ioStreams *genericclioptions.IOStreams, args []string) error { + u.ioStreams = ioStreams + // extracting the command-line arguments to store the build-name and the path to the directory // to be uploaded, in subsequent steps if err := u.extractArgs(args); err != nil { @@ -186,7 +188,7 @@ func (u *UploadCommand) createBuildRun(p *params.Params) (*buildv1beta1.BuildRun flags.SanitizeBuildRunSpec(&br.Spec) ns := p.Namespace() - log.Printf("Creating a BuildRun for '%s/%s' Build...", ns, u.buildRefName) + fmt.Fprintf(u.ioStreams.Out, "Creating a BuildRun for '%s/%s' Build...\n", ns, u.buildRefName) clientset, err := p.ShipwrightClientSet() if err != nil { return nil, err @@ -197,7 +199,7 @@ func (u *UploadCommand) createBuildRun(p *params.Params) (*buildv1beta1.BuildRun if err != nil { return nil, err } - log.Printf("BuildRun '%s' created!", br.GetName()) + fmt.Fprintf(u.ioStreams.Out, "BuildRun '%s' created!\n", br.GetName()) return br, nil } @@ -207,7 +209,7 @@ func (u *UploadCommand) performDataStreaming(target *streamer.Target) error { return nil } - fmt.Fprintf(os.Stdout, "Streaming %q to the Build POD %q ...\n", u.sourceDir, target.Pod) + fmt.Fprintf(u.ioStreams.Out, "Streaming %q to the Build POD %q ...\n", u.sourceDir, target.Pod) // creates an in-memory tarball with source directory data, and ready to start data streaming tarball, err := streamer.NewTar(u.sourceDir) if err != nil { diff --git a/pkg/shp/cmd/buildrun/list.go b/pkg/shp/cmd/buildrun/list.go index a1034d71a..7940b10a2 100644 --- a/pkg/shp/cmd/buildrun/list.go +++ b/pkg/shp/cmd/buildrun/list.go @@ -2,7 +2,6 @@ package buildrun import ( "fmt" - "os" "text/tabwriter" "time" @@ -57,11 +56,11 @@ func (c *ListCommand) Validate() error { } // Run executes list sub-command logic -func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams) error { +func (c *ListCommand) Run(params *params.Params, ioStreams *genericclioptions.IOStreams) error { // TODO: Support multiple output formats here, not only tabwriter // find out more in kubectl libraries and use them - writer := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0) + writer := tabwriter.NewWriter(ioStreams.Out, 0, 8, 2, '\t', 0) columnNames := "NAME\tSTATUS\tAGE" columnTemplate := "%s\t%s\t%s\n" @@ -77,7 +76,7 @@ func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams _, err = k8sclient.CoreV1().Namespaces().Get(c.cmd.Context(), params.Namespace(), metav1.GetOptions{}) if err != nil { if k8serrors.IsNotFound(err) { - fmt.Fprintf(io.Out, "Namespace '%s' not found. Please ensure that the namespace exists and try again.\n", params.Namespace()) + fmt.Fprintf(ioStreams.Out, "Namespace '%s' not found. Please ensure that the namespace exists and try again.\n", params.Namespace()) return nil } return err @@ -88,7 +87,7 @@ func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams return err } if len(brs.Items) == 0 { - fmt.Fprintf(io.Out, "No buildruns found in namespace '%s'. Please create a buildrun or verify the namespace.\n", params.Namespace()) + fmt.Fprintf(ioStreams.Out, "No buildruns found in namespace '%s'. Please create a buildrun or verify the namespace.\n", params.Namespace()) return nil } diff --git a/pkg/shp/cmd/buildstrategy/buildstrategy.go b/pkg/shp/cmd/buildstrategy/buildstrategy.go index e0a9a9df4..efc0cf760 100644 --- a/pkg/shp/cmd/buildstrategy/buildstrategy.go +++ b/pkg/shp/cmd/buildstrategy/buildstrategy.go @@ -9,7 +9,7 @@ import ( ) // Command represents "shp buildstrategy". -func Command(p *params.Params, io *genericclioptions.IOStreams) *cobra.Command { +func Command(p *params.Params, ioStreams *genericclioptions.IOStreams) *cobra.Command { cmd := &cobra.Command{ Use: "buildstrategy", Aliases: []string{"bs"}, @@ -20,8 +20,8 @@ func Command(p *params.Params, io *genericclioptions.IOStreams) *cobra.Command { } cmd.AddCommand( - runner.NewRunner(p, io, listCmd()).Cmd(), - runner.NewRunner(p, io, deleteCmd()).Cmd(), + runner.NewRunner(p, ioStreams, listCmd()).Cmd(), + runner.NewRunner(p, ioStreams, deleteCmd()).Cmd(), ) return cmd diff --git a/pkg/shp/cmd/buildstrategy/delete.go b/pkg/shp/cmd/buildstrategy/delete.go index 7d50e04f6..3e341c5fb 100644 --- a/pkg/shp/cmd/buildstrategy/delete.go +++ b/pkg/shp/cmd/buildstrategy/delete.go @@ -40,7 +40,7 @@ func (c *DeleteCommand) Complete(_ *params.Params, _ *genericclioptions.IOStream func (c *DeleteCommand) Validate() error { return nil } // Run executes delete sub-command logic -func (c *DeleteCommand) Run(p *params.Params, io *genericclioptions.IOStreams) error { +func (c *DeleteCommand) Run(p *params.Params, ioStreams *genericclioptions.IOStreams) error { cs, err := p.ShipwrightClientSet() if err != nil { return err @@ -50,6 +50,6 @@ func (c *DeleteCommand) Run(p *params.Params, io *genericclioptions.IOStreams) e Delete(c.cmd.Context(), c.name, metav1.DeleteOptions{}); err != nil { return err } - fmt.Fprintf(io.Out, "BuildStrategy deleted '%s'\n", c.name) + fmt.Fprintf(ioStreams.Out, "BuildStrategy deleted '%s'\n", c.name) return nil } diff --git a/pkg/shp/cmd/buildstrategy/list.go b/pkg/shp/cmd/buildstrategy/list.go index ffeeb44a3..0cdbdb098 100644 --- a/pkg/shp/cmd/buildstrategy/list.go +++ b/pkg/shp/cmd/buildstrategy/list.go @@ -2,7 +2,6 @@ package buildstrategy import ( "fmt" - "os" "text/tabwriter" "time" @@ -49,8 +48,8 @@ func (c *ListCommand) Validate() error { } // Run executes list sub-command logic -func (c *ListCommand) Run(p *params.Params, io *genericclioptions.IOStreams) error { - w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0) +func (c *ListCommand) Run(p *params.Params, ioStreams *genericclioptions.IOStreams) error { + w := tabwriter.NewWriter(ioStreams.Out, 0, 8, 2, '\t', 0) if !c.noHeader { fmt.Fprintln(w, "NAME\tAGE") } @@ -67,7 +66,7 @@ func (c *ListCommand) Run(p *params.Params, io *genericclioptions.IOStreams) err ns := p.Namespace() if _, err = k8s.CoreV1().Namespaces().Get(c.cmd.Context(), ns, metav1.GetOptions{}); err != nil { if k8serrors.IsNotFound(err) { - fmt.Fprintf(io.Out, "Namespace '%s' not found.\n", ns) + fmt.Fprintf(ioStreams.Out, "Namespace '%s' not found.\n", ns) return nil } return err @@ -78,7 +77,7 @@ func (c *ListCommand) Run(p *params.Params, io *genericclioptions.IOStreams) err return err } if len(list.Items) == 0 { - fmt.Fprintf(io.Out, "No BuildStrategies found in namespace '%s'.\n", ns) + fmt.Fprintf(ioStreams.Out, "No BuildStrategies found in namespace '%s'.\n", ns) return nil } diff --git a/pkg/shp/cmd/clusterbuildstrategy/clusterbuildstrategy.go b/pkg/shp/cmd/clusterbuildstrategy/clusterbuildstrategy.go index ee16c6bc2..24732edbf 100644 --- a/pkg/shp/cmd/clusterbuildstrategy/clusterbuildstrategy.go +++ b/pkg/shp/cmd/clusterbuildstrategy/clusterbuildstrategy.go @@ -9,7 +9,7 @@ import ( ) // Command represents "shp clusterbuildstrategy". -func Command(p *params.Params, io *genericclioptions.IOStreams) *cobra.Command { +func Command(p *params.Params, ioStreams *genericclioptions.IOStreams) *cobra.Command { cmd := &cobra.Command{ Use: "clusterbuildstrategy", Aliases: []string{"cbs"}, @@ -20,8 +20,8 @@ func Command(p *params.Params, io *genericclioptions.IOStreams) *cobra.Command { } cmd.AddCommand( - runner.NewRunner(p, io, listCmd()).Cmd(), - runner.NewRunner(p, io, deleteCmd()).Cmd(), + runner.NewRunner(p, ioStreams, listCmd()).Cmd(), + runner.NewRunner(p, ioStreams, deleteCmd()).Cmd(), ) return cmd diff --git a/pkg/shp/cmd/clusterbuildstrategy/delete.go b/pkg/shp/cmd/clusterbuildstrategy/delete.go index 40ef8dafb..250db9303 100644 --- a/pkg/shp/cmd/clusterbuildstrategy/delete.go +++ b/pkg/shp/cmd/clusterbuildstrategy/delete.go @@ -40,7 +40,7 @@ func (c *DeleteCommand) Complete(_ *params.Params, _ *genericclioptions.IOStream func (c *DeleteCommand) Validate() error { return nil } // Run executes delete sub-command logic -func (c *DeleteCommand) Run(p *params.Params, io *genericclioptions.IOStreams) error { +func (c *DeleteCommand) Run(p *params.Params, ioStreams *genericclioptions.IOStreams) error { cs, err := p.ShipwrightClientSet() if err != nil { return err @@ -50,6 +50,6 @@ func (c *DeleteCommand) Run(p *params.Params, io *genericclioptions.IOStreams) e Delete(c.cmd.Context(), c.name, metav1.DeleteOptions{}); err != nil { return err } - fmt.Fprintf(io.Out, "ClusterBuildStrategy deleted '%s'\n", c.name) + fmt.Fprintf(ioStreams.Out, "ClusterBuildStrategy deleted '%s'\n", c.name) return nil } diff --git a/pkg/shp/cmd/clusterbuildstrategy/list.go b/pkg/shp/cmd/clusterbuildstrategy/list.go index 740a99d26..d81835074 100644 --- a/pkg/shp/cmd/clusterbuildstrategy/list.go +++ b/pkg/shp/cmd/clusterbuildstrategy/list.go @@ -2,7 +2,6 @@ package clusterbuildstrategy import ( "fmt" - "os" "text/tabwriter" "time" @@ -48,8 +47,8 @@ func (c *ListCommand) Validate() error { } // Run executes list sub-command logic -func (c *ListCommand) Run(p *params.Params, io *genericclioptions.IOStreams) error { - w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0) +func (c *ListCommand) Run(p *params.Params, ioStreams *genericclioptions.IOStreams) error { + w := tabwriter.NewWriter(ioStreams.Out, 0, 8, 2, '\t', 0) if !c.noHeader { fmt.Fprintln(w, "NAME\tAGE") } @@ -63,7 +62,7 @@ func (c *ListCommand) Run(p *params.Params, io *genericclioptions.IOStreams) err return err } if len(list.Items) == 0 { - fmt.Fprintln(io.Out, "No ClusterBuildStrategies found.") + fmt.Fprintln(ioStreams.Out, "No ClusterBuildStrategies found.") return nil } diff --git a/pkg/shp/cmd/root.go b/pkg/shp/cmd/root.go index 0fc189cca..30b4d85e0 100644 --- a/pkg/shp/cmd/root.go +++ b/pkg/shp/cmd/root.go @@ -25,7 +25,7 @@ var rootCmd = &cobra.Command{ func NewCmdSHP(ioStreams *genericclioptions.IOStreams) *cobra.Command { p := params.NewParams() p.AddFlags(rootCmd.PersistentFlags()) - rootCmd.AddCommand(version.Command()) + rootCmd.AddCommand(version.Command(ioStreams)) rootCmd.AddCommand(build.Command(p, ioStreams)) rootCmd.AddCommand(buildrun.Command(p, ioStreams)) rootCmd.AddCommand(buildstrategy.Command(p, ioStreams)) diff --git a/pkg/shp/cmd/version/version.go b/pkg/shp/cmd/version/version.go index f66c8070a..6becd4701 100644 --- a/pkg/shp/cmd/version/version.go +++ b/pkg/shp/cmd/version/version.go @@ -4,13 +4,14 @@ import ( "fmt" "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericclioptions" ) var version string // Command returns Version subcommand of Shipwright CLI // for retrieving the shp version -func Command() *cobra.Command { +func Command(ioStreams *genericclioptions.IOStreams) *cobra.Command { command := &cobra.Command{ Use: "version", Aliases: []string{"v"}, @@ -23,7 +24,7 @@ func Command() *cobra.Command { version = "development" } - fmt.Printf("version: %s\n", version) + fmt.Fprintf(ioStreams.Out, "version: %s\n", version) }, } return command