Skip to content

Commit

Permalink
feat: improve the deletion of project
Browse files Browse the repository at this point in the history
Signed-off-by: amands98 <amandeepsm.in@gmail.com>
  • Loading branch information
amands98 committed Apr 3, 2024
1 parent df79cf3 commit e2a1813
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
9 changes: 4 additions & 5 deletions cmd/harbor/root/project/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ func DeleteProjectCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "project [NAME|ID]",
Short: "delete project by name or id",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)

if len(args) > 0 {
err = runDeleteProject(args[0], credentialName)
} else {
projectName := utils.GetProjectNameFromUser(credentialName)
err = runDeleteProject(projectName, credentialName)
}
// } else {
// projectName := utils.GetProjectNameFromUser(credentialName)
// err = runDeleteProject(projectName, credentialName)
// }
if err != nil {
log.Errorf("failed to delete project: %v", err)
}
Expand Down
24 changes: 18 additions & 6 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package utils

import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/goharbor/go-client/pkg/harbor"
view "github.com/goharbor/harbor-cli/pkg/views/project/select"

v2client "github.com/goharbor/go-client/pkg/sdk/v2.0/client"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/project"
log "github.com/sirupsen/logrus"
)

// Returns Harbor v2 client for given clientConfig
Expand Down Expand Up @@ -46,11 +51,18 @@ func PrintPayloadInJSONFormat(payload any) {
fmt.Println(string(jsonStr))
}

// func GetProjectNameFromUser(credentialName string) string {
// project := make(chan string)
// go func() {
func GetProjectNameFromUser(credentialName string) string {
projectName := make(chan string)
go func() {
client := GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.Project.ListProjects(ctx, &project.ListProjectsParams{})
if err != nil {
log.Fatal(err)
}
view.ProjectList(response.Payload, projectName)

// }()
}()

// return <-project
// }
return <-projectName
}
22 changes: 10 additions & 12 deletions pkg/views/project/select/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var (
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
helpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1)
quitTextStyle = lipgloss.NewStyle().Margin(1, 0, 2, 4)
)

type item string
Expand Down Expand Up @@ -51,9 +50,8 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
}

type model struct {
list list.Model
choice string
quitting bool
list list.Model
choice string
}

func (m model) Init() tea.Cmd {
Expand All @@ -68,10 +66,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "q", "ctrl+c":
m.quitting = true
return m, tea.Quit

case "enter":
i, ok := m.list.SelectedItem().(item)
if ok {
Expand All @@ -90,9 +84,6 @@ func (m model) View() string {
if m.choice != "" {
return ""
}
if m.quitting {
return quitTextStyle.Render("Not hungry? That’s cool.")
}
return "\n" + m.list.View()
}

Expand All @@ -113,8 +104,15 @@ func ProjectList(project []*models.Project, choice chan<- string) {

m := model{list: l}

if _, err := tea.NewProgram(m).Run(); err != nil {
p, err := tea.NewProgram(m, tea.WithAltScreen()).Run()

if err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}

if p, ok := p.(model); ok {
choice <- p.choice
}

}

0 comments on commit e2a1813

Please sign in to comment.