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