-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add repository command to harbor
Signed-off-by: amands98 <amandeepsm.in@gmail.com>
- Loading branch information
Showing
9 changed files
with
407 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package repository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.