Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phenixblue/hellogo-cli
An example of a simple Golang CLI for helping others get Go'ing
https://github.com/phenixblue/hellogo-cli
Last synced: about 1 month ago
JSON representation
An example of a simple Golang CLI for helping others get Go'ing
- Host: GitHub
- URL: https://github.com/phenixblue/hellogo-cli
- Owner: phenixblue
- License: apache-2.0
- Created: 2020-10-09T22:09:39.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2020-10-09T23:07:51.000Z (about 4 years ago)
- Last Synced: 2024-06-20T11:49:25.179Z (5 months ago)
- Language: Go
- Size: 22.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hellogo-cli
An example of a simple Golang CLI for helping others get Go'ing
This is meant as a learning tool and nothing else. Do NOT depend on this for anything!
## Root Command
```shell
$ ./hellogo-cliThis CLI used to show others a working model of the various
features that Cobra offers. It's also a great example of just
how easy it is to build a portable CLI with Go + CobraUsage:
hellogo-cli [command]Available Commands:
command1 Command 1 does a thing
command2 Command 2 has flags
command3 Command 3 has sub-commands
help Help about any commandFlags:
--config string config file (default is $HOME/.hellogo-cli.yaml)
-h, --help help for hellogo-cli
-t, --toggle Help message for toggleUse "hellogo-cli [command] --help" for more information about a command.
```## Command1
Command 1 will list files in your current working directory
```shell
$ ./hellogo-cli command1command1 called
.git
LICENSE
README.md
cmd
go.mod
go.sum
hellogo-cli
main.go
pkg
```## Command 2
Command 2 has flags that can be used to influence the execution of code
```none
Usage:
hellogo-cli command2 [flags]Flags:
-h, --help help for command2
-n, --name string A name to reference for a custom greeting (default "Stranger")
-t, --toggle-name Toggle name specific greeting
```Example 1:
```shell
$ ./hellogo-cli command2command2 called
Hello Stranger, I hope you are having a great day!
```Example 2:
```shell
$ ./hellogo-cli command2 --name Joecommand2 called
Hello Joe, I hope you are having a great day!
```Example 3:
```shell
$ ./hellogo-cli command2 --toggle-namecommand2 called
I hope you are having a great day!
```## Command 3
```none
Usage:
hellogo-cli command3 [flags]
hellogo-cli command3 [command]Available Commands:
subcommand1 This is the first sub-command for Command 3
subcommand2 This is the second sub-command for Command 3
```Example 1:
```shell
$ ./hellogo-cli command3 subcommand1subcommand1 called
```## Building CLI Binary
There are many ways to build a Go binary, but one of the simplest methods is this:
```shell
$ go build
```## Bootstrapping CLI files
[Cobra](https://github.com/spf13/cobra) makes it really easy to bootstrap/scaffold a CLI project. Here are the steps I used for this example:
### Start with fresh Git repo cloned locally**
```shell
$ git clone [email protected]:phenixblue/hellogo-cli.git
```### Initialize your Go project
```shell
$ go mod init github.com/phenixblue/hellogo-cli
```### Intialize your Cobra application
```shell
$ cobra init --pkg-name github.com/phenixblue/hellogo-cli -a "The WebRoot, Inc."
```### Add Commands
```shell
$ cobra add command1
$ cobra add command2
$ cobra add command3
```### Add Sub-Commands
This adds sub-commands by adding the `-p` flag and specifying `command3` as the parent command.
```shell
$ cobra add subcommand1 -p command3Cmd
$ cobra add subcommand2 -p command3Cmd
```