From 3e932d5e5f59b1b0348ae7749020ec78d9182ca4 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Wed, 7 Oct 2026 21:47:27 +0200 Subject: [PATCH] Answer architecture round 5, stability round 4 and product-owner round 4: an argument that is a lone reference is echoed back, and a hint for a template holding a backslash pipes it through printf --- cmd/fejkdata/main.go | 17 ++++++++++++++--- cmd/fejkdata/main_test.go | 3 ++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/fejkdata/main.go b/cmd/fejkdata/main.go index 620b640..af9bfb8 100644 --- a/cmd/fejkdata/main.go +++ b/cmd/fejkdata/main.go @@ -292,10 +292,12 @@ func operandError(arg string) error { return fmt.Errorf("%s; read a file from stdin: fejkdata < %s", unexpected, shellQuoted(arg)) } example := pipeHint - if inline, err := fejkdata.IsTemplate(arg); inline { - example = "echo " + shellQuoted(arg) + " | fejkdata" + if _, lone := loneReference(arg); lone { + example = piped(arg) + } else if inline, err := fejkdata.IsTemplate(arg); inline { + example = piped(arg) } else if _, lone := loneReference("{/" + arg + "}"); lone && err == nil { - example = "echo " + shellQuoted("{/"+arg+"}") + " | fejkdata" + example = piped("{/" + arg + "}") } return fmt.Errorf("%s; the template is read from stdin: %s", unexpected, example) } @@ -492,6 +494,15 @@ func spacedReference(template string, err error) error { return err } +// piped is the command piping template to fejkdata; some shells' echo reads a backslash +// as an escape, so a template holding one goes through printf. +func piped(template string) string { + if strings.ContainsRune(template, '\\') { + return "printf '%s' " + shellQuoted(template) + " | fejkdata" + } + return "echo " + shellQuoted(template) + " | fejkdata" +} + // shellQuoted is s as a shell reads it back: bare where it holds nothing a shell expands. func shellQuoted(s string) string { if s != "" && !strings.ContainsAny(s, " \t\r\n'\"$`\\*?[]{}()<>|&;#~!") { diff --git a/cmd/fejkdata/main_test.go b/cmd/fejkdata/main_test.go index 0aaef6c..a08e81b 100644 --- a/cmd/fejkdata/main_test.go +++ b/cmd/fejkdata/main_test.go @@ -249,10 +249,11 @@ func TestRunTakesNoOperand(t *testing.T) { want string }{ {[]string{"sv_SE.person"}, "echo '{/sv_SE.person}' | fejkdata"}, - {[]string{"{/sv_SE.person}"}, "echo '{/sv_SE.person}' | fejkdata"}, + {[]string{"{/en_US.person}"}, "echo '{/en_US.person}' | fejkdata"}, {[]string{"name: {/sv_SE.person.last}"}, "echo 'name: {/sv_SE.person.last}' | fejkdata"}, {[]string{"geo.US.locality[O'Fallon].name"}, `echo '{/geo.US.locality[O'\''Fallon].name}' | fejkdata`}, {[]string{"en_US.address"}, "echo '{/en_US.address}' | fejkdata"}, + {[]string{`C:\temp {/sv_SE.person}`}, `printf '%s' 'C:\temp {/sv_SE.person}' | fejkdata`}, {[]string{"-"}, "drop it"}, {[]string{file}, "fejkdata < " + file}, } {