Skip to content

Commit b2db31f

Browse files
committed
editor: print a warning when override.yaml exists
Fix issue 612 Signed-off-by: Akihiro Suda <[email protected]>
1 parent 71918d1 commit b2db31f

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

cmd/limactl/edit.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func editAction(cmd *cobra.Command, args []string) error {
5555
hdr := fmt.Sprintf("# Please edit the following configuration for Lima instance %q\n", instName)
5656
hdr += "# and an empty file will abort the edit.\n"
5757
hdr += "\n"
58+
hdr += generateEditorWarningHeader()
5859
yBytes, err := openEditor(cmd, instName, yContent, hdr)
5960
if err != nil {
6061
return err

cmd/limactl/start.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os/exec"
1111
"path"
1212
"path/filepath"
13+
"regexp"
1314
"strings"
1415

1516
"github.com/AlecAivazis/survey/v2"
@@ -19,6 +20,7 @@ import (
1920
"github.com/lima-vm/lima/pkg/osutil"
2021
"github.com/lima-vm/lima/pkg/start"
2122
"github.com/lima-vm/lima/pkg/store"
23+
"github.com/lima-vm/lima/pkg/store/dirnames"
2224
"github.com/lima-vm/lima/pkg/store/filenames"
2325
"github.com/mattn/go-isatty"
2426
"github.com/norouter/norouter/cmd/norouter/editorcmd"
@@ -147,6 +149,7 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
147149
}
148150
hdr += "# - To cancel starting Lima, just save this file as an empty file.\n"
149151
hdr += "\n"
152+
hdr += generateEditorWarningHeader()
150153
yBytes, err = openEditor(cmd, instName, yBytes, hdr)
151154
if err != nil {
152155
return nil, err
@@ -211,6 +214,40 @@ func askWhetherToOpenEditor(name string) (bool, error) {
211214
}
212215
}
213216

217+
func generateEditorWarningHeader() string {
218+
var s string
219+
configDir, err := dirnames.LimaConfigDir()
220+
if err != nil {
221+
s += "# WARNING: failed to load the config dir\n"
222+
s += "\n"
223+
return s
224+
}
225+
226+
re := regexp.MustCompile(`(?m)^`)
227+
repl := []byte("# ")
228+
229+
defaultPath := filepath.Join(configDir, filenames.Default)
230+
if b, err := os.ReadFile(defaultPath); err == nil {
231+
s += "# WARNING: " + defaultPath + "includes the following settings,\n"
232+
s += "# which is applied before applying this YAML:\n"
233+
s += "# -----------\n"
234+
s += string(re.ReplaceAll(b, repl)) + "\n"
235+
s += "# -----------\n"
236+
s += "\n"
237+
}
238+
239+
overridePath := filepath.Join(configDir, filenames.Override)
240+
if b, err := os.ReadFile(overridePath); err == nil {
241+
s += "# WARNING: " + overridePath + "includes the following settings,\n"
242+
s += "# which will take precedence over anything configured in this YAML:\n"
243+
s += "# -----------\n"
244+
s += string(re.ReplaceAll(b, repl)) + "\n"
245+
s += "# -----------\n"
246+
s += "\n"
247+
}
248+
return s
249+
}
250+
214251
// openEditor opens an editor, and returns the content (not path) of the modified yaml.
215252
//
216253
// openEditor returns nil when the file was saved as an empty file, optionally with whitespaces.

0 commit comments

Comments
 (0)