Skip to content

Commit

Permalink
Refactored delete.go file for customrun
Browse files Browse the repository at this point in the history
  • Loading branch information
Senjuti256 committed Aug 28, 2024
1 parent 8fa0a32 commit 7321f8e
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions pkg/cmd/customrun/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,30 @@ package customrun
import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/actions"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"go.uber.org/multierr"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func crExists(args []string, p cli.Params) ([]string, error) {
availableCrs := make([]string, 0)
func crExists(args string, p cli.Params) error {
c, err := p.Clients()
if err != nil {
return availableCrs, err
return err
}
var errorList error
ns := p.Namespace()
for _, name := range args {
var cr *v1beta1.CustomRun
err := actions.GetV1(customrunGroupResource, c, name, ns, metav1.GetOptions{}, &cr)
if err != nil {
errorList = multierr.Append(errorList, err)
if !errors.IsNotFound(err) {
fmt.Fprintf(os.Stderr, "Error checking CustomRun %s in namespace %s: %v\n", name, ns, err)
continue
}
// CustomRun not found, skip to the next
fmt.Fprintf(os.Stderr, "CustomRun %s not found in namespace %s\n", name, ns)
continue
var cr *v1beta1.CustomRun
err = actions.GetV1(customrunGroupResource, c, args, p.Namespace(), metav1.GetOptions{}, &cr)
if err != nil {
if !errors.IsNotFound(err) {
return err
}
availableCrs = append(availableCrs, name)
return fmt.Errorf("CustomRun %s not found in namespace %s", args, p.Namespace())
}
return availableCrs, nil
return nil
}

func deleteCommand(p cli.Params) *cobra.Command {
Expand Down Expand Up @@ -100,14 +88,14 @@ func deleteCustomRuns(s *cli.Stream, p cli.Params, crNames []string) error {
namespace := p.Namespace()
for _, crName := range crNames {
// Check if CustomRun exists before attempting deletion
exists, _ := crExists([]string{crName}, p)
if len(exists) == 0 {
err := crExists(crName, p)
if err != nil {
fmt.Fprintf(s.Err, "CustomRun %s not found in namespace %s\n", crName, namespace)
continue
}

// Proceed with deletion
err := deleteCustomRun(cs, namespace, crName)
err = deleteCustomRun(cs, namespace, crName)
if err == nil {
fmt.Fprintf(s.Out, "CustomRun '%s' deleted successfully from namespace '%s'\n", crName, namespace)
} else {
Expand Down

0 comments on commit 7321f8e

Please sign in to comment.