/
default.go
184 lines (158 loc) · 4.61 KB
/
default.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package config
import (
"database/sql"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
stdSync "sync"
"time"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/admpub/nging/v5/application/library/common"
"github.com/admpub/nging/v5/application/library/setup"
)
const (
LockFileName = `installed.lock`
)
var _ = FixWd()
var (
Installed sql.NullBool
installedSchemaVer float64
installedTime time.Time
defaultConfig *Config
defaultConfigMu stdSync.RWMutex
defaultCLIConfig = NewCLIConfig()
ErrUnknowDatabaseType = errors.New(`unkown database type`)
onceUpgrade stdSync.Once
sqlCollection = NewSQLCollection().RegisterInstall(`nging`, setup.InstallSQL)
)
func FromCLI() *CLIConfig {
return defaultCLIConfig
}
func FromFile() *Config {
defaultConfigMu.RLock()
v := defaultConfig
defaultConfigMu.RUnlock()
return v
}
func FromDB(group ...string) echo.H {
return echo.GetStoreByKeys(common.SettingName, group...)
}
func GetSQLCollection() *SQLCollection {
return sqlCollection
}
func RegisterInstallSQL(project string, installSQL string) {
sqlCollection.RegisterInstall(project, installSQL)
}
func RegisterInsertSQL(project string, insertSQL string) {
sqlCollection.RegisterInsert(project, insertSQL)
}
func RegisterPreupgradeSQL(project string, version, preupgradeSQL string) {
sqlCollection.RegisterPreupgrade(project, version, preupgradeSQL)
}
func GetInsertSQLs() map[string][]string {
return sqlCollection.Insert
}
func GetInstallSQLs() map[string][]string {
return sqlCollection.Install
}
func GetPreupgradeSQLs() map[string]map[string][]string {
return sqlCollection.Preupgrade
}
func SetInstalled(lockFile string) error {
now := time.Now()
err := os.WriteFile(lockFile, []byte(now.Format(`2006-01-02 15:04:05`)+"\n"+fmt.Sprint(Version.DBSchema)), os.ModePerm)
if err != nil {
return err
}
installedTime = now
installedSchemaVer = Version.DBSchema
Installed.Valid = true
Installed.Bool = true
return nil
}
func InstalledLockFile() string {
for _, lockFile := range []string{
filepath.Join(FromCLI().ConfDir(), LockFileName),
filepath.Join(echo.Wd(), LockFileName),
} {
if info, err := os.Stat(lockFile); err == nil && !info.IsDir() {
return lockFile
}
}
return ``
}
func IsInstalled() bool {
if !Installed.Valid {
lockFile := InstalledLockFile()
if len(lockFile) > 0 {
if b, e := os.ReadFile(lockFile); e == nil {
content := string(b)
content = strings.TrimSpace(content)
lines := strings.Split(content, "\n")
switch len(lines) {
case 2:
installedSchemaVer, _ = strconv.ParseFloat(strings.TrimSpace(lines[1]), 64)
fallthrough
case 1:
installedTime, _ = time.Parse(`2006-01-02 15:04:05`, strings.TrimSpace(lines[0]))
}
}
Installed.Valid = true
Installed.Bool = true
}
}
return Installed.Bool
}
func GetSQLInstallFiles() ([]string, error) {
confDIR := FromCLI().Confd
sqlFile := filepath.Join(confDIR, `install.sql`)
var sqlFiles []string
if com.FileExists(sqlFile) {
sqlFiles = append(sqlFiles, sqlFile)
}
matches, err := filepath.Glob(confDIR + echo.FilePathSeparator + `install.*.sql`)
if len(matches) > 0 {
sqlFiles = append(sqlFiles, matches...)
}
return sqlFiles, err
}
func GetPreupgradeSQLFiles() []string {
confDIR := FromCLI().Confd
sqlFiles := []string{}
matches, _ := filepath.Glob(confDIR + echo.FilePathSeparator + `preupgrade.*.sql`)
if len(matches) > 0 {
sqlFiles = append(sqlFiles, matches...)
}
return sqlFiles
}
func GetSQLInsertFiles() []string {
confDIR := FromCLI().Confd
sqlFile := filepath.Join(confDIR, `insert.sql`)
sqlFiles := []string{}
if com.FileExists(sqlFile) {
sqlFiles = append(sqlFiles, sqlFile)
}
matches, _ := filepath.Glob(confDIR + echo.FilePathSeparator + `insert.*.sql`)
if len(matches) > 0 {
sqlFiles = append(sqlFiles, matches...)
}
return sqlFiles
}