Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Isolate all the cobra cli commands under cli Folder #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions cli/cmd/artifact/ls_artifact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package artifact

import (
"github.com/goharbor/harbor-cli/api"
"github.com/goharbor/harbor-cli/internal/pkg/config"
"github.com/goharbor/harbor-cli/internal/pkg/constants"
"github.com/spf13/cobra"
)

// NewListArtifactCommand creates a new `harbor list artifact` command
func ListArtifactCommand() *cobra.Command {
var opts config.ListArtifactOptions

cmd := &cobra.Command{
Use: "artifact",
Short: "list artifact",
RunE: func(cmd *cobra.Command, args []string) error {
credentialName, _ := cmd.Flags().GetString(constants.CredentialNameOption)

return api.RunListArtifact(opts, credentialName, config.OutputType, config.WideOutput)
},
}

flags := cmd.Flags()
flags.StringVarP(&opts.ProjectName, "projectname", "", "", "Name of the project")
flags.StringVarP(&opts.RepositoryName, "reponame", "", "", "Name of the repository")
flags.StringVarP(&config.OutputType, "output", "o", "", "Output type [json/yaml]")
flags.BoolVarP(&config.WideOutput, "owide", "", false, "Wide output result [true/false]")
return cmd
}
38 changes: 38 additions & 0 deletions cli/cmd/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"github.com/goharbor/harbor-cli/api"
"github.com/goharbor/harbor-cli/internal/pkg/config"
"github.com/spf13/cobra"
)

// LoginCommand creates a new `harbor login` command
func LoginCommand() *cobra.Command {
var opts config.LoginOptions

cmd := &cobra.Command{
Use: "login [SERVER]",
Short: "Log in to Harbor registry",
Long: "Authenticate with Harbor Registry.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.ServerAddress = args[0]
return api.RunLogin(&opts)
},
}

flags := cmd.Flags()
flags.StringVarP(&opts.Name, "name", "n", "", "name for the set of credentials")
flags.StringVarP(&opts.ServerAddress, "url", "", "", "server address")

flags.StringVarP(&opts.Username, "username", "u", "", "Username")
if err := cmd.MarkFlagRequired("username"); err != nil {
panic(err)
}
flags.StringVarP(&opts.Password, "password", "p", "", "Password")
if err := cmd.MarkFlagRequired("password"); err != nil {
panic(err)
}

return cmd
}
34 changes: 34 additions & 0 deletions cli/cmd/project/create_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package project

import (
"github.com/goharbor/harbor-cli/api"
"github.com/goharbor/harbor-cli/internal/pkg/config"
"github.com/goharbor/harbor-cli/internal/pkg/constants"
"github.com/spf13/cobra"
)

// CreateProjectCommand creates a new `harbor create project` command
func CreateProjectCommand() *cobra.Command {
var opts config.CreateProjectOptions

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

flags := cmd.Flags()
flags.BoolVarP(&opts.Public, "public", "", false, "Project is public or private")
flags.Int64VarP(&opts.RegistryID, "registry-id", "", 1, "ID of referenced registry when creating the proxy cache project")
flags.Int64VarP(&opts.StorageLimit, "storage-limit", "", -1, "Storage quota of the project")

return cmd
}
29 changes: 29 additions & 0 deletions cli/cmd/project/delete_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package project

import (
"github.com/goharbor/harbor-cli/api"
"github.com/goharbor/harbor-cli/internal/pkg/config"
"github.com/goharbor/harbor-cli/internal/pkg/constants"
"github.com/spf13/cobra"
)

// DeleteProjectCommand creates a new `harbor delete project` command
func DeleteProjectCommand() *cobra.Command {
var opts config.DeleteProjectOptions

cmd := &cobra.Command{
Use: "project [NAME|ID]",
Short: "delete project by name or id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.ProjectNameOrID = args[0]
credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)
if err != nil {
return err
}
return api.RunDeleteProject(opts, credentialName)
},
}

return cmd
}
37 changes: 37 additions & 0 deletions cli/cmd/project/ls_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package project

import (
"github.com/goharbor/harbor-cli/api"
"github.com/goharbor/harbor-cli/internal/pkg/config"
"github.com/goharbor/harbor-cli/internal/pkg/constants"
"github.com/spf13/cobra"
)

// NewListProjectCommand creates a new `harbor list project` command
func ListProjectCommand() *cobra.Command {
var opts config.ListProjectOptions

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

flags := cmd.Flags()
flags.StringVarP(&opts.Name, "name", "", "", "Name of the project")
flags.StringVarP(&opts.Owner, "owner", "", "", "Name of the project owner")
flags.Int64VarP(&opts.Page, "page", "", 1, "Page number")
flags.Int64VarP(&opts.PageSize, "page-size", "", 10, "Size of per page")
flags.BoolVarP(&opts.Public, "public", "", true, "Project is public or private")
flags.StringVarP(&opts.Q, "query", "q", "", "Query string to query resources")
flags.StringVarP(&opts.Sort, "sort", "", "", "Sort the resource list in ascending or descending order")
flags.StringVarP(&config.OutputType, "output", "o", "", "Output type [json/yaml]")
flags.BoolVarP(&config.WideOutput, "wide", "", false, "Wide output result [true/false]")
return cmd
}
31 changes: 31 additions & 0 deletions cli/cmd/project/view_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package project

import (
"github.com/goharbor/harbor-cli/api"
"github.com/goharbor/harbor-cli/internal/pkg/config"
"github.com/goharbor/harbor-cli/internal/pkg/constants"
"github.com/spf13/cobra"
)

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

cmd := &cobra.Command{
Use: "project [NAME|ID]",
Short: "get project by name or id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.ProjectNameOrID = args[0]
credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)
if err != nil {
return err
}
return api.RunGetProject(opts, credentialName, config.OutputType, config.WideOutput)
},
}

flags := cmd.Flags()
flags.StringVarP(&config.OutputType, "output", "o", "", "Output type [json/yaml]")
return cmd
}
37 changes: 37 additions & 0 deletions cli/cmd/registry/create_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package registry

import (
"github.com/goharbor/harbor-cli/api"
"github.com/goharbor/harbor-cli/internal/pkg/config"
"github.com/goharbor/harbor-cli/internal/pkg/constants"
"github.com/spf13/cobra"
)

// CreateRegistryCommand creates a new `harbor create registry` command
func CreateRegistryCommand() *cobra.Command {
var opts config.CreateRegistrytOptions

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

flags := cmd.Flags()
flags.StringVarP(&opts.Name, "name", "n", "", "Name of the registry")
flags.StringVarP(&opts.Type, "type", "", "harbor", "Type of the registry")
flags.StringVarP(&opts.Url, "url", "", "", "Registry endpoint URL")
flags.StringVarP(&opts.Description, "description", "", "", "Description of the registry")
flags.BoolVarP(&opts.Insecure, "insecure", "", true, "Whether or not the certificate will be verified when Harbor tries to access the server")
flags.StringVarP(&opts.Credential.AccessKey, "credential-access-key", "", "", "Access key, e.g. user name when credential type is 'basic'")
flags.StringVarP(&opts.Credential.AccessSecret, "credential-access-secret", "", "", "Access secret, e.g. password when credential type is 'basic'")
flags.StringVarP(&opts.Credential.Type, "credential-type", "", "basic", "Credential type, such as 'basic', 'oauth'")

return cmd
}
38 changes: 38 additions & 0 deletions cli/cmd/registry/delete_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package registry

import (
"fmt"
"strconv"

"github.com/goharbor/harbor-cli/api"
"github.com/goharbor/harbor-cli/internal/pkg/config"
"github.com/goharbor/harbor-cli/internal/pkg/constants"
"github.com/spf13/cobra"
)

// DeleteRegistryCommand creates a new `harbor delete registry` command
func DeleteRegistryCommand() *cobra.Command {
var opts config.DeleteRegistryOptions

cmd := &cobra.Command{
Use: "registry [ID]",
Short: "delete registry by id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.Atoi(args[0])
if err != nil {
fmt.Printf("Invalid argument: %s. Expected an integer.\n", args[0])
return err
}
opts.Id = int64(id)

credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)
if err != nil {
return err
}
return api.RunDeleteRegistry(opts, credentialName)
},
}

return cmd
}
34 changes: 34 additions & 0 deletions cli/cmd/registry/ls_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package registry

import (
"github.com/goharbor/harbor-cli/api"
"github.com/goharbor/harbor-cli/internal/pkg/config"
"github.com/goharbor/harbor-cli/internal/pkg/constants"
"github.com/spf13/cobra"
)

// ListRegistryCommand creates a new `harbor list registry` command
func ListRegistryCommand() *cobra.Command {
var opts config.ListRegistryOptions

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

flags := cmd.Flags()
flags.Int64VarP(&opts.Page, "page", "", 1, "Page number")
flags.Int64VarP(&opts.PageSize, "page-size", "", 10, "Size of per page")
flags.StringVarP(&opts.Q, "query", "q", "", "Query string to query resources")
flags.StringVarP(&opts.Sort, "sort", "", "", "Sort the resource list in ascending or descending order")
flags.StringVarP(&config.OutputType, "output", "o", "", "Output type [json/yaml]")
flags.BoolVarP(&config.WideOutput, "wide", "", false, "Wide output result [true/false]")
return cmd
}
48 changes: 48 additions & 0 deletions cli/cmd/registry/update_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package registry

import (
"fmt"
"strconv"

"github.com/goharbor/harbor-cli/api"
"github.com/goharbor/harbor-cli/internal/pkg/config"
"github.com/goharbor/harbor-cli/internal/pkg/constants"
"github.com/spf13/cobra"
)

// UpdateRegistryCommand creates a new `harbor update registry` command
func UpdateRegistryCommand() *cobra.Command {
var opts config.UpdateRegistrytOptions

cmd := &cobra.Command{
Use: "registry [ID]",
Short: "update registry",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.Atoi(args[0])
if err != nil {
fmt.Printf("Invalid argument: %s. Expected an integer.\n", args[0])
return err
}
opts.Id = int64(id)

credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption)
if err != nil {
return err
}
return api.RunUpdateRegistry(opts, credentialName)
},
}

flags := cmd.Flags()
flags.StringVarP(&opts.Name, "name", "", "", "Name of the registry")
flags.StringVarP(&opts.Type, "type", "", "", "Type of the registry")
flags.StringVarP(&opts.Url, "url", "", "", "Registry endpoint URL")
flags.StringVarP(&opts.Description, "description", "", "", "Description of the registry")
flags.BoolVarP(&opts.Insecure, "insecure", "", true, "Whether or not the certificate will be verified when Harbor tries to access the server")
flags.StringVarP(&opts.Credential.AccessKey, "credential-access-key", "", "", "Access key, e.g. user name when credential type is 'basic'")
flags.StringVarP(&opts.Credential.AccessKey, "credential-access-secret", "", "", "Access secret, e.g. password when credential type is 'basic'")
flags.StringVarP(&opts.Credential.Type, "credential-type", "", "", "Credential type, such as 'basic', 'oauth'")

return cmd
}
Loading