Skip to content

Commit

Permalink
Add tkn-pac repo delete command
Browse files Browse the repository at this point in the history
Closes #523
  • Loading branch information
chmouel committed May 17, 2022
1 parent 516ad41 commit b765038
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
105 changes: 105 additions & 0 deletions pkg/cmd/tknpac/repository/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package repository

import (
"context"
"fmt"

"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
"github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/tknpac/completion"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const longHelp = `
Delete a Pipelines as Code Repository or multiple of them
eg:
tkn pac repository delete <repository-name> <repository-name2>
`

func DeleteCommand(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command {
var repository string
var cascade bool
cmd := &cobra.Command{
Args: cobra.MinimumNArgs(0),
Use: "delete",
Short: "Delete a Pipelines as Code Repository or multiple of them",
Long: longHelp,
Aliases: []string{"rm"},
RunE: func(cmd *cobra.Command, args []string) error {
var err error
opts := cli.NewCliOptions(cmd)
opts.Namespace, err = cmd.Flags().GetString(namespaceFlag)
if err != nil {
return err
}
ctx := context.Background()
err = run.Clients.NewClients(ctx, &run.Info)
if err != nil {
return err
}
if len(args) == 0 {
return fmt.Errorf("repository name is required")
}
if opts.Namespace == "" {
opts.Namespace = run.Info.Kube.Namespace
}
return repodelete(ctx, run, args, opts, ioStreams, cascade)
},
Annotations: map[string]string{
"commandType": "main",
},
}

cmd.Flags().StringP(
namespaceFlag, "n", "", "If present, the namespace scope for this CLI request")

cmd.Flags().BoolVarP(
&cascade, "cascade", "c", false, "Delete the repository and its secrets attached to it")
cmd.Flags().StringVar(&repository, "repository", "", "The name of the repository to delete")

_ = cmd.RegisterFlagCompletionFunc(namespaceFlag,
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completion.BaseCompletion(namespaceFlag, args)
},
)
return cmd
}

func repodelete(ctx context.Context, run *params.Run, names []string, opts *cli.PacCliOpts, ioStreams *cli.IOStreams, cascade bool) error {
for _, name := range names {
if cascade {
// get repo spec
repo, err := run.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(opts.Namespace).Get(ctx, name, v1.GetOptions{})
if err != nil {
return err
}
if repo.Spec.GitProvider != nil {
if repo.Spec.GitProvider.Secret != nil {
err = run.Clients.Kube.CoreV1().Secrets(opts.Namespace).Delete(ctx, repo.Spec.GitProvider.Secret.Name, v1.DeleteOptions{})
if err != nil {
fmt.Fprintf(ioStreams.ErrOut, "skipping deleting api secret %s\n", repo.Spec.GitProvider.Secret.Name)
} else {
fmt.Fprintf(ioStreams.Out, "secret %s has been deleted\n", repo.Spec.GitProvider.Secret.Name)
}
}
if repo.Spec.GitProvider.WebhookSecret != nil {
err = run.Clients.Kube.CoreV1().Secrets(opts.Namespace).Delete(ctx, repo.Spec.GitProvider.WebhookSecret.Name, v1.DeleteOptions{})
if err != nil {
fmt.Fprintf(ioStreams.ErrOut, "skipping deleting webhook secret %s\n", repo.Spec.GitProvider.WebhookSecret.Name)
} else {
fmt.Fprintf(ioStreams.Out, "secret %s has been deleted\n", repo.Spec.GitProvider.WebhookSecret.Name)
}
}
}
}

err := run.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(opts.Namespace).Delete(ctx, name, v1.DeleteOptions{})
if err != nil {
return err
}
fmt.Fprintf(ioStreams.Out, "repository %s has been deleted\n", name)
}
return nil
}
1 change: 1 addition & 0 deletions pkg/cmd/tknpac/repository/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Root(clients *params.Run, ioStreams *cli.IOStreams) *cobra.Command {
cmd.AddCommand(ListCommand(clients, ioStreams))
cmd.AddCommand(DescribeCommand(clients, ioStreams))
cmd.AddCommand(CreateCommand(clients, ioStreams))
cmd.AddCommand(DeleteCommand(clients, ioStreams))

return cmd
}

0 comments on commit b765038

Please sign in to comment.