Skip to content

theskyinflames/cmdarchetype

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cmdarchetype

This is an example of archetype of command line tool using cobra and 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:

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:

❯ 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:

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 !!

Releases

No releases published

Packages

No packages published