go-paste/cmd/paste/config.go

119 lines
2.4 KiB
Go
Raw Permalink Normal View History

2017-04-14 16:15:07 -06:00
package main
import (
"bytes"
2019-09-06 15:08:50 -06: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/log"
2019-09-06 15:08:50 -06:00
"sour.is/x/toolbox/log/event"
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"
2020-09-04 17:18:58 -06:00
store = "data/paste"
[module.artifact]
store = "data/artifact"
[module.image]
store = "data/image"
2020-09-07 10:45:20 -06:00
[module.short]
store = "data/meta.db"
2018-02-15 08:01:35 -07:00
`
2017-04-14 16:15:07 -06:00
var args map[string]interface{}
func init() {
var err error
2018-06-25 17:23:13 -06:00
viper.Set("app.name", AppName)
viper.SetDefault("app.version", "VERSION")
if AppVersion != "" {
viper.Set("app.version", AppVersion)
}
viper.SetDefault("app.build", "SNAPSHOT")
if AppBuild != "" {
viper.Set("app.build", AppBuild)
}
2020-03-23 19:26:27 -06:00
if args, err = docopt.ParseDoc(AppUsage); err != nil {
2017-04-14 16:15:07 -06:00
log.Fatal(err)
}
if args["-v"].(int) == 1 {
2019-09-06 15:08:50 -06:00
log.SetVerbose(event.VerbInfo)
log.Info("Verbose Logging.")
}
if args["-v"].(int) == 2 {
2019-09-06 15:08:50 -06:00
log.SetVerbose(event.VerbDebug)
log.Debug("Very Verbose Logging.")
2017-04-14 16:15:07 -06:00
}
2018-06-25 17:23:13 -06:00
log.Printf("%s (%s %s)\n",
viper.GetString("app.name"),
viper.GetString("app.version"),
viper.GetString("app.build"))
2018-06-25 17:23:13 -06:00
if args["serve"] == true {
2017-04-14 16:15:07 -06:00
2018-06-25 17:23:13 -06:00
viper.SetConfigName("config")
if args["--config"] != nil {
viper.AddConfigPath(args["--config"].(string))
}
2018-06-25 17:23:13 -06:00
viper.AddConfigPath("/etc/opt/sour.is/paste/")
viper.AddConfigPath(".")
2017-04-14 16:15:07 -06:00
2018-06-25 17:23:13 -06:00
viper.SetConfigType("toml")
2020-09-04 17:18:58 -06:00
_ = viper.ReadConfig(bytes.NewBuffer([]byte(defaultConfig)))
2017-04-14 16:15:07 -06:00
2018-06-25 17:23:13 -06:00
err = viper.MergeInConfig()
if err != nil { // Handle errors reading the config file
log.Warningf("config file not found: %s \n", err)
} else {
log.Notice("Read config from: ", viper.ConfigFileUsed())
}
2017-04-14 16:15:07 -06:00
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
}
}
}