Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rudifa/go-verse
Verse ... the genessis of a cli application using go and cobra
https://github.com/rudifa/go-verse
Last synced: 24 days ago
JSON representation
Verse ... the genessis of a cli application using go and cobra
- Host: GitHub
- URL: https://github.com/rudifa/go-verse
- Owner: rudifa
- License: apache-2.0
- Created: 2023-05-09T11:01:05.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-05-10T15:03:24.000Z (over 1 year ago)
- Last Synced: 2024-10-06T01:41:24.854Z (about 1 month ago)
- Language: Go
- Size: 8.82 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Verse ... the genessis of a cli application
## Create a go + cobra project
- preconditions: are go and cobra-cli installed?
```
verse % go version
go version go1.20.3 darwin/amd64verse % cobra-cli
Cobra is a CLI library for Go that empowers applications.
...
```- recommended: vscode, with extension `Go for Visual Studio Code` installed
- create the project skeleton
```
golang % mkdir verse && cd verseverse % go mod init verse
verse % cobra-cli init
Your Cobra application is ready at
/Users/rudifarkas/GitHub/golang/verse
```- look at what we have
```
verse % tree
.
├── cmd
│ └── root.go
├── LICENSE
├── go.mod
├── go.sum
└── main.go2 directories, 5 files
```- run (builds an executable in memory and runs it)
```
verse % go run .
A longer description that spans multiple lines and ...
```- build an executable and run it
```
verse % go build .verse % ./verse
A longer description that spans multiple lines and ...
```- create the git repo
```
verse % echo '# see also https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore' > .gitignore
verse % echo '.DS_Store' >> .gitignoreverse % git init
Initialized empty Git repository in /Users/rudifarkas/GitHub/golang/verse/.git/verse % git add .
verse % git commit -m 'initial commit'
[main (root-commit) d5e8577] initial commit
...
```- add the first command
```
verse % cobra-cli add future
future created at /Users/rudifarkas/GitHub/golang/verseverse % go run .
A longer description that spans multiple lines and ...
...
Usage:
verse [command]Available Commands:
completion Generate the autocompletion script for the specified shell
future A brief description of your command
help Help about any commandFlags:
-h, --help help for verse
-t, --toggle Help message for toggleUse "verse [command] --help" for more information about a command.
verse % go run . future
future called
```- look again
```
verse % tree
.
├── cmd
│ ├── future.go
│ └── root.go
├── LICENSE
├── README.md
├── go.mod
├── go.sum
├── main.go
└── verse
```- in `cmd/root.go` add the 2 lines (optional \*)
```
func Execute() {
cobra.EnablePrefixMatching = true // allow abbreviations
rootCmd.CompletionOptions.DisableDefaultCmd = true // disable default completion command
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
```(\*) my preferences: with `allows...`, it suffices to type just the first few letters of a command name; with `disables...` the app will not offer to generate shell scripts for installing command completions in the shell environment`
- now edit the help texts in `root.go`and `future.go` to something appropriate, then run
```
verse % go run . help
This is a go application that uses cobra to add CLI functionality.
...
Available Commands:
future A look into the future
help Help about any commandverse % go run . help fut
Future is now, go for it.
...
```- commit
```
git add .
verse % git commit -m 'add command future and update help messages'
```- now add a package to which to delegate the actual work for the command `future`
```
verse % mkdir pkg
verse % mkdir pkg/future
verse % touch pkg/future/peek.go
```- add code to `pkg/future/peek.go`
```
package futureimport (
"os/exec"
)func OpenOpenAIPage() error {
cmd := exec.Command("open", "https://openai.com")
return cmd.Run()
}
```- in `cmd/future.go` call `future.OpenOpenAIPage()`
```
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("future called")
future.OpenOpenAIPage()
},
```When you save above change, the vscode go extension inserts for you
```
import (
"fmt"
"verse/pkg/future""github.com/spf13/cobra"
)
```- run it and enjoy the view
```
verse % go run . fut
```- look yet again at the project structure
```
verse % tree
.
├── cmd
│ ├── future.go
│ └── root.go
├── pkg
│ └── future
│ └── peek.go
├── LICENSE
├── README.md
├── go.mod
├── go.sum
├── main.go
└── verse
```Normally you don't edit files `go.mod` `go.sum`, managed by the go tools. Their contents reflect the packages imported into the project, directly or as depenedencies.
- commit, create a github repo and push, update LICENSE, ...
There we go.