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
This commit is contained in:
+14
-3
@@ -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'\"$`\\*?[]{}()<>|&;#~!") {
|
||||
|
||||
@@ -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},
|
||||
} {
|
||||
|
||||
Reference in New Issue
Block a user