-
Notifications
You must be signed in to change notification settings - Fork 28
/
edit.go
181 lines (149 loc) · 4.28 KB
/
edit.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package config
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sort"
"github.com/nhost/be/services/mimir/model"
"github.com/nhost/cli/clienv"
"github.com/pelletier/go-toml/v2"
"github.com/urfave/cli/v2"
"github.com/wI2L/jsondiff"
)
const (
flagEditor = "editor"
)
func CommandEdit() *cli.Command {
return &cli.Command{ //nolint:exhaustruct
Name: "edit",
Aliases: []string{},
Usage: "Edit base configuration or an overlay",
Action: edit,
Flags: []cli.Flag{
&cli.StringFlag{ //nolint:exhaustruct
Name: flagSubdomain,
Usage: "If specified, edit this subdomain's overlay, otherwise edit base configuation",
EnvVars: []string{"NHOST_SUBDOMAIN"},
},
&cli.StringFlag{ //nolint:exhaustruct
Name: flagEditor,
Usage: "Editor to use",
Value: "vim",
EnvVars: []string{"EDITOR"},
},
},
}
}
func EditFile(ctx context.Context, editor, filepath string) error {
cmd := exec.CommandContext(
ctx,
editor,
filepath,
)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to open editor: %w", err)
}
return nil
}
func CopyConfig[T any](src, dst, overlayPath string) error {
var cfg *T
if err := clienv.UnmarshalFile(src, &cfg, toml.Unmarshal); err != nil {
return fmt.Errorf("failed to parse config: %w", err)
}
var err error
if clienv.PathExists(overlayPath) {
cfg, err = ApplyJSONPatches(*cfg, overlayPath)
if err != nil {
return fmt.Errorf("failed to apply json patches: %w", err)
}
}
if err := clienv.MarshalFile(cfg, dst, toml.Marshal); err != nil {
return fmt.Errorf("failed to save temporary file: %w", err)
}
return nil
}
func readFile(filepath string) (any, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
var v any
if err := toml.Unmarshal(b, &v); err != nil {
return nil, fmt.Errorf("failed to unmarshal toml: %w", err)
}
return v, nil
}
func GenerateJSONPatch(origfilepath, newfilepath, dst string) error {
origo, err := readFile(origfilepath)
if err != nil {
return fmt.Errorf("failed to convert original toml to json: %w", err)
}
newo, err := readFile(newfilepath)
if err != nil {
return fmt.Errorf("failed to convert new toml to json: %w", err)
}
patches, err := jsondiff.Compare(origo, newo)
if err != nil {
return fmt.Errorf("failed to generate json patch: %w", err)
}
dstf, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644) //nolint:mnd
if err != nil {
return fmt.Errorf("failed to open destination file: %w", err)
}
defer dstf.Close()
sort.Slice(patches, func(i, j int) bool {
return patches[i].Path < patches[j].Path
})
dstb, err := json.MarshalIndent(patches, "", " ")
if err != nil {
return fmt.Errorf("failed to prettify json: %w", err)
}
if _, err := dstf.Write(dstb); err != nil {
return fmt.Errorf("failed to write to destination file: %w", err)
}
return nil
}
func edit(cCtx *cli.Context) error {
ce := clienv.FromCLI(cCtx)
if cCtx.String(flagSubdomain) == "" {
if err := EditFile(cCtx.Context, cCtx.String(flagEditor), ce.Path.NhostToml()); err != nil {
return fmt.Errorf("failed to edit config: %w", err)
}
return nil
}
if err := os.MkdirAll(ce.Path.OverlaysFolder(), 0o755); err != nil { //nolint:mnd
return fmt.Errorf("failed to create json patches directory: %w", err)
}
tmpdir, err := os.MkdirTemp(os.TempDir(), "nhost-jsonpatch")
if err != nil {
return fmt.Errorf("failed to create temporary directory: %w", err)
}
defer os.RemoveAll(tmpdir)
tmpfileName := filepath.Join(tmpdir, "nhost.toml")
if err := CopyConfig[model.ConfigConfig](
ce.Path.NhostToml(), tmpfileName, ce.Path.Overlay(cCtx.String(flagSubdomain)),
); err != nil {
return fmt.Errorf("failed to copy config: %w", err)
}
if err := EditFile(cCtx.Context, cCtx.String(flagEditor), tmpfileName); err != nil {
return fmt.Errorf("failed to edit config: %w", err)
}
if err := GenerateJSONPatch(
ce.Path.NhostToml(), tmpfileName, ce.Path.Overlay(cCtx.String(flagSubdomain)),
); err != nil {
return fmt.Errorf("failed to generate json patch: %w", err)
}
return nil
}