|
| 1 | +# Proposal: Add Official CLI for Harbor |
| 2 | + |
| 3 | +Author: Akshat/[akshatdalton](https://github.com/akshatdalton) |
| 4 | + |
| 5 | +## Abstract |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +## Background |
| 10 | + |
| 11 | +There are some unofficial CLI projects to interact with Harbor API, and now Harbor wants to provide official support for CLI. |
| 12 | + |
| 13 | +## Goals |
| 14 | + |
| 15 | +- Implement official CLI for Harbor. |
| 16 | +- Support basic CRUD operations like create project, get project, list projects, etc. |
| 17 | + |
| 18 | +## Implementation |
| 19 | + |
| 20 | +### Directory structure |
| 21 | + |
| 22 | +``` |
| 23 | +cli/ |
| 24 | +├── main.go |
| 25 | +├── go.sum |
| 26 | +├── go.mod |
| 27 | +├── README.md |
| 28 | +├── .gitignore |
| 29 | +└── cmd/ |
| 30 | + ├── root.go |
| 31 | + ├── create_project.go |
| 32 | + ├── delete_project.go |
| 33 | + ├── login.go |
| 34 | + ├── logout.go |
| 35 | + ├── util.go |
| 36 | + ├── . |
| 37 | + ├── . |
| 38 | + └── . |
| 39 | +``` |
| 40 | + |
| 41 | +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. |
| 42 | + |
| 43 | +E.g.:<br> |
| 44 | + |
| 45 | +- for a single file - |
| 46 | + |
| 47 | + ``` |
| 48 | + cmd/ |
| 49 | + ├── create_project.go |
| 50 | + ├── create_project_test.go |
| 51 | + ├── delete_project.go |
| 52 | + ├── delete_project_test.go |
| 53 | + ├── . |
| 54 | + ├── . |
| 55 | + ├── . |
| 56 | + ``` |
| 57 | +
|
| 58 | +- for an individual sub-package - |
| 59 | +
|
| 60 | + ``` |
| 61 | + cmd/ |
| 62 | + ├── project |
| 63 | + ├── project.go |
| 64 | + ├── create_project.go |
| 65 | + ├── create_project_test.go |
| 66 | + ├── delete_project.go |
| 67 | + ├── delete_project_test.go |
| 68 | + ├── . |
| 69 | + ├── . |
| 70 | + ├── . |
| 71 | + ``` |
| 72 | +
|
| 73 | +<br> |
| 74 | +
|
| 75 | +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. |
| 76 | +
|
| 77 | +<br> |
| 78 | +
|
| 79 | +[harbor/go-client](https://github.com/goharbor/go-client) will be used to make any API calls for any given server address. |
| 80 | +
|
| 81 | +### Example Implementation for `get_project.go` |
| 82 | +
|
| 83 | +```go |
| 84 | +package project |
| 85 | +
|
| 86 | +import ( |
| 87 | + "fmt" |
| 88 | +
|
| 89 | + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/project" |
| 90 | + "github.com/spf13/cobra" |
| 91 | + "github.com/goharbor/cli/cmd" |
| 92 | +) |
| 93 | +
|
| 94 | +type getOptions struct { |
| 95 | + ProjectNameOrID string |
| 96 | +} |
| 97 | +
|
| 98 | +var opts getOptions |
| 99 | +
|
| 100 | +// getProjectCmd represents the get project command |
| 101 | +var getProjectCmd = &cobra.Command{ |
| 102 | + Use: "get", |
| 103 | + Short: "get a project by name or ID", |
| 104 | + Run: func(cmd *cobra.Command, args []string) { |
| 105 | + params := project.GetProjectParams{ |
| 106 | + ProjectNameOrID: opts.ProjectNameOrID, |
| 107 | + } |
| 108 | +
|
| 109 | + // projectApiClient is defined in the `project.go` file |
| 110 | + response, err := projectApiClient.GetProject(cliContext, ¶ms) |
| 111 | + if err != nil { |
| 112 | + fmt.Println(err) |
| 113 | + os.Exit(-1) |
| 114 | + } |
| 115 | +
|
| 116 | + jsonStr := JsonMarshallWithMultipleTypes(response) |
| 117 | + fmt.Println(jsonStr) |
| 118 | + }, |
| 119 | +} |
| 120 | +
|
| 121 | +func init() { |
| 122 | + flags := getProjectCmd.Flags() |
| 123 | + flags.StringVarP(&opts.ProjectNameOrID, "ProjectNameOrID", "", "", "project name or project ID") |
| 124 | + if err := getProjectCmd.MarkFlagRequired("ProjectNameOrID"); err != nil { |
| 125 | + panic(err) |
| 126 | + } |
| 127 | +
|
| 128 | + rootCmd.AddCommand(getProjectCmd) |
| 129 | +} |
| 130 | +``` |
0 commit comments