From d87bf818b56a1c7c9374c6a6508f0d6d49f9215d Mon Sep 17 00:00:00 2001 From: adarsh-jaiss Date: Tue, 10 Sep 2024 13:10:05 +0530 Subject: [PATCH] added suggested changes Signed-off-by: adarsh-jaiss --- pkg/shp/cmd/build/list.go | 11 +++++++++-- pkg/shp/cmd/buildrun/list.go | 12 ++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pkg/shp/cmd/build/list.go b/pkg/shp/cmd/build/list.go index f0012c62c..0cf4d689c 100644 --- a/pkg/shp/cmd/build/list.go +++ b/pkg/shp/cmd/build/list.go @@ -1,3 +1,4 @@ + package build import ( @@ -9,6 +10,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" ) @@ -70,14 +72,19 @@ 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 { - return fmt.Errorf("namespace %s not found. Please ensure that the namespace exists and try again", params.Namespace()) + 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 + } else { + 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 initiate a build or verify the namespace.\n", params.Namespace()) + fmt.Fprintf(io.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/buildrun/list.go b/pkg/shp/cmd/buildrun/list.go index 7410b4bf9..ce0786450 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" @@ -73,7 +74,12 @@ 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 { - return fmt.Errorf("namespace %s not found. Please ensure that the namespace exists and try again", params.Namespace()) + 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 + } else { + return err + } } var brs *buildv1alpha1.BuildRunList @@ -81,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 initiate a buildrun or verify the namespace.\n", params.Namespace()) + fmt.Fprintf(io.Out, "No buildruns found in namespace '%s'. Please create a buildrun or verify the namespace.\n", params.Namespace()) return nil } @@ -105,3 +111,5 @@ func (c *ListCommand) Run(params *params.Params, io *genericclioptions.IOStreams return writer.Flush() } + +