/
repository.go
144 lines (129 loc) · 2.78 KB
/
repository.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
package repository
import (
"fmt"
"os"
"os/user"
"path"
"path/filepath"
"runtime"
"github.com/arteev/logger"
"io"
"io/ioutil"
"net/http"
"net/url"
)
//RepositoryFile - current file of repository
var (
repositoryFile = "repository.sqlite"
isDefault = true
mustRemove = false
tmpFile string
)
//DSQL environment variables
const (
ENDSQLREPO = "DSQL_REPO"
)
var (
EnvDSQLRepo string
)
func Init() {
search()
}
func Done() {
if mustRemove {
os.Remove(tmpFile)
}
}
func search() error {
//env
if env, ok := os.LookupEnv(ENDSQLREPO); ok {
env := os.ExpandEnv(env)
if err := SetRepositoryFile(env); err != nil {
return err
}
EnvDSQLRepo = env
return nil
}
//workdir
if _, err := os.Stat(repositoryFile); err == nil {
return nil
}
var cfgLocation string
//appdata | ~/.config
if u, err := user.Current(); err == nil {
if runtime.GOOS == "windows" {
cfgLocation = filepath.Join(os.Getenv("APPDATA"), "dsql", repositoryFile)
} else {
cfgLocation = filepath.Join(u.HomeDir, ".config", "dsql", repositoryFile)
}
if _, err := os.Stat(cfgLocation); err == nil {
repositoryFile = cfgLocation
return nil
}
}
//folder dsql
absPath, _ := filepath.Abs(path.Dir(os.Args[0]))
inAppLocation := filepath.Join(absPath, repositoryFile)
if _, err := os.Stat(inAppLocation); err == nil {
repositoryFile = inAppLocation
return nil
}
if cfgLocation != "" {
repositoryFile = cfgLocation
}
return nil
}
//SetRepositoryFile - set new location repository file
func SetRepositoryFile(filename string) error {
if filename == "" {
return nil
}
if !isDefault {
return fmt.Errorf("can't twice change repository file ")
}
if filename != "" {
isDefault = false
repositoryFile = filename
}
return nil
}
//GetRepositoryFile - get current location repository file
func GetRepositoryFile() string {
return repositoryFile
}
//IsDefault returns location repository file is default
func IsDefault() bool {
return isDefault
}
//PrepareLocation - make directories for repository files
func PrepareLocation() error {
if url, err := url.Parse(repositoryFile); err == nil && url.Scheme != "" {
tmp, err := ioutil.TempFile("", "rep.sqlite3")
if err != nil {
return err
}
tmpFile = tmp.Name()
mustRemove = true
defer tmp.Close()
resp, err := http.Get(repositoryFile)
if err != nil {
return err
}
defer resp.Body.Close()
if _, err := io.Copy(tmp, resp.Body); err != nil {
return err
}
repositoryFile = "file:///" + tmp.Name() + "?" + url.RawQuery
logger.Info.Println("Repository temp:", repositoryFile, "on disk:", tmpFile)
return nil
}
dir := filepath.Dir(repositoryFile)
if dir == "" || dir == "." {
return nil
}
perm := 0700
if err := os.MkdirAll(dir, os.FileMode(perm)); err != nil {
return err
}
return nil
}