Skip to content

Commit

Permalink
Added latest changes from main; added changes suggested from reviewdo…
Browse files Browse the repository at this point in the history
…g pipeline about unused err in config_test.go

Signed-off-by: Patrick Eschenbach <[email protected]>
  • Loading branch information
qcserestipy committed Nov 25, 2024
2 parents 66716a8 + 9b7fcc5 commit d700e14
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/harbor/root/project/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func Project() *cobra.Command {
ListProjectCommand(),
ViewCommand(),
LogsProjectCommmand(),
SearchProjectCommand(),
)

return cmd
Expand Down
33 changes: 33 additions & 0 deletions cmd/harbor/root/project/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package project

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/project/list"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func SearchProjectCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "search",
Short: "search project based on their names",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {

projects, err := api.SearchProject(args[0])
if err != nil {
log.Fatalf("failed to get projects: %v", err)
}
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(projects)
return
}

list.SearchProjects(projects.Payload.Project)
},
}
return cmd
}
1 change: 1 addition & 0 deletions cmd/harbor/root/repository/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func Repository() *cobra.Command {
ListRepositoryCommand(),
RepoInfoCmd(),
RepoDeleteCmd(),
SearchRepoCmd(),
)

return cmd
Expand Down
33 changes: 33 additions & 0 deletions cmd/harbor/root/repository/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package repository

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/repository/search"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func SearchRepoCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "search",
Short: "search repository based on their names",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {

repo, err := api.SearchRepository(args[0])
if err != nil {
log.Fatalf("failed to get repositories: %v", err)
}
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(repo)
return
}

search.SearchRepositories(repo.Payload.Repository)
},
}
return cmd
}
14 changes: 14 additions & 0 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/search"
"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 @@ -102,6 +103,19 @@ func ListAllProjects(opts ...ListFlags) (project.ListProjectsOK, error) {
return *response, nil
}

func SearchProject(query string) (search.SearchOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return search.SearchOK{}, err
}

response, err := client.Search.Search(ctx, &search.SearchParams{Q: query})
if err != nil {
return search.SearchOK{}, err
}
return *response, nil
}

func LogsProject(projectName string) (*project.GetLogsOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions pkg/api/repository_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/search"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -52,3 +53,17 @@ func ListRepository(projectName string) (repository.ListRepositoriesOK, error) {
return *response, nil

}

func SearchRepository(query string) (search.SearchOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return search.SearchOK{}, err
}

response, err := client.Search.Search(ctx, &search.SearchParams{Q: query})
if err != nil {
return search.SearchOK{}, err
}

return *response, nil
}
31 changes: 31 additions & 0 deletions pkg/views/project/list/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,34 @@ func ListProjects(projects []*models.Project) {
os.Exit(1)
}
}

func SearchProjects(projects []*models.Project) {
var rows []table.Row
for _, project := range projects {
accessLevel := project.Metadata.Public
if accessLevel != "true" {
accessLevel = "private"
} else {
accessLevel = "public"
}
projectType := "project"
if project.RegistryID != 0 {
projectType = "proxy cache"
}
createdTime, _ := utils.FormatCreatedTime(project.CreationTime.String())
rows = append(rows, table.Row{
strconv.FormatInt(int64(project.ProjectID), 10), // ProjectID
project.Name, // Project Name
accessLevel, // Access Level
projectType, // Type
strconv.FormatInt(project.RepoCount, 10),
createdTime, // Creation Time
})
}
m := tablelist.NewModel(columns, rows, len(rows))

if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
45 changes: 45 additions & 0 deletions pkg/views/repository/search/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package search

import (
"fmt"
"os"
"strconv"

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/views/base/tablelist"
)

var columns = []table.Column{
{Title: "Repository Name", Width: 30},
{Title: "Project Id", Width: 11},
{Title: "Project Name", Width: 16},
{Title: "Access Level", Width: 12},
{Title: "Artifact Count", Width: 14},
{Title: "Pull Count", Width: 12},
}

func SearchRepositories(repos []*models.SearchRepository) {
var rows []table.Row
for _, repo := range repos {
accessLevel := "public"
if !repo.ProjectPublic {
accessLevel = "private"
}
rows = append(rows, table.Row{
repo.RepositoryName,
fmt.Sprintf("%d", repo.ProjectID),
repo.ProjectName,
accessLevel,
fmt.Sprintf("%d", repo.ArtifactCount),
strconv.FormatInt(repo.PullCount, 10),
})
}

m := tablelist.NewModel(columns, rows, len(rows))
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
4 changes: 3 additions & 1 deletion test/e2e/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func Test_Config_EnvVar(t *testing.T) {
assert.NoError(t, err, "Expected no error for Root command execution")

currentData, err := utils.GetCurrentHarborData()
assert.NoError(t, err, "Expected no error when fetching HarborData")
defer ConfigCleanup(t, currentData)

currentConfig, err := utils.GetCurrentHarborConfig()
Expand All @@ -93,8 +94,8 @@ func Test_Config_Vanilla(t *testing.T) {
err := cds.Execute()
assert.NoError(t, err, "Expected no error for Root command")
assert.NoError(t, err, "Expected no error for Root command execution")

currentData, err := utils.GetCurrentHarborData()
assert.NoError(t, err, "Expected no error when fetching HarborData")
defer ConfigCleanup(t, currentData)

currentConfig, err := utils.GetCurrentHarborConfig()
Expand All @@ -118,6 +119,7 @@ func Test_Config_Xdg(t *testing.T) {
assert.NoError(t, err, "Expected no error for Root command execution")

currentData, err := utils.GetCurrentHarborData()
assert.NoError(t, err, "Expected no error when fetching HarborData")
defer ConfigCleanup(t, currentData)

currentConfig, err := utils.GetCurrentHarborConfig()
Expand Down

0 comments on commit d700e14

Please sign in to comment.