An open API service indexing awesome lists of open source software.

https://github.com/squidmin/gocli

Command-Line Interface (CLI) template built with Go, leveraging the Cobra library
https://github.com/squidmin/gocli

cli cobra cobra-cli go golang template template-project

Last synced: about 1 year ago
JSON representation

Command-Line Interface (CLI) template built with Go, leveraging the Cobra library

Awesome Lists containing this project

README

          

# gocli

`gocli` is a simple Command-Line Interface (CLI) application built with Go, leveraging the Cobra library for easy command management.
It is a template and should be treated as a starting point for building your own CLI applications.

In this document you can find instructions for building, running, and extending `gocli`.

## Prerequisites

- Go 1.16 or later
- A code editor or IDE
- Basic knowledge of Go and the command line

## Installation

Clone the repository:

```shell
git clone
```

Navigate to the project directory:

```shell
cd gocli
```

## Project Structure

The project structure is as follows:

```
├── cmd
│ └── cli
│ └── main.go
├── go.mod
├── go.sum
└── README.md
```

- `cmd/cli/main.go`: The main entry point for the application.
- `go.mod` and `go.sum`: Go module files that define the module's dependencies.
- `README.md`: The project documentation.

## Building the Application

To compile the `gocli` application and generate an executable file, follow these steps:

1. Navigate to the `cmd/cli` directory:
```shell
cd cmd/cli
```

This compiles your program and generates an executable file.

2. Build the application:
```shell
go build -o gocli
```

This creates an executable file named `gocli` in the same directory.

A build script (`build.sh`) is also provided for convenience. You can run it using the following command:
```shell
./build.sh
```

This process compiles the program into an executable named gocli in the current directory.

## Running the Application

```shell
chmod +x gocli
./gocli
```

Upon successful execution, you will see the application's output, indicating that `gocli` is running properly.

## Extending the Application

### Adding new commands

The `gocli` application can be easily extended with additional commands using the Cobra library.
Below is an example of how to add a `version` command to your CLI application:

1. **Define the `version` command**: \
Add the following code to your `main.go` or an appropriate file within the `cmd/cli` directory:
```go
package main

import (
"fmt"
"github.com/spf13/cobra"
)

func main() {
// Define the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the version of gocli",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("gocli version 1.0.0")
},
}
}
```
2. **Add the `version` command to the root command**: \
Add the following code to the `main` function in `main.go`:
```go
func main() {
// Define the root command
var rootCmd = &cobra.Command{Use: "gocli"}

// Add the version command to the root command
rootCmd.AddCommand(versionCmd)

// Execute the root command
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
```
3. **Rebuild the application**: \
After adding the new command, rebuild the application using the build steps mentioned earlier.
4. **Run the `version` command**: \
After rebuilding the application, you can run the `version` command by executing the following command:
```shell
./gocli version
```
You should see the output: `gocli version 1.0.0`, indicating that the `version` command is functioning correctly.

---

## Conclusion

This documentation provides the essential steps to build, run, and extend the `gocli` application.
With the Cobra library, adding more commands and functionality to your CLI tool is straightforward, allowing for scalable and maintainable command-line applications.