From d729be221c4604dd63097d50dd44ea802c9d1c74 Mon Sep 17 00:00:00 2001 From: Akshat Date: Tue, 16 May 2023 16:28:42 +0530 Subject: [PATCH] Add delete project cmd This command will help to delete a project by name or id. Signed-off-by: Akshat --- cmd/project/delete_project.go | 48 +++++++++++++++++++++++++++++++++++ cmd/root.go | 14 ++++++++++ 2 files changed, 62 insertions(+) create mode 100644 cmd/project/delete_project.go diff --git a/cmd/project/delete_project.go b/cmd/project/delete_project.go new file mode 100644 index 00000000..e3d077b9 --- /dev/null +++ b/cmd/project/delete_project.go @@ -0,0 +1,48 @@ +package project + +import ( + "context" + + "github.com/akshatdalton/harbor-cli/cmd/constants" + "github.com/akshatdalton/harbor-cli/cmd/utils" + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/project" + "github.com/spf13/cobra" +) + +type deleteProjectOptions struct { + projectNameOrID string +} + +// NewDeleteProjectCommand creates a new `harbor delete project` command +func NewDeleteProjectCommand() *cobra.Command { + var opts deleteProjectOptions + + cmd := &cobra.Command{ + Use: "project [NAME|ID]", + Short: "delete project by name or id", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + opts.projectNameOrID = args[0] + credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption) + if err != nil { + return err + } + return runDeleteProject(opts, credentialName) + }, + } + + return cmd +} + +func runDeleteProject(opts deleteProjectOptions, credentialName string) error { + client := utils.GetClientByCredentialName(credentialName) + ctx := context.Background() + response, err := client.Project.DeleteProject(ctx, &project.DeleteProjectParams{ProjectNameOrID: opts.projectNameOrID}) + + if err != nil { + return err + } + + utils.PrintPayloadInJSONFormat(response) + return nil +} diff --git a/cmd/root.go b/cmd/root.go index 70b0105e..747869d7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -50,11 +50,25 @@ func newCreateCommand() *cobra.Command { return cmd } +// newDeleteCommand creates a new `harbor delete` command +func newDeleteCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "delete [COMMAND]", + Short: "delete project, registry, etc.", + Long: `Delete project, registry`, + } + + cmd.PersistentFlags().String(constants.CredentialNameOption, "", constants.CredentialNameHelp) + cmd.AddCommand(project.NewDeleteProjectCommand()) + return cmd +} + func addCommands(cmd *cobra.Command) { cmd.AddCommand(login.NewLoginCommand()) cmd.AddCommand(newGetCommand()) cmd.AddCommand(newListCommand()) cmd.AddCommand(newCreateCommand()) + cmd.AddCommand(newDeleteCommand()) } // CreateHarborCLI creates a new Harbor CLI