Skip to content

Commit

Permalink
Add delete project cmd
Browse files Browse the repository at this point in the history
This command will help to delete a project
by name or id.

Signed-off-by: Akshat <akshat25iiit@gmail.com>
  • Loading branch information
akshatdalton committed May 16, 2023
1 parent 3bf5c61 commit d729be2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
48 changes: 48 additions & 0 deletions cmd/project/delete_project.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d729be2

Please sign in to comment.