From 975340b5f7c18719a546bf16e7ce6c65bb22f7b2 Mon Sep 17 00:00:00 2001 From: Bjoern Kalkbrenner Date: Sat, 28 Nov 2026 12:33:03 +0100 Subject: [PATCH] fix(configcmd): config create --output - prints the template to stdout It wrote a file named "-" in the working directory instead. Co-Authored-By: Claude Opus 5.5 --- configcmd/configcmd.go | 10 +++++++--- configcmd/configcmd_test.go | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/configcmd/configcmd.go b/configcmd/configcmd.go index eb06fd1..0f03a86 100644 --- a/configcmd/configcmd.go +++ b/configcmd/configcmd.go @@ -2,7 +2,7 @@ // Yetano program standard (docs/program-conventions.md and ADR-0012 in // the yetano meta-repository): // -// config create [--minimal|--full] [--force] [--output ] +// config create [--minimal|--full] [--force] [--output |-] // config validate // config upgrade [--write] // config path @@ -201,7 +201,7 @@ func usage(s Spec, w io.Writer) { } fmt.Fprintf(w, `usage: %[1]s config [--config ] - create [--minimal|--full] [--force] [--output ] + create [--minimal|--full] [--force] [--output |-] write a commented starter configuration (minimal by default) validate check the configuration file the program would use upgrade [--write] @@ -342,7 +342,7 @@ func (s Spec) create(explicit string, args []string, stdout, stderr io.Writer) i minimal := fs.Bool("minimal", false, "write the minimal starter configuration (default)") full := fs.Bool("full", false, "write the fully commented reference configuration") force := fs.Bool("force", false, "overwrite the file if it already exists") - output := fs.String("output", "", "file to write (default: --config, else the file the program would use, else "+s.filename()+" in the user config directory -- the system one as root)") + output := fs.String("output", "", "file to write, - for stdout (default: --config, else the file the program would use, else "+s.filename()+" in the user config directory -- the system one as root)") if code := parse(fs, args); code >= 0 { return code } @@ -356,6 +356,10 @@ func (s Spec) create(explicit string, args []string, stdout, stderr io.Writer) i } path := *output + if path == "-" { // the template on stdout, nothing written + io.WriteString(stdout, content) + return ExitOK + } if path == "" { var err error if path, err = s.defaultCreatePath(explicit); err != nil { diff --git a/configcmd/configcmd_test.go b/configcmd/configcmd_test.go index 7e869ca..adda083 100644 --- a/configcmd/configcmd_test.go +++ b/configcmd/configcmd_test.go @@ -190,6 +190,27 @@ func TestCreateExplicitAndOutput(t *testing.T) { } } +// --output - prints the template and writes no file (not one named "-"). +func TestCreateOutputStdout(t *testing.T) { + root := t.TempDir() + s := testSpec(root) + work := filepath.Join(root, "work") + if err := os.MkdirAll(work, 0o755); err != nil { + t.Fatal(err) + } + t.Chdir(work) + code, out, errOut := run(t, s, "", "create", "--full", "--output", "-") + if code != ExitOK || out != fullTemplate || errOut != "" { + t.Fatalf("create --output -: exit %d, stdout %q, stderr %q", code, out, errOut) + } + if _, err := os.Stat(filepath.Join(work, "-")); !errors.Is(err, os.ErrNotExist) { + t.Errorf("create --output - wrote a file named \"-\": %v", err) + } + if code, out, _ := run(t, s, "", "create", "--output=-"); code != ExitOK || out != minimalTemplate { + t.Errorf("create --output=-: exit %d, stdout %q", code, out) + } +} + func TestValidate(t *testing.T) { root := t.TempDir() s := testSpec(root)