Skip to content

Commit

Permalink
Merge branch 'main' into dagger-cicd
Browse files Browse the repository at this point in the history
Signed-off-by: Bishal Das  <70086051+bishal7679@users.noreply.github.com>
  • Loading branch information
bishal7679 authored Apr 27, 2024
2 parents 78151ca + 59ef18f commit 470618d
Show file tree
Hide file tree
Showing 23 changed files with 529 additions and 455 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
}
93 changes: 63 additions & 30 deletions cmd/harbor/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,84 @@ import (
"github.com/goharbor/go-client/pkg/harbor"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/user"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/login"
"github.com/spf13/cobra"
)

type loginOptions struct {
name string
var (
serverAddress string
username string
password string
}
Username string
Password string
Name string
)

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

cmd := &cobra.Command{
Use: "login [SERVER]",
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 runLogin(opts)
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
serverAddress = args[0]
}

loginView := login.LoginView{
Server: serverAddress,
Username: Username,
Password: Password,
Name: Name,
}

var err error

if loginView.Server != "" && loginView.Username != "" && loginView.Password != "" && loginView.Name != "" {
err = runLogin(opts)
} else {
err = createLoginView(&loginView)
}

if err != nil {
fmt.Println(err)
}

},
}

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

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)
}
flags.StringVarP(&Username, "username", "u", "", "Username")
flags.StringVarP(&Password, "password", "p", "", "Password")

return cmd
}

func runLogin(opts loginOptions) error {

func createLoginView(loginView *login.LoginView) error {
if loginView == nil {
loginView = &login.LoginView{
Server: "",
Username: "",
Password: "",
Name: "",
}
}

login.CreateView(loginView)

return runLogin(*loginView)

}

func runLogin(opts login.LoginView) error {
clientConfig := &harbor.ClientSetConfig{
URL: opts.serverAddress,
Username: opts.username,
Password: opts.password,
URL: opts.Server,
Username: opts.Username,
Password: opts.Password,
}
client := utils.GetClientByConfig(clientConfig)

Expand All @@ -62,14 +95,14 @@ func runLogin(opts loginOptions) error {
}

cred := utils.Credential{
Name: opts.name,
Username: opts.username,
Password: opts.password,
ServerAddress: opts.serverAddress,
Name: opts.Name,
Username: opts.Username,
Password: opts.Password,
ServerAddress: opts.Server,
}

if err = utils.StoreCredential(cred, true); err != nil {
return fmt.Errorf("Failed to store the credential: %s", err)
if err = utils.AddCredentialsToConfigFile(cred,utils.DefaultConfigPath); err != nil {
return fmt.Errorf("failed to store the credential: %s", err)
}
return nil
}
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
Loading

0 comments on commit 470618d

Please sign in to comment.