Skip to content

Commit

Permalink
add '--force' flag to delete non-empty projects
Browse files Browse the repository at this point in the history
Signed-off-by: adwait-godbole <[email protected]>
  • Loading branch information
adwait-godbole committed Nov 14, 2024
1 parent cc8d1c8 commit 074fad4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 6 additions & 2 deletions cmd/harbor/root/project/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

// DeleteProjectCommand creates a new `harbor delete project` command
func DeleteProjectCommand() *cobra.Command {
var forceDelete bool

cmd := &cobra.Command{
Use: "delete",
Expand All @@ -18,16 +19,19 @@ func DeleteProjectCommand() *cobra.Command {
var err error

if len(args) > 0 {
err = api.DeleteProject(args[0])
err = api.DeleteProject(args[0], forceDelete)
} else {
projectName := prompt.GetProjectNameFromUser()
err = api.DeleteProject(projectName)
err = api.DeleteProject(projectName, forceDelete)
}
if err != nil {
log.Errorf("failed to delete project: %v", err)
}
},
}

flags := cmd.Flags()
flags.BoolVar(&forceDelete, "force", false, "Deletes all repositories and artifacts within the project")

return cmd
}
26 changes: 24 additions & 2 deletions pkg/api/project_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strconv"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/project"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/project/create"
Expand Down Expand Up @@ -54,19 +55,40 @@ func GetProject(projectName string) error {
return nil
}

func DeleteProject(projectName string) error {
func DeleteProject(projectName string, forceDelete bool) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
}

if forceDelete {
var resp repository.ListRepositoriesOK

resp, err = ListRepository(projectName)

if err != nil {
log.Errorf("failed to list repositories: %v", err)
return err
}

for _, repo := range resp.Payload {
_, repoName := utils.ParseProjectRepo(repo.Name)
err = RepoDelete(projectName, repoName)

if err != nil {
log.Errorf("failed to delete repository: %v", err)
return err
}
}
}

_, err = client.Project.DeleteProject(ctx, &project.DeleteProjectParams{ProjectNameOrID: projectName})

if err != nil {
return err
}

log.Info("project deleted successfully")
log.Infof("Project %s deleted successfully", projectName)
return nil
}

Expand Down

0 comments on commit 074fad4

Please sign in to comment.