Skip to content

Commit

Permalink
Add create project cmd
Browse files Browse the repository at this point in the history
This add support for the create project cmd.
User can also pass different options while
creating a project.

Signed-off-by: Akshat <akshat25iiit@gmail.com>
  • Loading branch information
akshatdalton committed May 15, 2023
1 parent 94cafa1 commit d285cdf
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package constants
const (
HarborCredentialName = "HARBORCREDENTIALNAME"
CredentialNameOption = "credential-name"
CredentialNameHelp = "Name of the credential to use for authentication (defaults to the current logged in session)"
)
56 changes: 56 additions & 0 deletions cmd/project/create_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package project

import (
"context"

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

type createProjectOptions struct {
projectName string
public bool
registryID int64
storageLimit int64
}

// NewCreateProjectCommand creates a new `harbor create project` command
func NewCreateProjectCommand() *cobra.Command {
var opts createProjectOptions

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

flags := cmd.Flags()
flags.StringVarP(&opts.projectName, "name", "", "", "Name of the project")
flags.BoolVarP(&opts.public, "public", "", true, "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
}

func runCreateProject(opts createProjectOptions, credentialName string) error {
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}})

if err != nil {
return err
}

utils.PrintPayloadInJSONFormat(response)
return nil
}
18 changes: 16 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func newGetCommand() *cobra.Command {
Long: `Get project, registry`,
}

cmd.PersistentFlags().String(constants.CredentialNameOption, "", "Name of the credential to use for authentication")
cmd.PersistentFlags().String(constants.CredentialNameOption, "", constants.CredentialNameHelp)
cmd.AddCommand(project.NewGetProjectCommand())
cmd.AddCommand(registry.NewGetRegistryCommand())
return cmd
Expand All @@ -30,16 +30,30 @@ func newListCommand() *cobra.Command {
Long: `List project, registry`,
}

cmd.PersistentFlags().String(constants.CredentialNameOption, "", "Name of the credential to use for authentication")
cmd.PersistentFlags().String(constants.CredentialNameOption, "", constants.CredentialNameHelp)
cmd.AddCommand(project.NewListProjectCommand())
cmd.AddCommand(registry.NewListRegistryCommand())
return cmd
}

// 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.NewCreateProjectCommand())
return cmd
}

func addCommands(cmd *cobra.Command) {
cmd.AddCommand(login.NewLoginCommand())
cmd.AddCommand(newGetCommand())
cmd.AddCommand(newListCommand())
cmd.AddCommand(newCreateCommand())
}

// CreateHarborCLI creates a new Harbor CLI
Expand Down

0 comments on commit d285cdf

Please sign in to comment.