1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-12-23 04:21:18 +00:00

Use rconfig package for CLI argument parsing

This commit is contained in:
Knut Ahlers 2016-03-27 18:29:47 +02:00
parent 31d0ac0c7e
commit a707308b88

View file

@ -1,49 +1,35 @@
package config // import "github.com/Luzifer/mondash/config" package config // import "github.com/Luzifer/mondash/config"
import ( import (
"fmt"
"log" "log"
"net/url" "net/url"
"os"
"strings"
"github.com/spf13/pflag" "github.com/Luzifer/rconfig"
) )
var storageDrivers = []string{"s3", "file"} var storageDrivers = []string{"s3", "file"}
// Config is a storage struct for configuration parameters // Config is a storage struct for configuration parameters
type Config struct { type Config struct {
Storage string Storage string `flag:"storage" default:"s3" description:"Storage engine to use"`
BaseURL string BaseURL string `flag:"baseurl" env:"BASE_URL" description:" "The Base-URL the application is running on for example https://mondash.org"`
APIToken string APIToken string `flag:"api-token" env:"API_TOKEN" description:"API Token used for the /welcome dashboard (you can choose your own)"`
Listen string Listen string `flag:"listen" default:":3000" description:"Address to listen on"`
S3 struct { S3 struct {
Bucket string Bucket string `flag:"s3Bucket" env:"S3Bucket" "Bucket to use for S3 storage"`
} }
FileStorage struct { FileStorage struct {
Directory string Directory string `flag:"fileDirectory" default:"./" description:"Directory to use for plain text storage"`
} }
} }
// Load parses arguments / ENV variable to load configuration // Load parses arguments / ENV variable to load configuration
func Load() *Config { func Load() *Config {
cfg := &Config{} cfg := &Config{}
pflag.StringVar(&cfg.Storage, "storage", "s3", fmt.Sprintf("Storage engine to use (%s)", strings.Join(storageDrivers, ", "))) rconfig.Parse(cfg)
pflag.StringVar(&cfg.BaseURL, "baseurl", os.Getenv("BASE_URL"), "The Base-URL the application is running on for example https://mondash.org")
pflag.StringVar(&cfg.APIToken, "api-token", os.Getenv("API_TOKEN"), "API Token used for the /welcome dashboard (you can choose your own)")
pflag.StringVar(&cfg.Listen, "listen", ":3000", "Address to listen on")
// S3
pflag.StringVar(&cfg.S3.Bucket, "s3Bucket", os.Getenv("S3Bucket"), "Bucket to use for S3 storage")
// FileStorage
pflag.StringVar(&cfg.FileStorage.Directory, "fileDirectory", "./", "Directory to use for plain text storage")
pflag.Parse()
return cfg return cfg
} }