go-toolkit

command module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 2 Imported by: 0

README

# cobra-cli-template

This template provides a starting point for creating a CLI application using the Cobra library in Go.
It makes it easy to create a CLI application with subcommands, flags, and arguments.
It's a TDD centered approach to building CLI applications. It encourages the use of tests to ensure the correctness of the application's behavior.

.Tools
|===
| type | name

| utilities | samber/lo
| test runner | onsi/ginkgo
| assertion | testify/assert
| fake data | brianvoe/gofakeit/v7
|===


## Usage

[NOTE]
.To use this template you can use git to clone it. 
====
```sh
git clone https://github.com/louiss0/cobra-cli-template .
```
====


[NOTE]
.Or you could use `gh cli`
====
```sh
gh repo create {project name} --template louiss0/cobra-cli-template --public --clone
```
====

[NOTE]
.This is a TDD template which means you test commands by running a test suite.
====
To test commands, you can run the following command:

```sh
ginkgo run
```

To watch files for changes and re-run tests:

```sh
ginkgo watch
```
====

### Writing Commands

When you create a new command, you should follow these steps:

. Create a new file in the `cmd` directory with the name of the command.
. In the file, make a called New{CommandName}Cmd function that returns the command you see after using `cobra-cli add`.
. In the `init()` 's `rootCmd.AddCommand()` Use the function you created in step 2.
. In the function write the flags and arguments for the command after the variable creation.
. Write tests for the command in the `cmd` directory.

TIP: Use the `cobra-cli add` command to create a new command.

NOTE: Remember to always add arguments to your commands.

CAUTION: Gingko will run tests in parallel by default.


## Project structure

```sh
├── cmd
│   └── root.go
├── cmd_test.go
├── cobra_cli_template_suite_test.go
├── custom_errors
│   └── root.go
├── custom_flags
│   └── root.go
├── go.mod
├── go.sum
├── LICENSE
├── main.go
```

This project has the cmd package and two other packages.
The `custom_errors` and `custom_flags` package. 
The custom errors package was created to created named errors.
The custom flags package is created for generic flags. 

The custom_flags is filled with flags that are meant to be used in cobra's `cmd.Flags().Var()` methods.
To use them you must create each struct in a variable then use each variable as a reference to each function.
I decided to use the word `root.go` as the name of each file because it makes it clear that it's the main file.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package custom_errors provides error handling functionality for flag-related and argument-related operations.
Package custom_errors provides error handling functionality for flag-related and argument-related operations.
Package custom_flags provides custom flag types for command-line argument parsing.
Package custom_flags provides custom flag types for command-line argument parsing.
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL