Skip to content

Commit ea9ef05

Browse files
committed
Cleanup default and override warning in limactl edit
Improvements: * Don't warn about empty files * Don't show an extra empty line at the end of the file * Don't show empty lines with trailing whitespace as "# \n" Signed-off-by: Jan Dubois <[email protected]>
1 parent 71861bc commit ea9ef05

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

cmd/limactl/start.go

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

1615
"github.com/AlecAivazis/survey/v2"
@@ -214,6 +213,26 @@ func askWhetherToOpenEditor(name string) (bool, error) {
214213
}
215214
}
216215

216+
func fileWarning(filename string) string {
217+
b, err := os.ReadFile(filename)
218+
if err != nil || len(b) == 0 {
219+
return ""
220+
}
221+
s := "# WARNING: " + filename + " includes the following settings,\n"
222+
s += "# which are applied before applying this YAML:\n"
223+
s += "# -----------\n"
224+
for _, line := range strings.Split(strings.TrimSuffix(string(b), "\n"), "\n") {
225+
s += "#"
226+
if len(line) > 0 {
227+
s += " " + line
228+
}
229+
s += "\n"
230+
}
231+
s += "# -----------\n"
232+
s += "\n"
233+
return s
234+
}
235+
217236
func generateEditorWarningHeader() string {
218237
var s string
219238
configDir, err := dirnames.LimaConfigDir()
@@ -223,28 +242,8 @@ func generateEditorWarningHeader() string {
223242
return s
224243
}
225244

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-
}
245+
s += fileWarning(filepath.Join(configDir, filenames.Default))
246+
s += fileWarning(filepath.Join(configDir, filenames.Override))
248247
return s
249248
}
250249

0 commit comments

Comments
 (0)