-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
57 lines (48 loc) · 1.19 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
package main
import (
_ "github.com/denisenkom/go-mssqldb"
"github.com/spf13/cobra"
)
var cmd = &cobra.Command{
Use: "testdb <server> <database>",
Short: "Simple tool to check SQL database connectivity",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
username, err := cmd.Flags().GetString("username")
cobra.CheckErr(err)
password, err := cmd.Flags().GetString("password")
cobra.CheckErr(err)
dbOptions := DBOptions{
Server: args[0],
Database: args[1],
Username: coalesce(username, DefaultUsername),
Password: coalesce(password, DefaultPassword),
}
db, err := dbOptions.connect()
cobra.CheckErr(err)
defer db.Close()
tests := []Test{
{"SELECT 1", `SELECT 1;`},
}
namelen := testNameMaxLen(tests)
for _, test := range tests {
result, err := RunTest(db, test)
printResult(test, result, err, namelen)
}
},
}
func testNameMaxLen(tests []Test) int {
var max int
for _, t := range tests {
length := len(t.Name)
if length > max {
max = length
}
}
return max
}
func main() {
cmd.Flags().StringP("username", "U", "", "Username")
cmd.Flags().StringP("password", "P", "", "Password")
cobra.CheckErr(cmd.Execute())
}