Skip to content

Commit

Permalink
improved error messaging for Nonexistent namespaces,builds and buildr…
Browse files Browse the repository at this point in the history
…uns in shipwright cli

Signed-off-by: adarsh-jaiss <[email protected]>
  • Loading branch information
Adarsh-jaiss committed Sep 9, 2024
1 parent 6e2f07b commit fe51c90
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 14 additions & 0 deletions pkg/shp/cmd/build/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,23 @@ 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 {
return fmt.Errorf("namespace %s not found. Please ensure that the namespace exists and try again", params.Namespace())
}

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())
return nil
}

if !c.noHeader {
fmt.Fprintln(writer, columnNames)
Expand Down
15 changes: 14 additions & 1 deletion pkg/shp/cmd/buildrun/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,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

Expand All @@ -67,10 +67,23 @@ 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 {
return fmt.Errorf("namespace %s not found. Please ensure that the namespace exists and try again", params.Namespace())
}

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 initiate a buildrun or verify the namespace.\n", params.Namespace())
return nil
}

if !c.noHeader {
fmt.Fprintln(writer, columnNames)
Expand Down

0 comments on commit fe51c90

Please sign in to comment.