From 865958a54743b44d380a2a08c5cf0995629dbe7e Mon Sep 17 00:00:00 2001 From: Jonathan Watson Date: Tue, 11 Feb 2025 17:14:34 +0000 Subject: [PATCH 1/4] feat: allow runtime output file --- curl/upload/upload.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/curl/upload/upload.go b/curl/upload/upload.go index bf3dcce..8437cc7 100644 --- a/curl/upload/upload.go +++ b/curl/upload/upload.go @@ -28,7 +28,7 @@ func (r *RunfileVar) Set(value string) error { return nil } -func upload(curl string, src string, dest string, retry uint64, retry_delay uint64) error { +func upload(curl string, src string, output string, dest string, retry uint64, retry_delay uint64) error { args := []string{ "--netrc", "--location", @@ -38,6 +38,11 @@ func upload(curl string, src string, dest string, retry uint64, retry_delay uint "--upload-file", src, "--url", dest, } + + if output != "" { + args = append(args, "--output", output) + } + cmd := exec.Command(curl, args...) cmd.Stdout = os.Stdout @@ -79,6 +84,7 @@ func main() { var retry_delay uint64 var src RunfileVar var url string + var output string flag.Var(&curl, "curl", "The path to the curl bin") flag.Uint64Var(&retry, "retry", 0, "The number of times to retry the request") @@ -86,6 +92,7 @@ func main() { flag.Var(&src, "src", "Path to the source file") flag.StringVar(&url, "url", "", "The URL to upload to") + flag.StringVar(&output, "output", "", "Output file to use") args_file, err := runfiles.Rlocation("upload.args") if err != nil { @@ -98,8 +105,7 @@ func main() { } flag.Parse() - - err = upload(curl.String(), src.String(), url, retry, retry_delay) + err = upload(curl.String(), src.String(), output, url, retry, retry_delay) if err != nil { log.Fatal(err) } -- GitLab From 4f938eeaa5a38b65c7b4e89b5f9c724fb1ab0aec Mon Sep 17 00:00:00 2001 From: Jonathan Watson Date: Tue, 11 Feb 2025 17:14:56 +0000 Subject: [PATCH 2/4] feat: print stderr for curl --- curl/upload/upload.go | 1 + 1 file changed, 1 insertion(+) diff --git a/curl/upload/upload.go b/curl/upload/upload.go index 8437cc7..870ad5b 100644 --- a/curl/upload/upload.go +++ b/curl/upload/upload.go @@ -46,6 +46,7 @@ func upload(curl string, src string, output string, dest string, retry uint64, r cmd := exec.Command(curl, args...) cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr err := cmd.Run() if err != nil { -- GitLab From 4614de3d599944c4e81224b5f17e0fbad8e5d779 Mon Sep 17 00:00:00 2001 From: Jonathan Watson Date: Tue, 11 Feb 2025 14:50:27 +0000 Subject: [PATCH 3/4] fix: pass runfile environment to command --- curl/upload/upload.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/curl/upload/upload.go b/curl/upload/upload.go index 870ad5b..7931535 100644 --- a/curl/upload/upload.go +++ b/curl/upload/upload.go @@ -45,10 +45,17 @@ func upload(curl string, src string, output string, dest string, retry uint64, r cmd := exec.Command(curl, args...) + runfilesEnv, err := runfiles.Env() + if err != nil { + return err + } + + cmd.Env = append(os.Environ(), runfilesEnv...) + cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - err := cmd.Run() + err = cmd.Run() if err != nil { return err } -- GitLab From ddb2a3d86238361ae32892542df3ddf52d3c5ad3 Mon Sep 17 00:00:00 2001 From: Jonathan Watson Date: Wed, 12 Feb 2025 11:43:15 +0000 Subject: [PATCH 4/4] chore: add tests for --output --- e2e/mock/curl.sh | 1 + e2e/upload/file/BUILD.bazel | 18 ++++++++++++++++++ e2e/upload/file/expected_with_output.txt | 13 +++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 e2e/upload/file/expected_with_output.txt diff --git a/e2e/mock/curl.sh b/e2e/mock/curl.sh index 1357249..92734a1 100755 --- a/e2e/mock/curl.sh +++ b/e2e/mock/curl.sh @@ -7,6 +7,7 @@ while test "${#}" -ne 0; do shift ;; "--upload-file") + # Check that the input files exist printf '%s\n' "${1}" shift if ! test -e "${1}"; then diff --git a/e2e/upload/file/BUILD.bazel b/e2e/upload/file/BUILD.bazel index d3e917f..1490884 100644 --- a/e2e/upload/file/BUILD.bazel +++ b/e2e/upload/file/BUILD.bazel @@ -76,3 +76,21 @@ curl_upload_file( ) for test in ["basic", "original_filename", "injection", "no_extension"] ] + +# Check that --output is forwarded on to curl binary. +genrule( + name = "execute_with_output", + testonly = True, + outs = [ + "upload_with_output.out", + ], + cmd = "./$(location :upload_basic) --output output_file.txt > $@", + tools = [":upload_basic"], +) + +diff_file_test( + name = "test_output", + size = "small", + a = ":expected_with_output.txt", + b = ":upload_with_output.out" +) diff --git a/e2e/upload/file/expected_with_output.txt b/e2e/upload/file/expected_with_output.txt new file mode 100644 index 0000000..f7cf456 --- /dev/null +++ b/e2e/upload/file/expected_with_output.txt @@ -0,0 +1,13 @@ +--netrc +--location +--progress-bar +--retry +3 +--retry-delay +1 +--upload-file +input.txt +--url +https://test.case/directory/fixture.txt +--output +output_file.txt -- GitLab