This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
138 lines (123 loc) · 2.68 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"github.com/stealthrocket/timecraft/internal/timecraft"
"gopkg.in/yaml.v3"
)
const configUsage = `
Usage: timecraft config [options]
Options:
-c, --config path Path to the timecraft configuration file (overrides TIMECRAFTCONFIG)
--edit Open $EDITOR to edit the configuration
-h, --help Show usage information
-o, --output format Output format, one of: text, json, yaml
`
func config(ctx context.Context, args []string) error {
var (
edit bool
output = outputFormat("text")
)
flagSet := newFlagSet("timecraft config", configUsage)
boolVar(flagSet, &edit, "edit")
customVar(flagSet, &output, "o", "output")
if _, err := parseFlags(flagSet, args); err != nil {
return err
}
r, path, err := timecraft.OpenConfig()
if err != nil {
return err
}
defer r.Close()
if edit {
editor := os.Getenv("EDITOR")
if editor == "" {
return errors.New(`$EDITOR is not set`)
}
shell := os.Getenv("SHELL")
if shell == "" {
shell = "/bin/sh"
}
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
if !errors.Is(err, fs.ErrExist) {
return err
}
}
tmp, err := createTempFile(path, r)
if err != nil {
return err
}
defer os.Remove(tmp)
p, err := os.StartProcess(shell, []string{shell, "-c", editor + " " + tmp}, &os.ProcAttr{
Files: []*os.File{
0: os.Stdin,
1: os.Stdout,
2: os.Stderr,
},
})
if err != nil {
return err
}
if _, err := p.Wait(); err != nil {
return err
}
f, err := os.Open(tmp)
if err != nil {
return err
}
defer f.Close()
if _, err := timecraft.ReadConfig(f); err != nil {
return fmt.Errorf("not applying configuration updates because the file has a syntax error: %w", err)
}
if err := os.Rename(tmp, path); err != nil {
return err
}
}
config, err := timecraft.LoadConfig()
if err != nil {
return err
}
w := io.Writer(os.Stdout)
for {
switch output {
case "json":
e := json.NewEncoder(w)
e.SetEscapeHTML(false)
e.SetIndent("", " ")
_ = e.Encode(config)
case "yaml":
e := yaml.NewEncoder(w)
e.SetIndent(2)
_ = e.Encode(config)
_ = e.Close()
default:
r, _, err := timecraft.OpenConfig()
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
output = "yaml"
continue
}
return err
}
defer r.Close()
_, _ = io.Copy(w, r)
}
return nil
}
}
func createTempFile(path string, r io.Reader) (string, error) {
dir, file := filepath.Split(path)
w, err := os.CreateTemp(dir, "."+file+".*")
if err != nil {
return "", err
}
defer w.Close()
_, err = io.Copy(w, r)
return w.Name(), err
}