-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
94cafa1
commit d285cdf
Showing
3 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters