-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
51 lines (43 loc) · 1018 Bytes
/
config.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
package qiisync
import (
"io"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
)
// Config stores Qiita's configuration and local environment settings.
type Config struct {
Qiita qiitaConfig `toml:"qiita"`
Local localConfig `toml:"local"`
}
type qiitaConfig struct {
Token string `toml:"api_token"`
}
type localConfig struct {
Dir string `toml:"base_dir"`
FileNameMode string `toml:"filename_mode"`
}
// LoadConfiguration gets its configuration from "~/.config/qiisync/config".
func LoadConfiguration() (*Config, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
filename := filepath.Join(home, ".config", "qiisync", "config")
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return loadConfig(f)
}
func loadConfig(r io.Reader) (*Config, error) {
var config Config
if _, err := toml.DecodeReader(r, &config); err != nil {
return nil, err
}
return &config, nil
}
func (c *Config) baseDir() string {
return c.Local.Dir
}