/
conf.go
112 lines (96 loc) · 2.54 KB
/
conf.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
package conf
import (
"io/ioutil"
"os"
"path/filepath"
flag "github.com/juju/gnuflag"
"gopkg.in/yaml.v2"
)
var (
// F flag prints full request
F = flag.Bool("f", false, "Print full results, ie. no more '...'")
// L flag limit results to a number
L = flag.Int("l", 5, "Result list limit. Defaults to 5")
// H flag specifies the host to connect to
S = flag.String("s", "ok-b.org", "Server to connect to")
H = flag.Bool("h", false, "Display help")
Help = flag.Bool("help", false, "Display help, same as -h")
// P flag enables private search
P = flag.Bool("p", false, "Private search. Your search won't leave a trace. Pinky promise. Don't use this all the time if you want to see the search result relevancy improved")
// D flag enables debug mode
D = flag.Bool("d", false, "Debug mode")
// DontPipe
DontPipe = flag.Bool("dontpipe", false, "Flag for internal use - ignore this")
// Version flag displays current version
Version = flag.Bool("version", false, "Print version number")
// V flag displays current version
V = flag.Bool("v", false, "Print version number")
)
var (
// EditFile borg edit file.
EditFile string
// ConfigFile borg config file.
ConfigFile string
// QueryFile borg query file.
QueryFile string
)
func init() {
borgDir := borgDir()
EditFile = filepath.Join(borgDir, "edit")
ConfigFile = filepath.Join(borgDir, "config.yml")
QueryFile = filepath.Join(borgDir, "query")
os.Mkdir(borgDir, os.ModePerm)
os.Create(EditFile)
if _, err := os.Stat(ConfigFile); os.IsNotExist(err) {
os.Create(ConfigFile)
}
if _, err := os.Stat(QueryFile); os.IsNotExist(err) {
os.Create(QueryFile)
}
}
func borgDir() string {
home := os.Getenv("HOME")
if len(home) == 0 {
panic("$HOME environment variable is not set")
}
dir := filepath.Join(home, ".borg")
if _, err := os.Stat(dir); os.IsNotExist(err) {
if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
dir = filepath.Join(xdgConfigHome, "borg")
} else {
dir = filepath.Join(home, ".config")
}
}
return dir
}
// Config file
type Config struct {
Token string
DefaultTags []string
Editor string
PipeTo string
}
// Save config
func (c Config) Save() error {
bs, err := yaml.Marshal(c)
if err != nil {
return err
}
return ioutil.WriteFile(ConfigFile, bs, os.ModePerm)
}
// Get config
func Get() (Config, error) {
bs, err := ioutil.ReadFile(ConfigFile)
if err != nil {
panic(err)
}
c := &Config{}
err = yaml.Unmarshal(bs, c)
if err != nil {
return *c, err
}
if len(c.Editor) == 0 {
c.Editor = "vim"
}
return *c, nil
}