/
config.go
68 lines (55 loc) · 1.18 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package config
import (
"bytes"
"errors"
"fmt"
"os"
"github.com/BurntSushi/toml"
)
type Config struct {
SilentAdd bool
SilentRemove bool
}
func Load() (*Config, error) {
config := Config{}
configLocation, err := configPath()
if err != nil {
return nil, err
}
_, err = toml.DecodeFile(configLocation, &config)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return &Config{
SilentAdd: false,
SilentRemove: false,
}, nil
}
}
return &config, nil
}
func Write(config *Config) error {
configLocation, err := configPath()
if err != nil {
return err
}
buf := new(bytes.Buffer)
err = toml.NewEncoder(buf).Encode(config)
if err != nil {
return fmt.Errorf("failed to encode the config: %w", err)
}
// Delete the old config
f, err := os.OpenFile(configLocation, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
if err != nil {
return fmt.Errorf("failed to open config: %w", err)
}
defer f.Close()
_, err = f.Write(buf.Bytes())
if err != nil {
return fmt.Errorf("Failed to write config: %w", err)
}
return nil
}
func Print(config *Config) {
fmt.Printf("silentAdd: %v\n", config.SilentAdd)
fmt.Printf("silentRemove: %v\n", config.SilentRemove)
}