go-paste/cmd/paste/config.go

119 lines
2.5 KiB
Go
Raw Normal View History

2017-04-14 16:15:07 -06:00
package main
import (
"bytes"
"fmt"
2018-02-15 08:01:35 -07:00
2017-05-22 11:13:47 -06:00
"github.com/docopt/docopt.go"
"github.com/spf13/viper"
2018-03-13 13:38:04 -06:00
"sour.is/x/toolbox/httpsrv"
"sour.is/x/toolbox/log"
2018-03-16 08:10:34 -06:00
_ "sour.is/x/toolbox/stats"
2017-04-14 16:15:07 -06:00
)
var (
2018-02-15 08:01:35 -07:00
// AppVersion Application Version Number
AppVersion string
// AppBuild Application Build Number
AppBuild string
)
2018-02-15 08:01:35 -07:00
// AppName name of the application
var AppName = "Paste API"
// AppUsage displays runtime options
var AppUsage = `Paste API
2017-04-14 16:15:07 -06:00
Usage:
paste version
paste [ -v | -vv ] serve
2017-04-14 16:15:07 -06:00
Options:
-v Log info to console.
-vv Log debug to console.
2017-04-14 16:15:07 -06:00
-l <ListenAddress>, --listen=<ListenAddress> Address to listen on.
-c <ConfigDir>, --config=<ConfigDir> Set Config Directory.
2017-04-14 16:15:07 -06:00
Config:
The config file is read from the following locations:
- <ConfigDir>
- /etc/opt/sour.is/paste/
2017-04-14 16:15:07 -06:00
- Working Directory
`
2018-02-15 08:01:35 -07:00
var defaultConfig = `
[http]
listen = ":9010"
[module.paste]
random = "4096"
store = "data/"
`
2017-04-14 16:15:07 -06:00
var args map[string]interface{}
func init() {
var err error
2018-02-15 08:01:35 -07:00
if args, err = docopt.Parse(AppUsage, nil, true, AppVersion, false); err != nil {
2017-04-14 16:15:07 -06:00
log.Fatal(err)
}
if args["-v"].(int) == 1 {
log.SetVerbose(log.Vinfo)
}
if args["-v"].(int) == 2 {
log.SetVerbose(log.Vdebug)
log.Debug("Debug Logging.")
2017-04-14 16:15:07 -06:00
}
viper.SetConfigName("config")
2017-05-22 11:13:47 -06:00
if args["--config"] != nil {
viper.AddConfigPath(args["--config"].(string))
}
viper.AddConfigPath("/etc/opt/sour.is/paste/")
2017-04-14 16:15:07 -06:00
viper.AddConfigPath(".")
viper.SetConfigType("toml")
viper.ReadConfig(bytes.NewBuffer([]byte(defaultConfig)))
2017-04-14 16:15:07 -06:00
err = viper.MergeInConfig()
if err != nil { // Handle errors reading the config file
log.Fatalf("Fatal error config file: %s \n", err)
2017-04-14 16:15:07 -06:00
}
2018-02-15 08:01:35 -07:00
viper.Set("app.name", AppName)
viper.SetDefault("app.version", "VERSION")
2018-02-15 08:01:35 -07:00
if AppVersion != "" {
viper.Set("app.version", AppVersion)
}
2017-04-14 16:15:07 -06:00
viper.SetDefault("app.build", "SNAPSHOT")
2018-02-15 08:01:35 -07:00
if AppBuild != "" {
viper.Set("app.build", AppBuild)
}
2017-04-14 16:15:07 -06:00
if args["serve"] == true {
if args["--listen"] != nil {
2018-02-15 08:01:35 -07:00
viper.Set("http.listen", args["--listen"].(string))
2017-04-14 16:15:07 -06:00
}
log.Noticef("Startup: %s (%s %s)",
viper.GetString("app.name"),
viper.GetString("app.version"),
viper.GetString("app.build"))
log.Notice("Read config from: ", viper.ConfigFileUsed())
2018-02-15 08:01:35 -07:00
httpsrv.Config()
} else if args["version"] == true {
fmt.Printf("Version: %s (%s %s)\n",
viper.GetString("app.name"),
viper.GetString("app.version"),
viper.GetString("app.build"))
2017-04-14 16:15:07 -06:00
}
}