Skip to content

Commit

Permalink
Add proposal for official CLI project for Harbor
Browse files Browse the repository at this point in the history
Signed-off-by: Akshat <[email protected]>
  • Loading branch information
akshatdalton committed Apr 24, 2023
1 parent b4c27b4 commit aba26cb
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions proposals/new/cli.md
Original file line number Diff line number Diff line change
@@ -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
├── .
├── .
├── .
```

<br>

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.

<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 (
"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
```

0 comments on commit aba26cb

Please sign in to comment.