diff --git a/pkg/shp/cmd/build/list.go b/pkg/shp/cmd/build/list.go index 2dd5c647a..9e0bebe9f 100644 --- a/pkg/shp/cmd/build/list.go +++ b/pkg/shp/cmd/build/list.go @@ -9,6 +9,7 @@ import ( "github.com/shipwright-io/cli/pkg/shp/params" "github.com/spf13/cobra" + k8serrors "k8s.io/apimachinery/pkg/api/errors" // Import the k8serrors package metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/cli-runtime/pkg/genericclioptions" ) @@ -63,9 +64,27 @@ func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams if err != nil { return err } + + k8sclient, err := params.ClientSet() + if err != nil { + return fmt.Errorf("failed to get k8s client: %w", err) + } + _, 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()) + return nil + } + return err + } + if buildList, err = clientset.ShipwrightV1alpha1().Builds(params.Namespace()).List(c.cmd.Context(), metav1.ListOptions{}); err != nil { 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()) + return nil + } if !c.noHeader { fmt.Fprintln(writer, columnNames) diff --git a/pkg/shp/cmd/buildrun/list.go b/pkg/shp/cmd/buildrun/list.go index 92fb65b72..7edc6ee0c 100644 --- a/pkg/shp/cmd/buildrun/list.go +++ b/pkg/shp/cmd/buildrun/list.go @@ -7,6 +7,7 @@ import ( "time" "github.com/spf13/cobra" + k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/duration" @@ -54,7 +55,7 @@ func (c *ListCommand) Validate() error { } // Run executes list sub-command logic -func (c *ListCommand) Run(params *params.Params, _ *genericclioptions.IOStreams) error { +func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams) error { // TODO: Support multiple output formats here, not only tabwriter // find out more in kubectl libraries and use them @@ -67,10 +68,27 @@ func (c *ListCommand) Run(params *params.Params, _ *genericclioptions.IOStreams) return err } + k8sclient, err := params.ClientSet() + if err != nil { + return fmt.Errorf("failed to get k8s client: %w", err) + } + _, 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()) + return nil + } + return err + } + var brs *buildv1alpha1.BuildRunList if brs, err = clientset.ShipwrightV1alpha1().BuildRuns(params.Namespace()).List(c.cmd.Context(), metav1.ListOptions{}); err != nil { 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()) + return nil + } if !c.noHeader { fmt.Fprintln(writer, columnNames)