From 2568dc45c2e4fb40e99f1290a4560b7ec429b239 Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Tue, 8 Sep 2026 20:47:47 +1000 Subject: [PATCH] Fixed arg order of os bang --- ucl/builtins/os.go | 23 ++++++++++++++--------- ucl/builtins/os_test.go | 7 +++++-- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/ucl/builtins/os.go b/ucl/builtins/os.go index 925f9c8..0f3da1d 100644 --- a/ucl/builtins/os.go +++ b/ucl/builtins/os.go @@ -93,11 +93,20 @@ func (oh osHandlers) exec(ctx context.Context, args ucl.CallArgs) (any, error) { func (oh osHandlers) bang(ctx context.Context, args ucl.CallArgs) (any, error) { var ( - cmdStr string + cmdStr string + stdIn string + hasStdin bool ) - if err := args.Bind(&cmdStr); err != nil { - return nil, err + if args.NArgs() == 1 { + if err := args.Bind(&cmdStr); err != nil { + return nil, err + } + } else { + if err := args.Bind(&stdIn, &cmdStr); err != nil { + return nil, err + } + hasStdin = true } cmd, err := oh.provider.ExecBang(ctx, cmdStr) @@ -105,12 +114,8 @@ func (oh osHandlers) bang(ctx context.Context, args ucl.CallArgs) (any, error) { return nil, err } - if args.NArgs() > 0 { - var stdin string - if err := args.Bind(&stdin); err != nil { - return nil, err - } - cmd.Stdin = strings.NewReader(stdin) + if hasStdin { + cmd.Stdin = strings.NewReader(stdIn) } res, err := cmd.Output() diff --git a/ucl/builtins/os_test.go b/ucl/builtins/os_test.go index 748d632..ed30ba5 100644 --- a/ucl/builtins/os_test.go +++ b/ucl/builtins/os_test.go @@ -69,8 +69,11 @@ func TestOS_Bang(t *testing.T) { }{ {descr: "run command 1", eval: `os:! "echo 'hello, world'"`, want: "hello, world\n"}, {descr: "run command 2", eval: `os:! "date +%Y%m%d"`, want: time.Now().Format("20060102") + "\n"}, - {descr: "run command 3", eval: `os:! "tr [a-z] [A-Z]" "hello"`, want: "HELLO"}, - {descr: "run command 4", eval: `os:! "tr -d e" "hello"`, want: "hllo"}, + {descr: "run command 3", eval: `os:! "hello" "tr [a-z] [A-Z]"`, want: "HELLO"}, + {descr: "run command 4", eval: `os:! "hello" "tr -d e" "hello"`, want: "hllo"}, + {descr: "run command 5", eval: `"hello" | os:! "tr [a-z] [A-Z]"`, want: "HELLO"}, + {descr: "run command 6", eval: `"hello" | os:! "tr -d e"`, want: "hllo"}, + {descr: "run command 7", eval: `"hello" | os:! "tr -d e" | os:! "tr [a-z] [A-Z]"`, want: "HLLO"}, } for _, tt := range tests {