- Make sure you have
Go
installed. - Make sure you have
$GOBIN
set and added to yourPATH
.
- Download the latest version of
Cobra
using:go install github.com/spf13/cobra-cli@latest
- Run the
cobra-cli
commad to verify installation.
-
Create a new
go
project.- I'll name mine
DadJoke-CLI-with-Cobra
.
- I'll name mine
-
Initialize the
go mod
using:go mod init github.com/sheryarbutt/DadJoke-CLI-with-Cobra
-
Initialize the
cobra
command using:cobra-cli init
This will create a main.go
file and a cmd
directory in the project.
-
Navgite to the
cmd
directory and update Long and Short descriptions inroot.go
. -
Verify proper initialization using:
go run main.go
- Add new command called
random
using:cobra-cli add random
- This will add a new
random.go
file to thecmd
directory. - Navgite to the
cmd
directory and update Long and Short descriptions inrandom.go
. - Add code to handle the
random
command inrandom.go
.- I'm using
icanhazdadjoke.com
to get random dad jokes through their API.
- I'm using
- Run the CLI application using:
go run main.go
- Get a random joke using:
go run main.go random