|
| 1 | +package repository |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/cli" |
| 8 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/tknpac/completion" |
| 9 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/params" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | +) |
| 13 | + |
| 14 | +const longHelp = ` |
| 15 | +Delete a Pipelines as Code Repository or multiple of them |
| 16 | +
|
| 17 | +eg: |
| 18 | + tkn pac repository delete <repository-name> <repository-name2> |
| 19 | + ` |
| 20 | + |
| 21 | +func DeleteCommand(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command { |
| 22 | + var repository string |
| 23 | + var cascade bool |
| 24 | + cmd := &cobra.Command{ |
| 25 | + Args: cobra.MinimumNArgs(0), |
| 26 | + Use: "delete", |
| 27 | + Short: "Delete a Pipelines as Code Repository or multiple of them", |
| 28 | + Long: longHelp, |
| 29 | + Aliases: []string{"rm"}, |
| 30 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 31 | + var err error |
| 32 | + opts := cli.NewCliOptions(cmd) |
| 33 | + opts.Namespace, err = cmd.Flags().GetString(namespaceFlag) |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + ctx := context.Background() |
| 38 | + err = run.Clients.NewClients(ctx, &run.Info) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + if len(args) == 0 { |
| 43 | + return fmt.Errorf("repository name is required") |
| 44 | + } |
| 45 | + if opts.Namespace == "" { |
| 46 | + opts.Namespace = run.Info.Kube.Namespace |
| 47 | + } |
| 48 | + return repodelete(ctx, run, args, opts, ioStreams, cascade) |
| 49 | + }, |
| 50 | + Annotations: map[string]string{ |
| 51 | + "commandType": "main", |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + cmd.Flags().StringP( |
| 56 | + namespaceFlag, "n", "", "If present, the namespace scope for this CLI request") |
| 57 | + |
| 58 | + cmd.Flags().BoolVarP( |
| 59 | + &cascade, "cascade", "c", false, "Delete the repository and its secrets attached to it") |
| 60 | + cmd.Flags().StringVar(&repository, "repository", "", "The name of the repository to delete") |
| 61 | + |
| 62 | + _ = cmd.RegisterFlagCompletionFunc(namespaceFlag, |
| 63 | + func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 64 | + return completion.BaseCompletion(namespaceFlag, args) |
| 65 | + }, |
| 66 | + ) |
| 67 | + return cmd |
| 68 | +} |
| 69 | + |
| 70 | +func repodelete(ctx context.Context, run *params.Run, names []string, opts *cli.PacCliOpts, ioStreams *cli.IOStreams, cascade bool) error { |
| 71 | + for _, name := range names { |
| 72 | + if cascade { |
| 73 | + // get repo spec |
| 74 | + repo, err := run.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(opts.Namespace).Get(ctx, name, v1.GetOptions{}) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + if repo.Spec.GitProvider != nil { |
| 79 | + if repo.Spec.GitProvider.Secret != nil { |
| 80 | + err = run.Clients.Kube.CoreV1().Secrets(opts.Namespace).Delete(ctx, repo.Spec.GitProvider.Secret.Name, v1.DeleteOptions{}) |
| 81 | + if err != nil { |
| 82 | + fmt.Fprintf(ioStreams.ErrOut, "skipping deleting api secret %s\n", repo.Spec.GitProvider.Secret.Name) |
| 83 | + } else { |
| 84 | + fmt.Fprintf(ioStreams.Out, "secret %s has been deleted\n", repo.Spec.GitProvider.Secret.Name) |
| 85 | + } |
| 86 | + } |
| 87 | + if repo.Spec.GitProvider.WebhookSecret != nil { |
| 88 | + err = run.Clients.Kube.CoreV1().Secrets(opts.Namespace).Delete(ctx, repo.Spec.GitProvider.WebhookSecret.Name, v1.DeleteOptions{}) |
| 89 | + if err != nil { |
| 90 | + fmt.Fprintf(ioStreams.ErrOut, "skipping deleting webhook secret %s\n", repo.Spec.GitProvider.WebhookSecret.Name) |
| 91 | + } else { |
| 92 | + fmt.Fprintf(ioStreams.Out, "secret %s has been deleted\n", repo.Spec.GitProvider.WebhookSecret.Name) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + err := run.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(opts.Namespace).Delete(ctx, name, v1.DeleteOptions{}) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + fmt.Fprintf(ioStreams.Out, "repository %s has been deleted\n", name) |
| 103 | + } |
| 104 | + return nil |
| 105 | +} |
0 commit comments