-
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 commit adds the project get command for the user to get the project by name or ID. Signed-off-by: Akshat <akshat25iiit@gmail.com>
- Loading branch information
1 parent
49ee28f
commit f9412b8
Showing
3 changed files
with
65 additions
and
0 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,48 @@ | ||
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/spf13/cobra" | ||
) | ||
|
||
type getProjectOptions struct { | ||
projectNameOrID string | ||
} | ||
|
||
// NewGetProjectCommand creates a new `harbor get project` command | ||
func NewGetProjectCommand() *cobra.Command { | ||
var opts 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 runGetProject(opts, credentialName) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func runGetProject(opts getProjectOptions, credentialName string) error { | ||
client := utils.GetClientByCredentialName(credentialName) | ||
ctx := context.Background() | ||
response, err := client.Project.GetProject(ctx, &project.GetProjectParams{ProjectNameOrID: opts.projectNameOrID}) | ||
|
||
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