This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 396
/
config.go
63 lines (55 loc) · 1.49 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
package main
import (
"fmt"
"io"
"os"
"github.com/Azure/draft/pkg/draft/draftpath"
"github.com/BurntSushi/toml"
"github.com/spf13/cobra"
)
const (
configHelp = `Manage global Draft configuration stored in $DRAFT_HOME/config.toml.`
)
// DraftConfig is the configuration stored in $DRAFT_HOME/config.toml
type DraftConfig map[string]string
// ReadConfig reads in global configuration from $DRAFT_HOME/config.toml
func ReadConfig() (DraftConfig, error) {
var data DraftConfig
h := draftpath.Home(draftHome)
f, err := os.Open(h.Config())
if err != nil {
if os.IsNotExist(err) {
return make(map[string]string), nil
}
return nil, fmt.Errorf("Could not open file %s: %s", h.Config(), err)
}
defer f.Close()
if _, err := toml.DecodeReader(f, &data); err != nil {
return nil, fmt.Errorf("Could not decode config %s: %s", h.Config(), err)
}
return data, nil
}
// SaveConfig saves global configuration to $DRAFT_HOME/config.toml
func SaveConfig(data DraftConfig) error {
h := draftpath.Home(draftHome)
f, err := os.OpenFile(h.Config(), os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return fmt.Errorf("Could not open file %s: %s", h.Config(), err)
}
defer f.Close()
return toml.NewEncoder(f).Encode(data)
}
func newConfigCmd(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "manage Draft configuration",
Long: configHelp,
}
cmd.AddCommand(
newConfigListCmd(out),
newConfigGetCmd(out),
newConfigSetCmd(out),
newConfigUnsetCmd(out),
)
return cmd
}