https://github.com/theskyinflames/cmdarchetype
An archetype of command line tool using Cobra and Viper
https://github.com/theskyinflames/cmdarchetype
archetype cobra command-line-tool golang golang-tools theskyinflames viper
Last synced: over 1 year ago
JSON representation
An archetype of command line tool using Cobra and Viper
- Host: GitHub
- URL: https://github.com/theskyinflames/cmdarchetype
- Owner: theskyinflames
- License: apache-2.0
- Created: 2019-03-08T20:55:35.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-14T07:07:53.000Z (about 7 years ago)
- Last Synced: 2025-01-05T17:11:52.417Z (over 1 year ago)
- Topics: archetype, cobra, command-line-tool, golang, golang-tools, theskyinflames, viper
- Language: Go
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cmdarchetype
This is an example of archetype of command line tool using [cobra](https://github.com/spf13/cobra) and [viper](https://github.com/spf13/viper). The command parameters can be loaded from a file, specified inline, or a mix of the two options.
## How it works
This is the sequence to set the parameters values:
1. If a parameter value is specified in the command line, this value will be taken
2. If a config file is specified, and it contais a value for the parameter, it will be taken
3. Otherwise, if a default value for the parameter has been set, it will be taken
There is a config file as example:
```yalm
do-async: false
source-data: https://data.safe.net:443
result-receivers:
- receiver1
- receiver2
- receiver3
db-connection-params:
user: myuser
password: mypassword
db-url: https://db.safe.net:443
```
## Environment used to build this archetype
* Go: go1.11.5 linux/amd64
* Make: GNU Make 4.2.1
## Execute the command
As I've said above, if a config file is specified, its parameters will be loaded. In adition, we can specify some different value for a given paraemter:
```sh
❯ go run main.go -c=./example-config.yml --do-async=true --db-url=myDB
```
With the above showed config file, this is the config loaded:
```sh
INFO[0000] loading config from [./example-config.yml] file
INFO[0000] loaded config: (*config.Config)(0xc000098900)({
SourceData: (string) (len=25) "https://data.safe.net:443",
ResultReceivers: ([]string) (len=3 cap=3) {
(string) (len=9) "receiver1",
(string) (len=9) "receiver2",
(string) (len=9) "receiver3"
},
DBConnectionParams: (config.DBConnectionParams) {
User: (string) (len=6) "myuser",
Password: (string) (len=10) "mypassword",
DBURL: (string) (len=4) "myDB"
},
DoAsynchronously: (bool) true
})
INFO[0000] starting the command at 2019-03-11 20:30:14.50096253 +0100 CET m=+0.001722683
INFO[0001] Action done sucessfully !!!, in 1.000157953s
```
As you can see, in the config file, the parameter *db-url* value is *https://db.safe.net:443*, but executing the tool on this way, I've forced the value for this parameter to *myDB*
Of course, passing a config file is not mandatory. I such case, you'll have to pass all of parameters by command line. Otherwise, the default values will be taken.
Enjoy it !!