-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (64 loc) · 2.03 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
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
dbshiftcore "github.com/limoli/dbshift-core"
"os"
)
const (
envPrefix = "DBSHIFT_CLI_MYSQL"
envMySqlTable = envPrefix + "_TABLE"
envMySqlUsername = envPrefix + "_USERNAME"
envMySqlPassword = envPrefix + "_PASSWORD"
envMySqlDatabase = envPrefix + "_DATABASE"
envMySqlAddress = envPrefix + "_ADDRESS"
envMySqlOptionIsMultiStatement = envPrefix + "_OPTION_IS_MULTI_STATEMENT"
)
func main() {
cfg, err := initConfiguration()
if err != nil {
dbshiftcore.PrintFailure(err.Error())
if errWithCodeObj, ok := err.(*errorWithCode); ok {
os.Exit(errWithCodeObj.code)
}
}
db, err := newMysqlDatabase(*cfg)
if err != nil {
dbshiftcore.PrintFailure(err.Error())
os.Exit(150)
}
cmd, err := dbshiftcore.NewCmd(db)
if err != nil {
dbshiftcore.PrintFailure(err.Error())
os.Exit(160)
}
cmd.Run()
}
func initConfiguration() (*config, error) {
const errMsg = "missing or unset %s environment variable"
username := os.Getenv(envMySqlUsername)
password := os.Getenv(envMySqlPassword)
address := os.Getenv(envMySqlAddress)
databaseName := os.Getenv(envMySqlDatabase)
if databaseName == "" {
return nil, &errorWithCode{110, fmt.Sprintf(errMsg, envMySqlUsername)}
}
optionIsMultiStatement := os.Getenv(envMySqlOptionIsMultiStatement)
if optionIsMultiStatement != "" && !(optionIsMultiStatement == "true" || optionIsMultiStatement == "false") {
return nil, &errorWithCode{115, fmt.Sprintf("bad boolean value for %s environment variable", envMySqlOptionIsMultiStatement)}
}
tableName := os.Getenv(envMySqlTable)
if tableName == "" {
return nil, &errorWithCode{code: 130, message: fmt.Sprintf("missing or unset %s environment variable", envMySqlTable)}
}
return &config{
username: username,
password: password,
address: address,
dbName: databaseName,
tableName: tableName,
options: configOptions{
isMultiStatement: optionIsMultiStatement,
},
}, nil
}