fathom/pkg/cli/cli.go

74 lines
1.5 KiB
Go
Raw Normal View History

package cli
2018-05-14 14:11:52 +02:00
import (
"fmt"
2018-05-14 14:11:52 +02:00
"os"
"strings"
2018-11-14 09:31:47 +01:00
"time"
2018-05-14 14:11:52 +02:00
log "github.com/sirupsen/logrus"
2018-05-14 14:11:52 +02:00
"github.com/urfave/cli"
"github.com/usefathom/fathom/pkg/config"
2018-05-14 14:11:52 +02: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 14:11:52 +02:00
// Run parses the CLI arguments & run application command
func Run(version string, commit string, buildDate string) error {
// force all times in UTC, regardless of server timezone
2018-11-14 09:31:47 +01:00
time.Local = time.UTC
// setup CLI app
app = &App{cli.NewApp(), nil, nil}
2018-05-14 14:11:52 +02:00
app.Name = "Fathom"
app.Usage = "simple & transparent website analytics"
app.Version = fmt.Sprintf("%v, commit %v, built at %v", strings.TrimPrefix(version, "v"), commit, buildDate)
2018-05-14 14:11:52 +02: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 14:11:52 +02:00
app.Commands = []cli.Command{
serverCmd,
2018-09-26 14:44:13 +02:00
userCmd,
statsCmd,
2018-05-14 14:11:52 +02:00
}
if len(os.Args) < 2 || os.Args[1] != "--version" {
log.Printf("%s version %s", app.Name, app.Version)
}
2018-05-14 14:11:52 +02:00
err := app.Run(os.Args)
if err != nil {
return err
2018-05-14 14:11:52 +02:00
}
return nil
2018-05-14 14:11:52 +02:00
}
func before(c *cli.Context) error {
configFile := c.String("config")
2018-07-16 11:14:30 +07: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 09:20:07 +02:00
err := app.database.Close()
return err
}