mirror of
https://github.com/status-im/fathom.git
synced 2025-03-01 03:20:27 +00:00
add --config flag to global cli options. closes #30
This commit is contained in:
parent
8b639d348d
commit
1f3dc51b26
@ -1,12 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"math/rand"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/usefathom/fathom/pkg/datastore"
|
||||
)
|
||||
|
||||
@ -16,12 +16,17 @@ type Config struct {
|
||||
Secret string
|
||||
}
|
||||
|
||||
func parseConfig() *Config {
|
||||
func parseConfig(file string) *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 {
|
||||
log.Fatalf("Error parsing Fathom config from environment: %s", err)
|
||||
log.Fatalf("error parsing config from environment values: %s", err)
|
||||
}
|
||||
|
||||
// alias sqlite to sqlite3
|
||||
|
@ -4,21 +4,29 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/urfave/cli"
|
||||
"github.com/usefathom/fathom/pkg/datastore"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := parseConfig()
|
||||
db := datastore.Init(cfg.Database)
|
||||
defer db.Close()
|
||||
var db *sqlx.DB
|
||||
var config *Config
|
||||
|
||||
func main() {
|
||||
app := cli.NewApp()
|
||||
app.Name = "Fathom"
|
||||
app.Usage = "simple & transparent website analytics"
|
||||
app.Version = "1.0.0"
|
||||
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{
|
||||
{
|
||||
Name: "server",
|
||||
@ -34,7 +42,7 @@ func main() {
|
||||
},
|
||||
cli.BoolFlag{
|
||||
EnvVar: "FATHOM_DEBUG",
|
||||
Name: "debug",
|
||||
Name: "debug, d",
|
||||
},
|
||||
},
|
||||
},
|
||||
@ -50,5 +58,17 @@ func main() {
|
||||
err := app.Run(os.Args)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
"github.com/usefathom/fathom/pkg/datastore"
|
||||
"github.com/usefathom/fathom/pkg/models"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"log"
|
||||
)
|
||||
|
||||
func register(c *cli.Context) error {
|
||||
|
Loading…
x
Reference in New Issue
Block a user