-
Notifications
You must be signed in to change notification settings - Fork 5
/
libconfig.go
55 lines (47 loc) · 1.55 KB
/
libconfig.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
package wallabago
import (
"bytes"
"encoding/json"
"os"
)
// LibConfig containing all data to access wallabag API
var LibConfig WallabagConfig
// WallabagConfig contains all data needed to connect to wallabag API like URL, id and secret of the API client and user name and according password
type WallabagConfig struct {
WallabagURL string
ClientID string
ClientSecret string
UserName string
UserPassword string
}
// NewWallabagConfig initializes a new WallabagConfig
func NewWallabagConfig(wallabagURL, clientID, clientSecret, userName, userPassword string) WallabagConfig {
return WallabagConfig{wallabagURL, clientID, clientSecret, userName, userPassword}
}
// SetConfig sets global wallabago Config
func SetConfig(wbgConfig WallabagConfig) {
LibConfig = wbgConfig
}
// ReadConfig will read the configuration from the given configJSON
// file and set the global Config setting with the results of the
// parsing
func ReadConfig(configJSON string) (err error) {
LibConfig, err = getConfig(configJSON)
return
}
// getConfig reads a given configJSON file and parses the result, returning a parsed config object
func getConfig(configJSON string) (config WallabagConfig, err error) {
raw, err := os.ReadFile(configJSON)
if err != nil {
return
}
config, err = readJSON(raw)
return
}
// readJSON parses a byte stream into a WallabagConfig object
func readJSON(raw []byte) (config WallabagConfig, err error) {
// trim BOM bytes that make the JSON parser crash
raw = bytes.TrimPrefix(raw, []byte("\xef\xbb\xbf"))
err = json.Unmarshal(raw, &config)
return
}