From d285cdf35916e9ce6ac3f9391aa3a74a7de975ad Mon Sep 17 00:00:00 2001 From: Akshat Date: Mon, 15 May 2023 23:59:48 +0530 Subject: [PATCH] Add create project cmd This add support for the create project cmd. User can also pass different options while creating a project. Signed-off-by: Akshat --- cmd/constants/constants.go | 1 + cmd/project/create_project.go | 56 +++++++++++++++++++++++++++++++++++ cmd/root.go | 18 +++++++++-- 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 cmd/project/create_project.go diff --git a/cmd/constants/constants.go b/cmd/constants/constants.go index 3240532a..28ec48f5 100644 --- a/cmd/constants/constants.go +++ b/cmd/constants/constants.go @@ -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)" ) diff --git a/cmd/project/create_project.go b/cmd/project/create_project.go new file mode 100644 index 00000000..03a4460a --- /dev/null +++ b/cmd/project/create_project.go @@ -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 +} diff --git a/cmd/root.go b/cmd/root.go index a06b3492..d2faeea9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 @@ -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