-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
98 lines (87 loc) · 2.35 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
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
// Package config saves and fetches settings used by the df2 tool.
package config
import (
"errors"
"fmt"
"io/fs"
"log"
"os"
"path"
"path/filepath"
"github.com/Defacto2/df2/pkg/logs"
"github.com/gookit/color"
gap "github.com/muesli/go-app-paths"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)
var ErrSaveType = errors.New("unsupported value interface type")
// directories are initialised and configured by InitDefaults() in lib/cmd.go.
const (
cmdRun = "df2 config"
filename = "config.yaml"
dir fs.FileMode = 0o700
file fs.FileMode = 0o600
)
// Settings for the configuration.
type Settings struct {
Name string // config filename
Errors bool // flag a config file error, used by root.go initConfig()
ignore bool // ignore config file error
nameFlag string // viper configuration path
}
// Config settings.
var Config = Settings{ //nolint:gochecknoglobals
Name: filename,
Errors: false,
ignore: false,
}
// Check prints a notice for the missing configuration file.
func Check() {
if Config.ignore {
return
}
if Config.Errors {
log.Printf("%s %s\n",
color.Warn.Sprint("config: no config file in use, please run"),
color.Bold.Sprintf(cmdRun+" create"))
}
}
// Filepath is the absolute path and filename of the configuration file.
func Filepath() string {
dir, err := gap.NewScope(gap.User, logs.GapUser).ConfigPath(filename)
if err != nil {
h, err := os.UserHomeDir()
if err != nil {
log.Print(fmt.Errorf("filepath userhomedir: %w", err))
}
return filepath.Join(h, filename)
}
return dir
}
func missing(suffix string) {
color.Warn.Println("no config file is in use")
logs.Printf("to create:\t%s %s\n", cmdRun, suffix)
}
// write saves all configs to a configuration file.
func write(update bool) error {
bs, err := yaml.Marshal(viper.AllSettings())
if err != nil {
return fmt.Errorf("write config yaml marshal: %w", err)
}
root := path.Dir(Filepath())
if _, err = os.Stat(root); os.IsNotExist(err) {
if err1 := os.MkdirAll(root, file); err1 != nil {
return fmt.Errorf("create parent dir for config file %s: %w", root, err)
}
}
err = os.WriteFile(Filepath(), bs, file) // owner+wr
if err != nil {
return fmt.Errorf("write config file %s: %w", Filepath(), err)
}
s := "created a new"
if update {
s = "updated the"
}
logs.Println(s+" config file", Filepath())
return nil
}