-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
181 lines (160 loc) · 4.23 KB
/
main.go
File metadata and controls
181 lines (160 loc) · 4.23 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"database/sql"
"errors"
"html/template"
"net"
"net/url"
"os"
"strings"
"time"
dat "gopkg.in/mgutz/dat.v1"
runner "gopkg.in/mgutz/dat.v1/sqlx-runner"
"github.com/jaybeecave/render"
dotenv "github.com/joho/godotenv"
"github.com/urfave/cli"
)
func main() {
dotenv.Load() // load from .env file where scaffold is run
render := getRenderer()
db := getDBConnection()
app := cli.NewApp()
app.Name = "scaffold"
app.Usage = "generate models & migrations using dat"
app.Commands = []cli.Command{
{
Name: "table",
Aliases: []string{"t"},
Usage: "Create a new table [tablename] [fieldname:fieldtype]",
Action: func(c *cli.Context) error {
return createTable(c, render, db)
},
},
{
Name: "fields",
Aliases: []string{"f"},
Usage: "Add fields to an existing table [tablename] [fieldname:fieldtype]",
Action: func(c *cli.Context) error {
return addFields(c, render, db)
},
},
{
Name: "model",
Aliases: []string{"m"},
Usage: "create a model from a table [tablename]",
Action: func(c *cli.Context) error {
return createModel(c, render, db)
},
},
{
Name: "rest",
Aliases: []string{"r"},
Usage: "create a restful interface from a table [tablename]",
Action: func(c *cli.Context) error {
return createRest(c, render, db)
},
},
{
Name: "migration",
Aliases: []string{"mi"},
Usage: "perform schema migration",
Action: func(c *cli.Context) error {
return doMigration(c, render, db)
},
},
}
app.Run(os.Args)
}
func getRenderer() *render.Render {
r := render.New(render.Options{
Directory: "./models/templates",
})
return r
}
func getDBConnection() *runner.DB {
//get url from ENV in the following format postgres://user:pass@192.168.8.8:5432/spaceio")
dbURL := os.Getenv("DATABASE_URL")
u, err := url.Parse(dbURL)
if err != nil {
panic(err)
}
username := u.User.Username()
pass, isPassSet := u.User.Password()
if !isPassSet {
panic("no database password")
}
host, port, _ := net.SplitHostPort(u.Host)
dbName := strings.Replace(u.Path, "/", "", 1)
db, _ := sql.Open("postgres", "dbname="+dbName+" user="+username+" password="+pass+" host="+host+" port="+port+" sslmode=disable")
err = db.Ping()
if err != nil {
panic(err)
}
// ensures the database can be pinged with an exponential backoff (15 min)
runner.MustPing(db)
// set to reasonable values for production
db.SetMaxIdleConns(4)
db.SetMaxOpenConns(16)
// set this to enable interpolation
dat.EnableInterpolation = true
// set to check things like sessions closing.
// Should be disabled in production/release builds.
dat.Strict = false
// Log any query over 10ms as warnings. (optional)
runner.LogQueriesThreshold = 10 * time.Millisecond
// db connection
return runner.NewDB(db, "postgres")
}
// for storing variables when running the templates
type viewBucket struct {
Data map[string]interface{}
}
func newViewBucket() *viewBucket {
return &viewBucket{Data: map[string]interface{}{
"LTEqStr": template.HTML(`<=`),
"GTEqStr": template.HTML(`>=`),
"LTStr": template.HTML(`<`),
"GTStr": template.HTML(`>`),
}}
}
func (viewBucket *viewBucket) add(key string, value interface{}) {
viewBucket.Data[key] = value
}
func (viewBucket *viewBucket) getStrSafe(key string) (string, error) {
val := viewBucket.Data[key]
if val == nil {
return "", errors.New("could not find " + key)
}
strVal, ok := val.(string)
if !ok {
return "", errors.New("could not cast " + key + " to string")
}
return strVal, nil
}
// getStr - returns a string for the provided key. Will panic if key not found
func (viewBucket *viewBucket) getStr(key string) string {
val, err := viewBucket.getStrSafe(key)
if err != nil {
panic(err)
}
return val
}
func (viewBucket *viewBucket) addFieldDataFromContext(c *cli.Context) {
args := c.Args()
viewBucket.add("TableName", args.First())
fields := Fields{}
for _, arg := range args {
if args.First() == arg {
continue // we dont care about the first arg as its the TableName
}
if strings.Contains(arg, ":") {
strSlice := strings.Split(arg, ":")
field := Field{
FieldName: strSlice[0],
FieldType: strSlice[1],
}
fields = append(fields, field)
}
}
viewBucket.add("Fields", fields)
}