From 5153b9575f9e86d23f91c6a9d2d421e5bbed64d1 Mon Sep 17 00:00:00 2001 From: Jonathan Watson Date: Tue, 7 Jan 2025 13:16:22 +0000 Subject: [PATCH 1/3] fix: avoid adding '.' to path when there's no extension --- curl/template/template.go | 7 ++++++- e2e/upload/file/BUILD.bazel | 26 ++++++++++++++++++++++++ e2e/upload/file/fixture_no_extension.txt | 10 +++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 e2e/upload/file/fixture_no_extension.txt diff --git a/curl/template/template.go b/curl/template/template.go index 8d4f7fc..8cc03c2 100644 --- a/curl/template/template.go +++ b/curl/template/template.go @@ -47,7 +47,12 @@ type FileInfo struct { } func (f FileInfo) Basename() string { - return f.Stem.String() + "." + f.Extension.String() + var extension_string = f.Extension.String() + var suffix = "" + if extension_string != "" { + suffix = "." + extension_string + } + return f.Stem.String() + suffix } func (f FileInfo) Path() string { diff --git a/e2e/upload/file/BUILD.bazel b/e2e/upload/file/BUILD.bazel index 6826b08..0cdb043 100644 --- a/e2e/upload/file/BUILD.bazel +++ b/e2e/upload/file/BUILD.bazel @@ -25,3 +25,29 @@ diff_file_test( a = ":fixture.txt", b = ":execute", ) + + +curl_upload_file( + name = "upload_no_extension", + testonly = True, + src = ":fixture.txt", + dst = "directory/fixture", + url = "https://test.case", +) + +genrule( + name = "execute_no_extension", + testonly = True, + outs = [ + "upload_no_extension.out", + ], + cmd = "./$(location :upload_no_extension) > $@", + tools = [":upload_no_extension"], +) + +diff_file_test( + name = "test_no_extension", + size = "small", + a = ":fixture_no_extension.txt", + b = ":execute_no_extension", +) diff --git a/e2e/upload/file/fixture_no_extension.txt b/e2e/upload/file/fixture_no_extension.txt new file mode 100644 index 0000000..806c14a --- /dev/null +++ b/e2e/upload/file/fixture_no_extension.txt @@ -0,0 +1,10 @@ +--netrc +--location +--progress-bar +--retry +3 +--retry-delay +1 +--upload-file +fixture.txt +https://test.case/directory/fixture -- GitLab From 2c8a37e13c1fd49daa8e835f82ac397749b006d5 Mon Sep 17 00:00:00 2001 From: Jonathan Watson Date: Tue, 7 Jan 2025 13:25:04 +0000 Subject: [PATCH 2/3] fix: make dirname work with default template --- curl/upload/file/rule.bzl | 4 ++-- curl/upload/manifests/rule.bzl | 4 ++-- curl/upload/posix.tmpl.sh | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/curl/upload/file/rule.bzl b/curl/upload/file/rule.bzl index 58f9ff9..ddad0e1 100644 --- a/curl/upload/file/rule.bzl +++ b/curl/upload/file/rule.bzl @@ -60,10 +60,10 @@ def implementation(ctx): csv = ctx.actions.declare_file("{}.upload.csv".format(ctx.label.name)) href = ctx.attr.url.rstrip("/").replace(",", "%2C") - dst = ctx.attr.dst args = ctx.actions.args() - args.add("{},{},{}".format(ctx.file.src.short_path, "{{{{.URL.Href}}}}/{}".format(dst), href)) + # SRC, DST, TEMPLATE, URL + args.add("{},{},{},{}".format(ctx.file.src.short_path, ctx.attr.dst, "{{.URL.Href}}/{{.File.Path}}", href)) ctx.actions.run( outputs = [csv], diff --git a/curl/upload/manifests/rule.bzl b/curl/upload/manifests/rule.bzl index 1d1bdef..713ebd9 100644 --- a/curl/upload/manifests/rule.bzl +++ b/curl/upload/manifests/rule.bzl @@ -71,8 +71,8 @@ def implementation(ctx): href = ctx.attr.url.rstrip("/").replace(",", "%2C") def _to_string(m): - # SRC,URL_TEMPL,URL - return "{},{},{}".format(m.file.short_path, m.url, href) + # SRC, DST, TEMPLATE, URL + return "{},{},{},{}".format(m.file.short_path, m.file.short_path, m.url, href) args = ctx.actions.args() args.add_all(manifests, map_each = _to_string, allow_closure = True) diff --git a/curl/upload/posix.tmpl.sh b/curl/upload/posix.tmpl.sh index 632ea48..05687f3 100644 --- a/curl/upload/posix.tmpl.sh +++ b/curl/upload/posix.tmpl.sh @@ -51,13 +51,13 @@ upload() { "${2}" } -while IFS=, read -r SRC TMPL URL; do - DST=$( +while IFS=, read -r SRC DST TMPL URL; do + FULL_DST=$( "${TEMPLATE}" \ --url "${URL}" \ --template "${TMPL}" \ - --file "${SRC}" \ + --file "${DST}" \ ${@} ) - upload "${SRC}" "${DST}" + upload "${SRC}" "${FULL_DST}" done <"${CSV}" -- GitLab From f488ce06408f9abd67436e530cd43682d9d2d279 Mon Sep 17 00:00:00 2001 From: Jonathan Watson Date: Wed, 8 Jan 2025 09:33:06 +0000 Subject: [PATCH 3/3] refactor: improve basename code clarity --- curl/template/template.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/curl/template/template.go b/curl/template/template.go index 8cc03c2..1a0d740 100644 --- a/curl/template/template.go +++ b/curl/template/template.go @@ -47,12 +47,12 @@ type FileInfo struct { } func (f FileInfo) Basename() string { - var extension_string = f.Extension.String() - var suffix = "" - if extension_string != "" { - suffix = "." + extension_string + extension := f.Extension.String() + stem := f.Stem.String() + if extension == "" { + return stem } - return f.Stem.String() + suffix + return stem + "." + extension } func (f FileInfo) Path() string { -- GitLab