forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars.go
147 lines (126 loc) · 3.53 KB
/
vars.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
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells 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.
*
* Pydio Cells 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 Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
// Package configs provides tools for managing configurations
package config
import (
"fmt"
"os"
"path/filepath"
"sync"
"github.com/micro/go-config/reader"
"github.com/pydio/go-os/config"
"github.com/pydio/go-os/config/source/file"
"encoding/json"
"time"
"github.com/pydio/cells/common/config/envvar"
file2 "github.com/pydio/cells/common/config/file"
)
var (
PydioConfigDir = ApplicationDataDir()
PydioConfigFile = "pydio.json"
VersionsStore file2.VersionsStore
defaultConfig *Config
once sync.Once
)
// Config wrapper around micro Config
type Config struct {
config.Config
}
func init() {
var e error
VersionsStore, e = file2.NewStore(PydioConfigDir)
if e != nil {
//log.Println("Could not open versions store", e)
}
written, err := file2.WriteIfNotExists(filepath.Join(PydioConfigDir, PydioConfigFile), SampleConfig)
if err != nil {
fmt.Println("Error while trying to create default config file")
os.Exit(1)
}
if written && VersionsStore != nil {
var data interface{}
if e := json.Unmarshal([]byte(SampleConfig), data); e == nil {
VersionsStore.Put(&file2.Version{
User: "cli",
Date: time.Now(),
Log: "Initialize with sample config",
Data: data,
})
}
}
}
// Default Config with initialisation
func Default() *Config {
once.Do(func() {
defaultConfig = &Config{config.NewConfig(
// config.WithSource(newEnvSource()),
config.WithSource(newLocalSource()),
)}
})
return defaultConfig
}
func newEnvSource() config.Source {
return envvar.NewSource(
config.SourceName("PYDIO"),
)
}
func newLocalSource() config.Source {
return file.NewSource(
config.SourceName(filepath.Join(PydioConfigDir, PydioConfigFile)),
)
}
func Get(path ...string) reader.Value {
return Default().Get(path...)
}
func Set(val interface{}, path ...string) {
Default().Set(val, path...)
}
func Del(path ...string) {
Default().Del(path...)
}
// func newConfig() *Config {
// dir := ApplicationDataDir()
//
// copySampleConfig(filepath.Join(dir, pydioConfigFile))
//
// // Setting the sources
// // 1 . From the sample file
// // 2 . From the local save
// src1 := file.NewSource(config.SourceName(filepath.Join(dir, pydioConfigFile)))
//
// // Create a default config handle with the 3 sources
// return &Config{
// Config: config.NewConfig(
// config.WithSource(src2),
// config.PollInterval(5*time.Second),
// )}
// }
func (c *Config) Unmarshal(val interface{}) error {
return c.Config.Get().Scan(&val)
}
func (c *Config) UnmarshalKey(key string, val interface{}) error {
return c.Config.Get(key).Scan(&val)
}
func watchConfig() {
}
// GetJsonPath build path for json that contain the local config
func GetJsonPath() string {
return filepath.Join(PydioConfigDir, PydioConfigFile)
}