Featured image of post Golang CLI

Golang CLI

Building a Golang CLI with Cobra to manage PocketBase users, made during a Master's module at Sup De Vinci

This project was done during my first year of Master’s at Sup De Vinci, as part of a module on the Go language.

Introduction

The specifications provided by the lecturer asked for a Golang CLI for one of the following three imposed authentication solutions:

Documentation had to be provided, so that the project would be turnkey and usable by anyone.


Choosing the solutions

Backend

After comparing the 3 authentication solutions, I noticed that Authentik and Casdoor are very complete, so they take some time to get to grips with. Given the project deadline and its needs, I went with PocketBase. It is simple to deploy and use, and it meets exactly the requirements of the project.

PocketBase home page with the admin interface listing the records of the posts collection

PocketBase UI

Golang framework

For the Go framework, I saw that 2 frameworks are widely favoured in this area: Cobra 🐍 and Kong 🦍.

By following YouTube tutorials that used the Cobra framework, its ease of use let me quickly build a POC of my CLI with very intuitive flag handling and automated documentation. So I stuck with this framework. A huge number of projects use Cobra, as you can see in this list.

I also added the Viper package to the project, which handles configuration in the Golang project, to work with the conf.json file.

Finally, adding the promptUI package improves the UX, in particular with interactive prompts.

Interactive prompt


Architecture

CLI

For the architecture of the CLI, I chose to base it on Docker’s, which I personally find very intuitive and convenient to use, and which is also built with Cobra. So I went with the same command [resource] [action] [flags] model.

Diagram of the go-cli CLI: the root command, the user resource with the create, delete, inspect and update actions, and the version, login and logout commands

CLI architecture

The CLI can therefore log in to and out of PocketBase, and perform CRUD actions (Create, Read, Update, Delete) on the users in the backend, which meets the request in the specifications.

Once the architecture of my CLI was drawn up, the design phase was over and I could start programming the CLI to make it concrete.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
β”œβ”€β”€ cmd
β”‚ β”œβ”€β”€ auth
β”‚ β”‚ β”œβ”€β”€ login.go
β”‚ β”‚ └── logout.go
β”‚ β”œβ”€β”€ root.go
β”‚ β”œβ”€β”€ user
β”‚ β”‚ β”œβ”€β”€ create.go
β”‚ β”‚ β”œβ”€β”€ delete.go
β”‚ β”‚ β”œβ”€β”€ inspect.go
β”‚ β”‚ β”œβ”€β”€ update.go
β”‚ β”‚ └── user.go
β”‚ └── version
β”‚ └── version.go
β”œβ”€β”€ common
β”‚ └── common.go
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
β”œβ”€β”€ go-cli
└── main.go
  • main.go: initialises the program through the main() function, which is the entry point of the program.

  • cmd/: this folder holds all the commands available in the CLI. The files are organised according to the architecture of the CLI.

  • common/: a module that holds the functions used in several modules of the project.


Deployment

The deployment of the project comes down to a simple docker-compose.yaml file that creates the PocketBase container holding the SQLite database. The pb_data file is also mounted, to have a database already populated with users.

1
2
3
4
5
6
7
8
services:
  pocketbase:
    build: .
    container_name: pocketbase
    ports:
      - 8090:8080
    volumes:
      - ./pb_data:/pb/pb_data

The CLI itself runs as a binary that can be downloaded from the GitLab releases or built locally from the code.


Demo

Demo of using the CLI


Conclusion

This project was quite challenging, because it came right after the one-week course module on Golang. I had to dig further into the language to handle the project properly. Having done some C in the not-so-distant past, this project brought back some good memories. πŸ˜„

I also gained experience in choosing the right tools to meet the project’s expectations, and to save time on development.

For more details on this project, the code is available in the go-cli GitLab repository!

Last updated on Oct 12, 2026 18:43 CEST
Built with Hugo
Theme Stack designed by Jimmy