Skip to content

Commit

Permalink
Merge pull request #31 from amands98/enha
Browse files Browse the repository at this point in the history
feat: move project and registry to root & improve the harbor config
  • Loading branch information
Vad1mo authored Apr 15, 2024
2 parents bba248a + dfc0528 commit 59ef18f
Show file tree
Hide file tree
Showing 23 changed files with 373 additions and 421 deletions.
2 changes: 1 addition & 1 deletion cmd/harbor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func main() {
err := root.New().Execute()
err := root.RootCmd().Execute()
if err != nil {
os.Exit(1)
}
Expand Down
168 changes: 82 additions & 86 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,114 +2,110 @@ package root

import (
"fmt"
"log"
"os"

"github.com/goharbor/harbor-cli/cmd/harbor/internal/version"
"github.com/goharbor/harbor-cli/cmd/harbor/root/project"
"github.com/goharbor/harbor-cli/cmd/harbor/root/registry"
"github.com/goharbor/harbor-cli/pkg/constants"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// versionCommand creates a new `harbor version` command
func versionCommand() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "get Harbor CLI version",
Long: `Get Harbor CLI version, git commit, go version, build time, release channel, os/arch, etc.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Version: %s\n", version.Version)
fmt.Printf("Go version: %s\n", version.GoVersion)
fmt.Printf("Git commit: %s\n", version.GitCommit)
fmt.Printf("Built: %s\n", version.BuildTime)
fmt.Printf("OS/Arch: %s\n", version.System)
},
}
}
var (
output string
cfgFile string
verbose bool
)

// newGetCommand creates a new `harbor get` command
func newGetCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "get [COMMAND]",
Short: "get project, registry, etc.",
Long: `Get project, registry`,
}

cmd.PersistentFlags().String(constants.CredentialNameOption, "", constants.CredentialNameHelp)
cmd.AddCommand(project.GetProjectCommand())
cmd.AddCommand(registry.GetRegistryCommand())
return cmd
}

// newListCommand creates a new `harbor list` command
func newListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list [COMMAND]",
Short: "list project, registry, etc.",
Long: `List project, registry`,
}
func initConfig() {
viper.SetConfigType("yaml")

cmd.PersistentFlags().String(constants.CredentialNameOption, "", constants.CredentialNameHelp)
cmd.AddCommand(project.ListProjectCommand())
cmd.AddCommand(registry.ListRegistryCommand())
return cmd
}
// cfgFile = viper.GetStering("config")
viper.SetConfigFile(cfgFile)
viper.SetDefault("output", "json")

// newCreateCommand creates a new `harbor create` command
func newCreateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "create [COMMAND]",
Short: "create project, registry, etc.",
Long: `Create project, registry`,
}

cmd.PersistentFlags().String(constants.CredentialNameOption, "", constants.CredentialNameHelp)
cmd.AddCommand(project.CreateProjectCommand())
cmd.AddCommand(registry.CreateRegistryCommand())
return cmd
}
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)
}

// newDeleteCommand creates a new `harbor delete` command
func newDeleteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [COMMAND]",
Short: "delete project, registry, etc.",
Long: `Delete project, registry`,
}
if os.IsNotExist(err) || (!os.IsNotExist(err) && stat.Size() == 0) {
if _, err := os.Stat(utils.HarborFolder); os.IsNotExist(err) {
// Create the parent directory if it doesn't exist

cmd.PersistentFlags().String(constants.CredentialNameOption, "", constants.CredentialNameHelp)
cmd.AddCommand(project.DeleteProjectCommand())
cmd.AddCommand(registry.DeleteRegistryCommand())
return cmd
}
fmt.Println("Creating config file", utils.HarborFolder)
if err := os.MkdirAll(utils.HarborFolder, os.ModePerm); err != nil {
log.Fatal(err)
}
}
err = utils.CreateConfigFile()

if err != nil {
log.Fatal(err)
}

// newUpdateCommand creates a new `harbor update` command
func newUpdateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "update [COMMAND]",
Short: "update registry, etc.",
Long: `Update registry`,
err = utils.AddCredentialsToConfigFile(utils.Credential{}, cfgFile)

if err != nil {
log.Fatal(err)
}

log.Printf("Config file created at %s", cfgFile)
}
}

if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file: %s", err)
}

cmd.PersistentFlags().String(constants.CredentialNameOption, "", constants.CredentialNameHelp)
cmd.AddCommand(registry.UpdateRegistryCommand())
return cmd
}

// CreateHarborCLI creates a new Harbor CLI
func New() *cobra.Command {
cmd := &cobra.Command{
Use: "harbor [command]",
Short: "Official Harbor CLI",
func RootCmd() *cobra.Command {
utils.SetLocation()

root := &cobra.Command{
Use: "harbor",
Short: "Official Harbor CLI",
SilenceUsage: true,
Long: "Official Harbor CLI",
Example: `
// Base command:
harbor
// Display help about the command:
harbor help
`,
// RunE: func(cmd *cobra.Command, args []string) error {

// },
}

cmd.AddCommand(
cobra.OnInitialize(initConfig)

root.PersistentFlags().StringVarP(&output, "output", "o", "json", "Output format. One of: json|yaml")
root.PersistentFlags().StringVar(&cfgFile, "config", utils.DefaultConfigPath, "config file (default is $HOME/.harbor/config.yaml)")
root.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")

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


root.AddCommand(
versionCommand(),
LoginCommand(),
newGetCommand(),
newListCommand(),
newCreateCommand(),
newDeleteCommand(),
newUpdateCommand(),
project.Project(),
registry.Registry(),
)
return cmd

return root
}
19 changes: 9 additions & 10 deletions cmd/harbor/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
)

var (
serverAddress string
Username string
Password string
Name string
serverAddress string
Username string
Password string
Name string
)

// LoginCommand creates a new `harbor login` command
Expand All @@ -28,10 +28,9 @@ func LoginCommand() *cobra.Command {
Long: "Authenticate with Harbor Registry.",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {

if len(args) > 0 {
serverAddress = args[0]
}
if len(args) > 0 {
serverAddress = args[0]
}

loginView := login.LoginView{
Server: serverAddress,
Expand Down Expand Up @@ -64,6 +63,7 @@ func LoginCommand() *cobra.Command {
return cmd
}


func createLoginView(loginView *login.LoginView) error {
if loginView == nil {
loginView = &login.LoginView{
Expand All @@ -80,7 +80,6 @@ func createLoginView(loginView *login.LoginView) error {

}


func runLogin(opts login.LoginView) error {
clientConfig := &harbor.ClientSetConfig{
URL: opts.Server,
Expand All @@ -102,7 +101,7 @@ func runLogin(opts login.LoginView) error {
ServerAddress: opts.Server,
}

if err = utils.StoreCredential(cred, true); 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
22 changes: 22 additions & 0 deletions cmd/harbor/root/project/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package project

import (
"github.com/spf13/cobra"
)

func Project() *cobra.Command {
cmd := &cobra.Command{
Use: "project",
Short: "Manage projects and assign resources to them",
Long: `Manage projects in Harbor`,
Example: ` harbor project list`,
}
cmd.AddCommand(
CreateProjectCommand(),
DeleteProjectCommand(),
ListProjectCommand(),
ViewCommand(),
)

return cmd
}
15 changes: 6 additions & 9 deletions cmd/harbor/root/project/create_project.go
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/project"
"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 createProjectOptions struct {
Expand All @@ -22,14 +22,10 @@ func CreateProjectCommand() *cobra.Command {
var opts createProjectOptions

cmd := &cobra.Command{
Use: "project",
Short: "create project",
Use: "create",
Short: "create",
RunE: func(cmd *cobra.Command, args []string) error {
credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)
if err != nil {
return err
}
return runCreateProject(opts, credentialName)
return runCreateProject(opts)
},
}

Expand All @@ -42,7 +38,8 @@ func CreateProjectCommand() *cobra.Command {
return cmd
}

func runCreateProject(opts createProjectOptions, credentialName string) error {
func runCreateProject(opts createProjectOptions) error {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.Project.CreateProject(ctx, &project.CreateProjectParams{Project: &models.ProjectReq{ProjectName: opts.projectName, Public: &opts.public, RegistryID: &opts.registryID, StorageLimit: &opts.storageLimit}})
Expand Down
2 changes: 1 addition & 1 deletion cmd/harbor/root/project/delete_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func DeleteProjectCommand() *cobra.Command {
var opts deleteProjectOptions

cmd := &cobra.Command{
Use: "project [NAME|ID]",
Use: "delete [NAME|ID]",
Short: "delete project by name or id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
14 changes: 6 additions & 8 deletions cmd/harbor/root/project/ls_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"

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

type listProjectOptions struct {
Expand All @@ -25,14 +25,11 @@ func ListProjectCommand() *cobra.Command {
var opts listProjectOptions

cmd := &cobra.Command{
Use: "project",
Use: "list",
Short: "list project",
RunE: func(cmd *cobra.Command, args []string) error {
credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)
if err != nil {
return err
}
return runListProject(opts, credentialName)

return runListProject(opts)
},
}

Expand All @@ -49,7 +46,8 @@ func ListProjectCommand() *cobra.Command {
return cmd
}

func runListProject(opts listProjectOptions, credentialName string) error {
func runListProject(opts listProjectOptions) error {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.Project.ListProjects(ctx, &project.ListProjectsParams{Name: &opts.name, Owner: &opts.owner, Page: &opts.page, PageSize: &opts.pageSize, Public: &opts.public, Q: &opts.q, Sort: &opts.sort, WithDetail: &opts.withDetail})
Expand Down
4 changes: 2 additions & 2 deletions cmd/harbor/root/project/view_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ type getProjectOptions struct {
}

// GetProjectCommand creates a new `harbor get project` command
func GetProjectCommand() *cobra.Command {
func ViewCommand() *cobra.Command {
var opts getProjectOptions

cmd := &cobra.Command{
Use: "project [NAME|ID]",
Use: "view [NAME|ID]",
Short: "get project by name or id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
Loading

0 comments on commit 59ef18f

Please sign in to comment.