Skip to content

Commit

Permalink
Merge remote-tracking branch 'harbor-cli/main' into bug/JianMinTang/240
Browse files Browse the repository at this point in the history
  • Loading branch information
Standing-Man committed Nov 26, 2024
2 parents 92b09c8 + 9b7fcc5 commit 994566d
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/harbor/root/artifact/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ListArtifactCommand() *cobra.Command {
repoName = prompt.GetRepoNameFromUser(projectName)
}

artifacts, err = api.ListArtifact(projectName, repoName)
artifacts, err = api.ListArtifact(projectName, repoName, opts)

if err != nil {
log.Errorf("failed to list artifacts: %v", err)
Expand Down
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(),
RepoViewCmd(),
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 @@ -103,6 +104,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)
}
}

0 comments on commit 994566d

Please sign in to comment.