forked from oelmekki/pgrebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
80 lines (66 loc) · 1.66 KB
/
config.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
package main
import (
"fmt"
"flag"
"os"
)
type Config struct {
WatchMode bool
DatabaseUrl string
SqlDirPath string
FunctionFiles []string
TriggerFiles []string
TypeFiles []string
ViewFiles []string
}
/*
* Retrieve configuration from command line options
*/
func ( config *Config ) Parse() ( err error ) {
if err = config.parseDatabaseUrl() ; err != nil { return err }
if err = config.parseFlags() ; err != nil { return err }
if err = config.parseSqlDir() ; err != nil { return err }
return
}
/*
* Scan sql directory for sql files
*/
func ( config *Config ) ScanFiles() {
config.FunctionFiles = make( []string, 0 )
config.TriggerFiles = make( []string, 0 )
config.TypeFiles = make( []string, 0 )
config.ViewFiles = make( []string, 0 )
sourceWalker := SourceWalker{ Config: config }
sourceWalker.Process()
}
/*
* Parse options
*/
func ( config *Config ) parseFlags() ( err error ) {
flag.BoolVar( &config.WatchMode, "w", false, "Keep watching for filesystem change" )
flag.Parse()
return
}
/*
* Retrieve database connection info
*/
func ( config *Config ) parseDatabaseUrl() ( err error ) {
config.DatabaseUrl = os.Getenv( "DATABASE_URL" )
if len( config.DatabaseUrl ) == 0 {
return fmt.Errorf( "You must provide database connection information as DATABASE_URL" )
}
return
}
/*
* Retrieve sql source directory
*/
func ( config *Config ) parseSqlDir() ( err error ) {
if len( os.Args ) == 1 {
return fmt.Errorf( "You must provide a sql directory" )
}
config.SqlDirPath = os.Args[ len( os.Args ) - 1 ]
if string( config.SqlDirPath[ len( config.SqlDirPath ) - 1 ] ) != "/" {
config.SqlDirPath += "/"
}
return
}