From aba26cb628b390c710d902316f13d83d5be53fa6 Mon Sep 17 00:00:00 2001 From: Akshat Date: Sun, 26 Mar 2023 19:56:03 +0530 Subject: [PATCH] Add proposal for official CLI project for Harbor Signed-off-by: Akshat --- proposals/new/cli.md | 112 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 proposals/new/cli.md diff --git a/proposals/new/cli.md b/proposals/new/cli.md new file mode 100644 index 00000000..c369a8ec --- /dev/null +++ b/proposals/new/cli.md @@ -0,0 +1,112 @@ +# 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/ +├── LICENSE +├── README.md +├── cmd +│   ├── login +│   │   └── login.go +│   ├── project +│   │   └── get_project.go +│   ├── root.go +│   └── utils +│   └── utils.go +├── go.mod +├── go.sum +└── main.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. Each of the commands will be treated as an individual sub-package. + +``` +cmd/ +├── project + ├── create_project.go + ├── create_project_test.go + ├── delete_project.go + ├── delete_project_test.go + ├── . + ├── . + ├── . +``` + +
+ +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. + +
+ +[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 ( + "context" + + "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] + return runGetProject(opts) + }, + } + + return cmd +} + +func runGetProject(opts getProjectOptions) error { + client := utils.GetClient(nil) + ctx := context.Background() + response, err := client.Project.GetProject(ctx, &project.GetProjectParams{ProjectNameOrID: opts.projectNameOrID}) + + if err != nil { + return err + } + + utils.PrintPayloadInJSONFormat(response) + return nil +} +``` + +We will follow the verb-noun syntax, for example: +``` +harbor get project 1 +```