-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
101 lines (87 loc) · 2.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/claudetech/loggo"
"github.com/codegangsta/cli"
_ "github.com/joho/godotenv/autoload"
)
func showErrorAndAbort(mesasge string) {
fmt.Fprintf(os.Stderr, "An error has occured: %s, aborting.\n", mesasge)
os.Exit(1)
}
func GetFixturesDirectories(fixturesPath string, env string) []string {
fixturesPaths := []string{fixturesPath}
if env != "" {
envDir := filepath.Join(fixturesPath, env)
if _, err := os.Stat(envDir); err == nil {
fixturesPaths = append(fixturesPaths, envDir)
}
}
return fixturesPaths
}
func RunApp(c *cli.Context) {
dbUrl := c.String("db-url")
if dbUrl == "" {
showErrorAndAbort("you need to provide 'db-url' or $DATABASE_URL needs to be set")
}
if c.Bool("debug") {
logger.SetLevel(loggo.Debug)
}
if c.Bool("quiet") {
logger.SetLevel(loggo.Warning)
}
fixturesPaths := GetFixturesDirectories(c.String("fixtures-path"), c.String("env"))
logger.Debugf("searching for fixtures in: %v", fixturesPaths)
fixtures, err := LoadDirectories(fixturesPaths)
if err != nil {
showErrorAndAbort(err.Error())
}
logger.Debugf("found the following fixtures: %+v", fixtures)
logger.Infof("connecting to DB %s", dbUrl)
populator, err := NewPopulator(dbUrl)
if err != nil {
showErrorAndAbort(err.Error())
}
if err := populator.PopulateData(fixtures); err != nil {
showErrorAndAbort(err.Error())
}
}
func main() {
app := cli.NewApp()
app.Name = "dbpopulate"
app.Usage = "populates SQL database from JSON or YAML files"
app.Author = "Daniel Perez <[email protected]>"
app.Version = "0.1.0"
app.Action = RunApp
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "fixtures-path, p",
Value: "./fixtures",
Usage: "Set the directory containing the fixtures",
EnvVar: "FIXTURES_PATH",
},
cli.StringFlag{
Name: "db-url, u",
Usage: "Set the database URL",
EnvVar: "DATABASE_URL",
},
cli.StringFlag{
Name: "env, e",
Usage: "Set the environment",
EnvVar: "GO_ENV",
},
cli.BoolFlag{
Name: "debug, d",
Usage: "Set debug mode on",
EnvVar: "DEBUG",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "Disable info log",
EnvVar: "QUIET",
},
}
app.Run(os.Args)
}