fathom/pkg/cli/cli.go

71 lines
1.2 KiB
Go
Raw Normal View History

package cli
2018-05-14 12:11:52 +00:00
import (
"os"
2018-11-14 08:31:47 +00:00
"time"
2018-05-14 12:11:52 +00:00
log "github.com/sirupsen/logrus"
2018-05-14 12:11:52 +00:00
"github.com/urfave/cli"
"github.com/usefathom/fathom/pkg/config"
2018-05-14 12:11:52 +00:00
"github.com/usefathom/fathom/pkg/datastore"
)
type App struct {
*cli.App
database datastore.Datastore
config *config.Config
}
// CLI application
var app *App
2018-05-14 12:11:52 +00:00
func Run(v string) error {
// force all times in UTC, regardless of server timezone
2018-11-14 08:31:47 +00:00
time.Local = time.UTC
// setup CLI app
app = &App{cli.NewApp(), nil, nil}
2018-05-14 12:11:52 +00:00
app.Name = "Fathom"
app.Usage = "simple & transparent website analytics"
app.Version = v
2018-05-14 12:11:52 +00:00
app.HelpName = "fathom"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: ".env",
Usage: "Load configuration from `FILE`",
},
}
app.Before = before
app.After = after
2018-05-14 12:11:52 +00:00
app.Commands = []cli.Command{
serverCmd,
2018-09-26 12:44:13 +00:00
userCmd,
statsCmd,
2018-05-14 12:11:52 +00:00
}
if len(os.Args) < 2 || os.Args[1] != "--version" {
log.Printf("%s %s", app.Name, app.Version)
}
2018-05-14 12:11:52 +00:00
err := app.Run(os.Args)
if err != nil {
return err
2018-05-14 12:11:52 +00:00
}
return nil
2018-05-14 12:11:52 +00:00
}
func before(c *cli.Context) error {
configFile := c.String("config")
2018-07-16 04:14:30 +00:00
config.LoadEnv(configFile)
app.config = config.Parse()
app.database = datastore.New(app.config.Database)
return nil
}
func after(c *cli.Context) error {
2018-07-15 07:20:07 +00:00
err := app.database.Close()
return err
}