This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
database.go
54 lines (51 loc) · 1.84 KB
/
database.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
package common
import (
"fmt"
"reflect"
"strconv"
)
type Database struct {
Driver string `name:"Driver" question:"What's the driver?" allow:"{mysql}"`
Version string `name:"Version" question:"What's the version?" allow:"{5.6, 5.7}"`
Storage int `name:"Storage in GB" question:"What's the storage in GB?"`
OriginHost string `name:"Origin Host" question:"What's the host of the origin database?"`
OriginName string `name:"Origin Name" question:"What's the name of the origin database?"`
OriginUsername string `name:"Origin Username" question:"What's the username of the origin database?"`
OriginPassword string `name:"Origin Password" question:"What's the password of the origin database?"`
/*
EnvVarHost string `name:"The Environment Variable for Host" question:"What's the environment variable for the host?"`
EnvVarDatabase string `name:"The Environment Variable for Database" question:"What's the environment variable for the database name?"`
EnvVarUsername string `name:"The Environment Variable for Username" question:"What's the environment variable for the username?"`
EnvVarPassword string `name:"The Environment Variable for Password" question:"What's the environment variable for the password?"`
*/
}
func (d Database) ValidateField(fieldName string, input string, field *reflect.Value) error {
if field.Kind() == reflect.String {
switch fieldName {
case "Driver":
switch input {
case "mysql":
break
default:
return fmt.Errorf("Not support %s", input)
}
case "Version":
switch input {
case "5.6":
case "5.7":
break
default:
return fmt.Errorf("Not support %s", input)
}
}
field.SetString(input)
}
if field.Kind() == reflect.Int {
inputToInt, err := strconv.Atoi(input)
if err != nil {
return err
}
field.Set(reflect.ValueOf(inputToInt))
}
return nil
}