Skip to content

Commit

Permalink
feat: improve the registry cli flow
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 6, 2024
1 parent 59ef18f commit a1c0afa
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 34 deletions.
8 changes: 2 additions & 6 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,21 @@ var (
verbose bool
)



func initConfig() {
viper.SetConfigType("yaml")

// cfgFile = viper.GetStering("config")
viper.SetConfigFile(cfgFile)
viper.SetDefault("output", "json")


if cfgFile != utils.DefaultConfigPath {
if cfgFile != utils.DefaultConfigPath {
viper.SetConfigFile(cfgFile)
} else {
stat, err := os.Stat(utils.DefaultConfigPath)
if !os.IsNotExist(err) && stat.Size() == 0 {
log.Println("Config file is empty, creating a new one")
}

if os.IsNotExist(err) {
log.Printf("Config file not found at %s, creating a new one", cfgFile)
}
Expand Down Expand Up @@ -99,7 +96,6 @@ harbor help

viper.BindPFlag("output", root.PersistentFlags().Lookup("output"))


root.AddCommand(
versionCommand(),
LoginCommand(),
Expand Down
3 changes: 1 addition & 2 deletions cmd/harbor/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func LoginCommand() *cobra.Command {
return cmd
}


func createLoginView(loginView *login.LoginView) error {
if loginView == nil {
loginView = &login.LoginView{
Expand Down Expand Up @@ -101,7 +100,7 @@ func runLogin(opts login.LoginView) error {
ServerAddress: opts.Server,
}

if err = utils.AddCredentialsToConfigFile(cred,utils.DefaultConfigPath); err != nil {
if err = utils.AddCredentialsToConfigFile(cred, utils.DefaultConfigPath); err != nil {
return fmt.Errorf("failed to store the credential: %s", err)
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/constants"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type createRegistrytOptions struct {
Expand All @@ -31,11 +31,7 @@ func CreateRegistryCommand() *cobra.Command {
Use: "create",
Short: "create registry",
RunE: func(cmd *cobra.Command, args []string) error {
credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)
if err != nil {
return err
}
return runCreateRegistry(opts, credentialName)
return runCreateRegistry(opts)
},
}

Expand All @@ -52,7 +48,8 @@ func CreateRegistryCommand() *cobra.Command {
return cmd
}

func runCreateRegistry(opts createRegistrytOptions, credentialName string) error {
func runCreateRegistry(opts createRegistrytOptions) error {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.Registry.CreateRegistry(ctx, &registry.CreateRegistryParams{Registry: &models.Registry{Credential: &models.RegistryCredential{AccessKey: opts.credential.accessKey, AccessSecret: opts.credential.accessSecret, Type: opts.credential._type}, Description: opts.description, Insecure: opts.insecure, Name: opts.name, Type: opts._type, URL: opts.url}})
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"

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

type listRegistryOptions struct {
Expand All @@ -23,12 +25,18 @@ func ListRegistryCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list registry",
RunE: func(cmd *cobra.Command, args []string) error {
credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)
Run: func(cmd *cobra.Command, args []string) {
registry, err := runListRegistry(opts)

if err != nil {
return err
log.Fatalf("failed to get projects list: %v", err)
}
FormatFlag := viper.GetString("output")
if FormatFlag != "json" {
utils.PrintPayloadInJSONFormat(registry)
return
}
return runListRegistry(opts, credentialName)
list.ListRegistry(registry.Payload)
},
}

Expand All @@ -41,15 +49,15 @@ func ListRegistryCommand() *cobra.Command {
return cmd
}

func runListRegistry(opts listRegistryOptions, credentialName string) error {
func runListRegistry(opts listRegistryOptions) (*registry.ListRegistriesOK, error) {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.Registry.ListRegistries(ctx, &registry.ListRegistriesParams{Page: &opts.page, PageSize: &opts.pageSize, Q: &opts.q, Sort: &opts.sort})

if err != nil {
return err
return nil, err
}

utils.PrintPayloadInJSONFormat(response.GetPayload())
return nil
return response, nil
}
File renamed without changes.
File renamed without changes.
13 changes: 3 additions & 10 deletions pkg/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/spf13/viper"
)


type Credential struct {
Name string `yaml:"name"`
Username string `yaml:"username"`
Expand All @@ -22,7 +21,7 @@ type HarborConfig struct {
}

var (
HarborFolder string
HarborFolder string
DefaultConfigPath string
)

Expand All @@ -35,9 +34,7 @@ func SetLocation() {
DefaultConfigPath = filepath.Join(HarborFolder, "config.yaml")
}



func (hc *HarborConfig) GetCurrentCredentialName() string{
func (hc *HarborConfig) GetCurrentCredentialName() string {
return hc.CurrentCredentialName
}

Expand All @@ -61,7 +58,7 @@ func AddCredentialsToConfigFile(credential Credential, configPath string) error
return err
}

c:= HarborConfig{}
c := HarborConfig{}
err = viper.Unmarshal(&c)
if err != nil {
return err
Expand All @@ -74,7 +71,6 @@ func AddCredentialsToConfigFile(credential Credential, configPath string) error
c.Credentials = append(c.Credentials, credential)
c.CurrentCredentialName = credential.Name


viper.Set("current-credential-name", credential.Name)
viper.Set("credentials", c.Credentials)
err = viper.WriteConfig()
Expand All @@ -85,7 +81,6 @@ func AddCredentialsToConfigFile(credential Credential, configPath string) error

}


func GetCredentials(credentialName string) (Credential, error) {
err := viper.ReadInConfig()
if err != nil {
Expand All @@ -105,5 +100,3 @@ func GetCredentials(credentialName string) (Credential, error) {
}
return Credential{}, nil
}


83 changes: 83 additions & 0 deletions pkg/views/registry/list/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package list

import (
"fmt"
"os"

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

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

type model struct {
table table.Model
}

var columns = []table.Column{
{Title: "Name", Width: 12},
{Title: "Status", Width: 12},
{Title: "Endpoint URL", Width: 26},
{Title: "Provider", Width: 12},
{Title: "Creation Time", Width: 24},
// {Title: "Verify Remote Cert", Width: 12},
{Title: "Description", Width: 12},
}

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 ListRegistry(registry []*models.Registry) {
var rows []table.Row
for _, regis := range registry {
// createdTime, _ := utils.FormatCreatedTime(project.CreationTime.String())
rows = append(rows, table.Row{
regis.Name, // Project Name
regis.Status, // Project Public
regis.URL, // Project Creation Time
regis.Type, // Project Update Time
regis.CreationTime.String(), // Project Update Time
regis.Description,
})
}

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)
}
}

0 comments on commit a1c0afa

Please sign in to comment.