|
| 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 | +├── LICENSE |
| 25 | +├── README.md |
| 26 | +├── cmd |
| 27 | +│ ├── login |
| 28 | +│ │ └── login.go |
| 29 | +│ ├── project |
| 30 | +│ │ └── get_project.go |
| 31 | +│ ├── root.go |
| 32 | +│ └── utils |
| 33 | +│ └── utils.go |
| 34 | +├── go.mod |
| 35 | +├── go.sum |
| 36 | +└── main.go |
| 37 | +``` |
| 38 | + |
| 39 | +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. Each of the commands will be treated as an individual sub-package. |
| 40 | + |
| 41 | +``` |
| 42 | +cmd/ |
| 43 | +├── project |
| 44 | + ├── create_project.go |
| 45 | + ├── create_project_test.go |
| 46 | + ├── delete_project.go |
| 47 | + ├── delete_project_test.go |
| 48 | + ├── . |
| 49 | + ├── . |
| 50 | + ├── . |
| 51 | +``` |
| 52 | + |
| 53 | +<br> |
| 54 | + |
| 55 | +User credentials will be stored in `~/.harbor/config/auth.yaml` upon sign in and the same will be used to read the credentials to make the API calls. |
| 56 | + |
| 57 | +<br> |
| 58 | + |
| 59 | +[harbor/go-client](https://github.com/goharbor/go-client) will be used to make any API calls for any given server address. |
| 60 | + |
| 61 | +### Example Implementation for `get_project.go` |
| 62 | + |
| 63 | +```go |
| 64 | +package project |
| 65 | + |
| 66 | +import ( |
| 67 | + "context" |
| 68 | + |
| 69 | + "github.com/akshatdalton/harbor-cli/cmd/utils" |
| 70 | + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/project" |
| 71 | + "github.com/spf13/cobra" |
| 72 | +) |
| 73 | + |
| 74 | +type getProjectOptions struct { |
| 75 | + projectNameOrID string |
| 76 | +} |
| 77 | + |
| 78 | +// NewGetProjectCommand creates a new `harbor get project` command |
| 79 | +func NewGetProjectCommand() *cobra.Command { |
| 80 | + var opts getProjectOptions |
| 81 | + |
| 82 | + cmd := &cobra.Command{ |
| 83 | + Use: "project [NAME|ID]", |
| 84 | + Short: "get project by name or id", |
| 85 | + Args: cobra.ExactArgs(1), |
| 86 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 87 | + opts.projectNameOrID = args[0] |
| 88 | + return runGetProject(opts) |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + return cmd |
| 93 | +} |
| 94 | + |
| 95 | +func runGetProject(opts getProjectOptions) error { |
| 96 | + client := utils.GetClient(nil) |
| 97 | + ctx := context.Background() |
| 98 | + response, err := client.Project.GetProject(ctx, &project.GetProjectParams{ProjectNameOrID: opts.projectNameOrID}) |
| 99 | + |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + utils.PrintPayloadInJSONFormat(response) |
| 105 | + return nil |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +We will follow the verb-noun syntax, for example: |
| 110 | +``` |
| 111 | +harbor get project 1 |
| 112 | +``` |
0 commit comments