Skip to content

Commit

Permalink
feat: add repository command to harbor
Browse files Browse the repository at this point in the history
Signed-off-by: amands98 <amandeepsm.in@gmail.com>
  • Loading branch information
amands98 committed May 16, 2024
1 parent 875ec33 commit f280d0f
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/goharbor/harbor-cli/cmd/harbor/root/project"
"github.com/goharbor/harbor-cli/cmd/harbor/root/registry"
repositry "github.com/goharbor/harbor-cli/cmd/harbor/root/repository"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -100,6 +101,7 @@ harbor help
LoginCommand(),
project.Project(),
registry.Registry(),
repositry.Repository(),
)

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

import "github.com/spf13/cobra"

func Repository() *cobra.Command {
cmd := &cobra.Command{
Use: "repo",
Short: "Manage repositories",
Long: `Manage repositories in Harbor context`,
}
cmd.AddCommand(
ListRepositoryCommand(),
RepoInfoCmd(),
RepoDeleteCmd(),
)

return cmd

}
50 changes: 50 additions & 0 deletions cmd/harbor/root/repository/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package repository

import (
"context"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func RepoDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete a repository",
Example: ` harbor repository delete [project_name]/[repository_name]`,
Long: `Delete a repository within a project in Harbor`,
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) > 0 {
projectName, repoName := utils.ParseProjectRepo(args[0])
err = runRepoDelete(projectName, repoName)
} else {
projectName := utils.GetProjectNameFromUser()
repoName := utils.GetRepoNameFromUser(projectName)
err = runRepoDelete(projectName, repoName)
}
if err != nil {
log.Errorf("failed to delete repository: %v", err)
}
},
}
return cmd
}

func runRepoDelete(projectName, repoName string) error {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()

_, err := client.Repository.DeleteRepository(ctx, &repository.DeleteRepositoryParams{ProjectName: projectName, RepositoryName: repoName})

if err != nil {
return err
}

log.Infof("Repository %s/%s deleted successfully", projectName, repoName)
return nil
}
52 changes: 52 additions & 0 deletions cmd/harbor/root/repository/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package repository

import (
"context"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func RepoInfoCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "info",
Short: "Get repository information",
Example: ` harbor repo info <project_name>/<repo_name>`,
Long: `Get information of a particular repository in a project`,
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) > 0 {
projectName, repoName := utils.ParseProjectRepo(args[0])
err = runRepoInfo(projectName, repoName)
} else {
projectName := utils.GetProjectNameFromUser()
repoName := utils.GetRepoNameFromUser(projectName)
err = runRepoInfo(projectName, repoName)
}
if err != nil {
log.Errorf("failed to get repository information: %v", err)
}

},
}

return cmd
}

func runRepoInfo(projectName, repoName string) error {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()

response, err := client.Repository.GetRepository(ctx, &repository.GetRepositoryParams{ProjectName: projectName, RepositoryName: repoName})

if err != nil {
return err
}

utils.PrintPayloadInJSONFormat(response.Payload)
return nil
}
51 changes: 51 additions & 0 deletions cmd/harbor/root/repository/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package repository

import (
"context"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/repository/list"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func ListRepositoryCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list repositories within a project",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error

if len(args) > 0 {
err = runListRepository(args[0])
} else {
projectName := utils.GetProjectNameFromUser()
err = runListRepository(projectName)
}
if err != nil {
log.Errorf("failed to list repositories: %v", err)
}
},
}

return cmd
}

func runListRepository(ProjectName string) error {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()

response, err := client.Repository.ListRepositories(ctx, &repository.ListRepositoriesParams{ProjectName: ProjectName})

if err != nil {
return err
}

list.ListRepositories(response.Payload)
return nil

}
1 change: 1 addition & 0 deletions cmd/harbor/root/repository/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package repository
29 changes: 29 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/goharbor/go-client/pkg/harbor"
v2client "github.com/goharbor/go-client/pkg/sdk/v2.0/client"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/project"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository"
pview "github.com/goharbor/harbor-cli/pkg/views/project/select"
rview "github.com/goharbor/harbor-cli/pkg/views/registry/select"
repoView "github.com/goharbor/harbor-cli/pkg/views/repository/select"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -88,3 +91,29 @@ func GetProjectNameFromUser() string {

return <-projectName
}

func GetRepoNameFromUser(projectName string) string {
repositoryName := make(chan string)

go func() {
credentialName := viper.GetString("current-credential-name")
client := GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.Repository.ListRepositories(ctx, &repository.ListRepositoriesParams{ProjectName: projectName})
if err != nil {
log.Fatal(err)
}
repoView.RepositoryList(response.Payload, repositoryName)
}()

return <-repositoryName

}

func ParseProjectRepo(projectRepo string) (string, string) {
split := strings.Split(projectRepo, "/")
if len(split) != 2 {
log.Fatalf("invalid project/repository format: %s", projectRepo)
}
return split[0], split[1]
}
82 changes: 82 additions & 0 deletions pkg/views/repository/list/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package list

import (
"fmt"
"os"
"strconv"

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

var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).Padding(0, 1)

type model struct {
table table.Model
}

var columns = []table.Column{
{Title: "Name", Width: 24},
{Title: "Artifacts", Width: 12},
{Title: "Pulls", Width: 12},
{Title: "Last Modified Time", Width: 30},
}

func (m model) Init() tea.Cmd {
return tea.Quit
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd

m.table, cmd = m.table.Update(msg)
return m, cmd
}

func (m model) View() string {
return baseStyle.Render(m.table.View()) + "\n"
}

func ListRepositories(repos []*models.Repository) {
var rows []table.Row
for _, repo := range repos {

createdTime, _ := utils.FormatCreatedTime(repo.UpdateTime.String())
rows = append(rows, table.Row{
repo.Name,
fmt.Sprintf("%d", repo.ArtifactCount),
strconv.FormatInt(repo.PullCount, 10),
createdTime,
})
}

t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(len(rows)),
)

// Set the styles for the table
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderBottom(true).
Bold(false)

s.Selected = s.Selected.
Foreground(s.Cell.GetForeground()).
Background(s.Cell.GetBackground()).
Bold(false)
t.SetStyles(s)

m := model{t}
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
Loading

0 comments on commit f280d0f

Please sign in to comment.