-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.go
89 lines (83 loc) · 1.84 KB
/
utils.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
package gismanager
import (
"database/sql"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
//postgres Driver
_ "github.com/lib/pq"
yaml "gopkg.in/yaml.v2"
)
//FromConfig load geoserver config from yaml file
func FromConfig(configFile string) (config *ManagerConfig, err error) {
gpkgConfig := ManagerConfig{}
gpkgConfig.logger = GetLogger()
path, _ := filepath.Abs(configFile)
yamlFile, err := ioutil.ReadFile(path)
if err != nil {
gpkgConfig.logger.Errorf("yamlFile.Get err %v ", err)
return
}
err = yaml.Unmarshal(yamlFile, &gpkgConfig)
if err != nil {
gpkgConfig.logger.Errorf("Unmarshal: %v", err)
return
}
config = &gpkgConfig
return
}
func isSupported(ext string) bool {
for _, a := range supportedEXT {
if a == ext {
return true
}
}
return false
}
//GetGISFiles retrun List of All GIS Files in this path
func GetGISFiles(root string) ([]string, error) {
root, _ = filepath.Abs(root)
var files []string
fileInfo, statErr := os.Stat(root)
if statErr != nil {
return files, statErr
}
if !fileInfo.IsDir() {
files = append(files, path.Join(root, fileInfo.Name()))
return files, nil
}
dirInfo, err := ioutil.ReadDir(root)
if err != nil {
return files, err
}
for _, file := range dirInfo {
if file.IsDir() {
subFiles, subErr := GetGISFiles(path.Join(root, file.Name()))
if subErr == nil {
files = append(files, subFiles...)
}
} else {
extension := strings.ToLower(filepath.Ext(file.Name()))
if isSupported(extension) {
files = append(files, path.Join(root, file.Name()))
}
}
}
return files, nil
}
//DBIsAlive check if database alive
func DBIsAlive(dbType string, connectionStr string) (err error) {
db, dbErr := sql.Open(dbType, connectionStr)
if dbErr != nil {
err = dbErr
return
}
if pingErr := db.Ping(); pingErr != nil {
db.Close()
err = pingErr
return
}
return
}