-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proposal for official CLI project for Harbor
Signed-off-by: Akshat <[email protected]>
- Loading branch information
1 parent
ebc90f3
commit 559fd81
Showing
1 changed file
with
130 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# Proposal: Add Official CLI for Harbor | ||
|
||
Author: Akshat/[akshatdalton](https://github.com/akshatdalton) | ||
|
||
## Abstract | ||
|
||
This proposal aims to add the official CLI for Harbor. It will have basic features like user login and other CRUD operations like create project, get project, list projects, etc. | ||
|
||
## Background | ||
|
||
There are some unofficial CLI projects to interact with Harbor API, and now Harbor wants to provide official support for CLI. | ||
|
||
## Goals | ||
|
||
- Implement official CLI for Harbor. | ||
- Support basic CRUD operations like create project, get project, list projects, etc. | ||
|
||
## Implementation | ||
|
||
### Directory structure | ||
|
||
``` | ||
cli/ | ||
├── main.go | ||
├── go.sum | ||
├── go.mod | ||
├── README.md | ||
├── .gitignore | ||
└── cmd/ | ||
├── root.go | ||
├── create_project.go | ||
├── delete_project.go | ||
├── login.go | ||
├── logout.go | ||
├── util.go | ||
├── . | ||
├── . | ||
└── . | ||
``` | ||
|
||
I will be using [cobra](https://github.com/spf13/cobra) to make this CLI tool and it will have the directory structure as shown above. For each of the commands, either we can have a single file or an individual sub-package for them. | ||
|
||
E.g.:<br> | ||
|
||
- for a single file - | ||
|
||
``` | ||
cmd/ | ||
├── create_project.go | ||
├── create_project_test.go | ||
├── delete_project.go | ||
├── delete_project_test.go | ||
├── . | ||
├── . | ||
├── . | ||
``` | ||
- for an individual sub-package - | ||
``` | ||
cmd/ | ||
├── project | ||
├── project.go | ||
├── create_project.go | ||
├── create_project_test.go | ||
├── delete_project.go | ||
├── delete_project_test.go | ||
├── . | ||
├── . | ||
├── . | ||
``` | ||
<br> | ||
User credentials will be stored in `~/.harbor/auth.yaml` upon sign in and the same will be used to read the credentials to make the API calls. | ||
<br> | ||
[harbor/go-client](https://github.com/goharbor/go-client) will be used to make any API calls for any given server address. | ||
### Example Implementation for `get_project.go` | ||
```go | ||
package project | ||
import ( | ||
"fmt" | ||
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/project" | ||
"github.com/spf13/cobra" | ||
"github.com/goharbor/cli/cmd" | ||
) | ||
type getOptions struct { | ||
ProjectNameOrID string | ||
} | ||
var opts getOptions | ||
// getProjectCmd represents the get project command | ||
var getProjectCmd = &cobra.Command{ | ||
Use: "get", | ||
Short: "get a project by name or ID", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
params := project.GetProjectParams{ | ||
ProjectNameOrID: opts.ProjectNameOrID, | ||
} | ||
// projectApiClient is defined in the `project.go` file | ||
response, err := projectApiClient.GetProject(cliContext, ¶ms) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(-1) | ||
} | ||
jsonStr := JsonMarshallWithMultipleTypes(response) | ||
fmt.Println(jsonStr) | ||
}, | ||
} | ||
func init() { | ||
flags := getProjectCmd.Flags() | ||
flags.StringVarP(&opts.ProjectNameOrID, "ProjectNameOrID", "", "", "project name or project ID") | ||
if err := getProjectCmd.MarkFlagRequired("ProjectNameOrID"); err != nil { | ||
panic(err) | ||
} | ||
rootCmd.AddCommand(getProjectCmd) | ||
} | ||
``` |