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 <noreply@anthropic.com>
This commit is contained in:
1 parent
5d141c7cef
commit
975340b5f7
2 files changed
+28
-3
No files matched your search
@@ -2,7 +2,7 @@
|
|||||||
// Yetano program standard (docs/program-conventions.md and ADR-0012 in
|
// Yetano program standard (docs/program-conventions.md and ADR-0012 in
|
||||||
// the yetano meta-repository):
|
// the yetano meta-repository):
|
||||||
//
|
//
|
||||||
// <program> config create [--minimal|--full] [--force] [--output <file>]
|
// <program> config create [--minimal|--full] [--force] [--output <file>|-]
|
||||||
// <program> config validate
|
// <program> config validate
|
||||||
// <program> config upgrade [--write]
|
// <program> config upgrade [--write]
|
||||||
// <program> config path
|
// <program> config path
|
||||||
@@ -201,7 +201,7 @@ func usage(s Spec, w io.Writer) {
|
|||||||
}
|
}
|
||||||
fmt.Fprintf(w, `usage: %[1]s config <subcommand> [--config <file>]
|
fmt.Fprintf(w, `usage: %[1]s config <subcommand> [--config <file>]
|
||||||
|
|
||||||
create [--minimal|--full] [--force] [--output <file>]
|
create [--minimal|--full] [--force] [--output <file>|-]
|
||||||
write a commented starter configuration (minimal by default)
|
write a commented starter configuration (minimal by default)
|
||||||
validate check the configuration file the program would use
|
validate check the configuration file the program would use
|
||||||
upgrade [--write]
|
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)")
|
minimal := fs.Bool("minimal", false, "write the minimal starter configuration (default)")
|
||||||
full := fs.Bool("full", false, "write the fully commented reference configuration")
|
full := fs.Bool("full", false, "write the fully commented reference configuration")
|
||||||
force := fs.Bool("force", false, "overwrite the file if it already exists")
|
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 {
|
if code := parse(fs, args); code >= 0 {
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
@@ -356,6 +356,10 @@ func (s Spec) create(explicit string, args []string, stdout, stderr io.Writer) i
|
|||||||
}
|
}
|
||||||
|
|
||||||
path := *output
|
path := *output
|
||||||
|
if path == "-" { // the template on stdout, nothing written
|
||||||
|
io.WriteString(stdout, content)
|
||||||
|
return ExitOK
|
||||||
|
}
|
||||||
if path == "" {
|
if path == "" {
|
||||||
var err error
|
var err error
|
||||||
if path, err = s.defaultCreatePath(explicit); err != nil {
|
if path, err = s.defaultCreatePath(explicit); err != nil {
|
||||||
|
|||||||
@@ -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) {
|
func TestValidate(t *testing.T) {
|
||||||
root := t.TempDir()
|
root := t.TempDir()
|
||||||
s := testSpec(root)
|
s := testSpec(root)
|
||||||
|
|||||||
Reference in new issue
Block a user