Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/shp/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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()
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions pkg/shp/cmd/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/shp/cmd/build/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions pkg/shp/cmd/build/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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
}

Expand Down
16 changes: 9 additions & 7 deletions pkg/shp/cmd/build/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package build // nolint:revive

import (
"fmt"
"log"
"os"
"path"
"time"
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions pkg/shp/cmd/buildrun/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package buildrun

import (
"fmt"
"os"
"text/tabwriter"
"time"

Expand Down Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/shp/cmd/buildstrategy/buildstrategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/shp/cmd/buildstrategy/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
9 changes: 4 additions & 5 deletions pkg/shp/cmd/buildstrategy/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package buildstrategy

import (
"fmt"
"os"
"text/tabwriter"
"time"

Expand Down Expand Up @@ -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")
}
Expand All @@ -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
Expand All @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/shp/cmd/clusterbuildstrategy/clusterbuildstrategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/shp/cmd/clusterbuildstrategy/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
7 changes: 3 additions & 4 deletions pkg/shp/cmd/clusterbuildstrategy/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package clusterbuildstrategy

import (
"fmt"
"os"
"text/tabwriter"
"time"

Expand Down Expand Up @@ -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")
}
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/shp/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 3 additions & 2 deletions pkg/shp/cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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
Expand Down