[WIP] add viper to manage envvars and cfg files

This commit is contained in:
1138-4EB
2019-01-06 06:11:15 +01:00
parent 09a6ffb1f3
commit 1080bfde68
7 changed files with 261 additions and 111 deletions

View File

@@ -8,8 +8,34 @@ import (
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/filebrowser/filebrowser/v2/storage/bolt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
v "github.com/spf13/viper"
)
func vaddP(f *pflag.FlagSet, k, p string, i interface{}, u string) {
switch y := i.(type) {
case bool:
f.BoolP(k, p, y, u)
case int:
f.IntP(k, p, y, u)
case string:
f.StringP(k, p, y, u)
}
v.SetDefault(k, i)
}
func vadd(f *pflag.FlagSet, k string, i interface{}, u string) {
switch y := i.(type) {
case bool:
f.Bool(k, y, u)
case int:
f.Int(k, y, u)
case string:
f.String(k, y, u)
}
v.SetDefault(k, i)
}
func checkErr(err error) {
if err != nil {
panic(err)
@@ -41,6 +67,7 @@ func mustGetUint(cmd *cobra.Command, flag string) uint {
}
func getDB() *storm.DB {
databasePath := v.GetString("database")
if _, err := os.Stat(databasePath); err != nil {
panic(errors.New(databasePath + " does not exist. Please run 'filebrowser init' first."))
}