-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.go
More file actions
129 lines (108 loc) · 2.81 KB
/
main.go
File metadata and controls
129 lines (108 loc) · 2.81 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
package main
import (
"flag"
"fmt"
"os"
"path"
"time"
"github.com/kenanbek/dbui/internal/config"
"github.com/kenanbek/dbui/internal/controller"
"github.com/kenanbek/dbui/internal/tui"
)
var (
version, date string
)
func main() {
var (
fConfFile string
fDemo bool
fConnDSN string
fConnType string
appConfig *config.AppConfig
)
fmt.Printf("Starting DBUI v%s (%s) \n", version, date)
flag.StringVar(&fConfFile, "f", "", "custom configuration file")
flag.BoolVar(&fDemo, "demo", false, "run with demo/dummy data source")
flag.StringVar(&fConnDSN, "dsn", "", "data source name")
flag.StringVar(&fConnType, "type", "", "data source type, used together with -dsn")
flag.Parse()
if fDemo {
appConfig = &config.AppConfig{
DataSourcesProp: []config.DataSourceConfig{
{AliasProp: "demo1", TypeProp: "dummy", DSNProp: "dummy"},
{AliasProp: "demo2", TypeProp: "dummy", DSNProp: "dummy"},
},
DefaultProp: "demo1",
}
startApp(appConfig)
return
}
if fConnDSN != "" {
if fConnType == "" {
fmt.Println("-dsn and -type flags must be used together")
fmt.Println("switching to configuration file mode, trying to load...")
time.Sleep(2 * time.Second)
} else {
appConfig = &config.AppConfig{
DataSourcesProp: []config.DataSourceConfig{},
DefaultProp: "custom",
}
appConfig.DataSourcesProp = append(
appConfig.DataSourcesProp,
config.DataSourceConfig{AliasProp: "custom", TypeProp: fConnType, DSNProp: fConnDSN},
)
startApp(appConfig)
return
}
}
appConfig = readConfig(fConfFile)
startApp(appConfig)
}
func startApp(appConfig *config.AppConfig) {
var exitStatus = 1
ctrl, err := controller.New(appConfig)
if err != nil {
fmt.Println(err)
os.Exit(exitStatus)
}
t := tui.NewTUI(appConfig, ctrl)
err = t.Start()
if err != nil {
fmt.Printf("failed to start: %v\n", err)
os.Exit(exitStatus)
}
}
func readConfig(customConfigFile string) *config.AppConfig {
var exitStatus = 0
var err error
var confPath string
if customConfigFile != "" {
if _, err = os.Stat(customConfigFile); err != nil {
fmt.Printf("configuration file `%s` does not exists\n", customConfigFile)
os.Exit(1)
}
confPath = customConfigFile
} else {
confPath = "dbui.yml"
if _, err = os.Stat(confPath); err != nil {
var userDir string
userDir, err = os.UserHomeDir()
if err != nil {
fmt.Println(err)
os.Exit(exitStatus)
}
confPath = path.Join(userDir, "dbui.yml")
if _, err = os.Stat(confPath); err != nil {
fmt.Printf("there is no `dbui.yml` file in the current (%s) nor user directory (%s)\n", "./dbui.yml", confPath)
fmt.Println("create one or use `-dsn` and `-type` args")
os.Exit(exitStatus)
}
}
}
appConfig, err := config.New(confPath)
if err != nil {
fmt.Println(err)
os.Exit(exitStatus)
}
return appConfig
}