add --config flag to global cli options. closes #30

This commit is contained in:
Danny 2018-05-15 11:07:27 +02:00
parent 8b639d348d
commit 1f3dc51b26
3 changed files with 37 additions and 12 deletions

View File

@ -1,12 +1,12 @@
package main package main
import ( import (
log "github.com/sirupsen/logrus"
"math/rand" "math/rand"
"os" "os"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig" "github.com/kelseyhightower/envconfig"
log "github.com/sirupsen/logrus"
"github.com/usefathom/fathom/pkg/datastore" "github.com/usefathom/fathom/pkg/datastore"
) )
@ -16,12 +16,17 @@ type Config struct {
Secret string Secret string
} }
func parseConfig() *Config { func parseConfig(file string) *Config {
var cfg Config var cfg Config
godotenv.Load()
err := envconfig.Process("Fathom", &cfg) err := godotenv.Load(file)
if err != nil && file != ".env" {
log.Fatalf("error parsing config file: %s", err)
}
err = envconfig.Process("Fathom", &cfg)
if err != nil { if err != nil {
log.Fatalf("Error parsing Fathom config from environment: %s", err) log.Fatalf("error parsing config from environment values: %s", err)
} }
// alias sqlite to sqlite3 // alias sqlite to sqlite3

View File

@ -4,21 +4,29 @@ import (
"log" "log"
"os" "os"
"github.com/jmoiron/sqlx"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/usefathom/fathom/pkg/datastore" "github.com/usefathom/fathom/pkg/datastore"
) )
func main() { var db *sqlx.DB
cfg := parseConfig() var config *Config
db := datastore.Init(cfg.Database)
defer db.Close()
func main() {
app := cli.NewApp() app := cli.NewApp()
app.Name = "Fathom" app.Name = "Fathom"
app.Usage = "simple & transparent website analytics" app.Usage = "simple & transparent website analytics"
app.Version = "1.0.0" app.Version = "1.0.0"
app.HelpName = "fathom" app.HelpName = "fathom"
app.Flags = []cli.Flag{} app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: ".env",
Usage: "Load configuration from `FILE`",
},
}
app.Before = before
app.After = after
app.Commands = []cli.Command{ app.Commands = []cli.Command{
{ {
Name: "server", Name: "server",
@ -34,7 +42,7 @@ func main() {
}, },
cli.BoolFlag{ cli.BoolFlag{
EnvVar: "FATHOM_DEBUG", EnvVar: "FATHOM_DEBUG",
Name: "debug", Name: "debug, d",
}, },
}, },
}, },
@ -50,5 +58,17 @@ func main() {
err := app.Run(os.Args) err := app.Run(os.Args)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
os.Exit(1)
} }
} }
func before(c *cli.Context) error {
config = parseConfig(c.String("config"))
db = datastore.Init(config.Database)
return nil
}
func after(c *cli.Context) error {
db.Close()
return nil
}

View File

@ -1,11 +1,11 @@
package main package main
import ( import (
log "github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/usefathom/fathom/pkg/datastore" "github.com/usefathom/fathom/pkg/datastore"
"github.com/usefathom/fathom/pkg/models" "github.com/usefathom/fathom/pkg/models"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"log"
) )
func register(c *cli.Context) error { func register(c *cli.Context) error {