2018-10-30 20:08:48 +01:00
|
|
|
package cli
|
2018-05-14 14:11:52 +02:00
|
|
|
|
|
|
|
import (
|
2018-11-20 09:25:24 +01:00
|
|
|
"fmt"
|
2018-05-14 14:11:52 +02:00
|
|
|
"os"
|
2018-11-20 09:25:24 +01:00
|
|
|
"strings"
|
2018-11-14 09:31:47 +01:00
|
|
|
"time"
|
2018-05-14 14:11:52 +02:00
|
|
|
|
2018-05-23 09:03:42 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-05-14 14:11:52 +02:00
|
|
|
"github.com/urfave/cli"
|
2018-05-15 13:55:10 +02:00
|
|
|
"github.com/usefathom/fathom/pkg/config"
|
2018-05-14 14:11:52 +02:00
|
|
|
"github.com/usefathom/fathom/pkg/datastore"
|
|
|
|
)
|
|
|
|
|
2018-05-15 13:55:10 +02:00
|
|
|
type App struct {
|
|
|
|
*cli.App
|
|
|
|
database datastore.Datastore
|
|
|
|
config *config.Config
|
|
|
|
}
|
|
|
|
|
2018-10-16 09:36:30 +02:00
|
|
|
// CLI application
|
2018-05-15 13:55:10 +02:00
|
|
|
var app *App
|
2018-05-14 14:11:52 +02:00
|
|
|
|
2018-11-20 09:25:24 +01:00
|
|
|
// Run parses the CLI arguments & run application command
|
|
|
|
func Run(version string, commit string, buildDate string) error {
|
2018-05-16 03:54:51 -05:00
|
|
|
// force all times in UTC, regardless of server timezone
|
2018-11-14 09:31:47 +01:00
|
|
|
time.Local = time.UTC
|
2018-05-16 03:54:51 -05:00
|
|
|
|
|
|
|
// setup CLI app
|
2018-05-15 13:55:10 +02:00
|
|
|
app = &App{cli.NewApp(), nil, nil}
|
2018-05-14 14:11:52 +02:00
|
|
|
app.Name = "Fathom"
|
|
|
|
app.Usage = "simple & transparent website analytics"
|
2018-11-20 09:25:24 +01:00
|
|
|
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"
|
2018-05-15 11:07:27 +02:00
|
|
|
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{
|
2018-07-13 11:48:28 +02:00
|
|
|
serverCmd,
|
2018-09-26 14:44:13 +02:00
|
|
|
userCmd,
|
2018-08-01 14:03:44 +02:00
|
|
|
statsCmd,
|
2018-05-14 14:11:52 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 12:36:56 +02:00
|
|
|
if len(os.Args) < 2 || os.Args[1] != "--version" {
|
2018-11-20 09:25:24 +01:00
|
|
|
log.Printf("%s version %s", app.Name, app.Version)
|
2018-06-04 12:36:56 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 14:11:52 +02:00
|
|
|
err := app.Run(os.Args)
|
|
|
|
if err != nil {
|
2018-10-30 20:08:48 +01:00
|
|
|
return err
|
2018-05-14 14:11:52 +02:00
|
|
|
}
|
2018-08-01 14:03:44 +02:00
|
|
|
|
2018-10-30 20:08:48 +01:00
|
|
|
return nil
|
2018-05-14 14:11:52 +02:00
|
|
|
}
|
2018-05-15 11:07:27 +02:00
|
|
|
|
|
|
|
func before(c *cli.Context) error {
|
2018-05-23 09:03:42 +02:00
|
|
|
configFile := c.String("config")
|
2018-07-16 11:14:30 +07:00
|
|
|
config.LoadEnv(configFile)
|
2018-07-15 21:09:44 +07:00
|
|
|
app.config = config.Parse()
|
2018-05-15 13:55:10 +02:00
|
|
|
app.database = datastore.New(app.config.Database)
|
2018-05-15 11:07:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func after(c *cli.Context) error {
|
2018-07-15 09:20:07 +02:00
|
|
|
err := app.database.Close()
|
|
|
|
return err
|
2018-05-15 11:07:27 +02:00
|
|
|
}
|