Skip to content

Commit

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

Signed-off-by: Akshat <akshat25iiit@gmail.com>
  • Loading branch information
akshatdalton committed May 16, 2023
1 parent d729be2 commit 1ab60f5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
56 changes: 56 additions & 0 deletions cmd/registry/delete_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package registry

import (
"context"
"fmt"
"strconv"

"github.com/akshatdalton/harbor-cli/cmd/constants"
"github.com/akshatdalton/harbor-cli/cmd/utils"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry"
"github.com/spf13/cobra"
)

type deleteRegistryOptions struct {
id int64
}

// NewDeleteRegistryCommand creates a new `harbor delete registry` command
func NewDeleteRegistryCommand() *cobra.Command {
var opts deleteRegistryOptions

cmd := &cobra.Command{
Use: "registry [NAME|ID]",
Short: "delete registry by name or id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.Atoi(args[0])
if err != nil {
fmt.Printf("Invalid argument: %s. Expected an integer.\n", args[0])
return err
}
opts.id = int64(id)

credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)
if err != nil {
return err
}
return runDeleteRegistry(opts, credentialName)
},
}

return cmd
}

func runDeleteRegistry(opts deleteRegistryOptions, credentialName string) error {
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.Registry.DeleteRegistry(ctx, &registry.DeleteRegistryParams{ID: opts.id})

if err != nil {
return err
}

utils.PrintPayloadInJSONFormat(response)
return nil
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func newDeleteCommand() *cobra.Command {

cmd.PersistentFlags().String(constants.CredentialNameOption, "", constants.CredentialNameHelp)
cmd.AddCommand(project.NewDeleteProjectCommand())
cmd.AddCommand(registry.NewDeleteRegistryCommand())
return cmd
}

Expand Down

0 comments on commit 1ab60f5

Please sign in to comment.