diff --git a/content/docs/tutorial/validating-simple-yaml-files/en.md b/content/docs/tutorial/validating-simple-yaml-files/en.md index ed42c297d..9e5129fa7 100644 --- a/content/docs/tutorial/validating-simple-yaml-files/en.md +++ b/content/docs/tutorial/validating-simple-yaml-files/en.md @@ -65,7 +65,7 @@ Place this information in it, including the deliberate mistake in the `species` field: {{{with upload "en" "yamlBroken"}}} --- charlie.yml -- +-- validating-yaml-with-cue/charlie.yml -- name: first: Charlie last: Cartwright @@ -79,7 +79,7 @@ Create a file called `pets.cue` to hold your schema, and place this CUE in it: {{{with upload "en" "pets.cue"}}} #codetab(pets.cue) linenos="table" --- pets.cue -- +-- validating-yaml-with-cue/pets.cue -- species!: "cat" | "dog" age?: number {{{end}}} @@ -151,7 +151,7 @@ Update `charlie.yml` to: Your corrected data file should read as follows: {{{with upload "en" "yamlFixed"}}} --- charlie.yml -- +-- validating-yaml-with-cue/charlie.yml -- name: first: Charlie last: Cartwright @@ -189,7 +189,7 @@ Add a second data file containing the details of another pet. Create a data file named `toby.yml` to hold Toby the dog's details: {{{with upload "en" "yaml2"}}} --- toby.yml -- +-- validating-yaml-with-cue/toby.yml -- name: first: Toby last: Dog diff --git a/hugo/content/en/docs/tutorial/validating-simple-yaml-files/index.md b/hugo/content/en/docs/tutorial/validating-simple-yaml-files/index.md index 7b57f4692..fc651096f 100644 --- a/hugo/content/en/docs/tutorial/validating-simple-yaml-files/index.md +++ b/hugo/content/en/docs/tutorial/validating-simple-yaml-files/index.md @@ -65,7 +65,7 @@ Create a data file named `charlie.yml` to hold Charlie the cat's details. Place this information in it, including the deliberate mistake in the `species` field: -```yml { title="charlie.yml" } +```yml { title="validating-yaml-with-cue/charlie.yml" } name: first: Charlie last: Cartwright @@ -77,7 +77,7 @@ age: "15" {{< step stepNumber="4" >}} Create a file called `pets.cue` to hold your schema, and place this CUE in it: -```cue { title="pets.cue" linenos="table" } +```cue { title="validating-yaml-with-cue/pets.cue" } species!: "cat" | "dog" age?: number ``` @@ -158,7 +158,7 @@ Update `charlie.yml` to: Your corrected data file should read as follows: -```yml { title="charlie.yml" } +```yml { title="validating-yaml-with-cue/charlie.yml" } name: first: Charlie last: Cartwright @@ -195,7 +195,7 @@ Add a second data file containing the details of another pet. Create a data file named `toby.yml` to hold Toby the dog's details: -```yml { title="toby.yml" } +```yml { title="validating-yaml-with-cue/toby.yml" } name: first: Toby last: Dog diff --git a/internal/cmd/preprocessor/cmd/rootfile.go b/internal/cmd/preprocessor/cmd/rootfile.go index 80b7d839a..a9d3bd6ab 100644 --- a/internal/cmd/preprocessor/cmd/rootfile.go +++ b/internal/cmd/preprocessor/cmd/rootfile.go @@ -24,7 +24,6 @@ import ( "math/rand" "os" "os/exec" - "path" "path/filepath" "runtime" "slices" @@ -433,21 +432,40 @@ func (rf *rootFile) buildMultistepScript() (*multiStepScript, error) { f := n.effectiveArchive.Files[0] cmdEchoFence := rf.getFence() + + // The upload target path provided via the txtar filename _might_ be + // absolute. We don't know. In case it is, we should treat it as + // such. Otherwise, as a convenience we treat it as relative to the + // starting working directory of the script, i.e. $HOME. The problem + // is that we can only determine if the target is absolute in the script + // itself (allowing for env var expansion, etc). So we use a unique + // variable name to store the target file path, which is made absolute + // as required. + targetFileVar := "target" + rf.getFence() + pf("if [[ \"%s\" != /* ]]; then %s=$HOME/%s; else %s=%s; fi\n", f.Name, targetFileVar, f.Name, targetFileVar, f.Name) + + // First echo the commands that we will run to make debugging + // easier. Notice this block is surrounded in a no-interpolation + // cat using a pseudo-random string fence. pf("cat <<'%s'\n", cmdEchoFence) if strings.Contains(f.Name, "/") { - pf("$ mkdir -p '%s'\n", path.Dir(f.Name)) + pf("$ mkdir -p $(dirname $%s)\n", targetFileVar) } - pf("$ cat < %s\n", f.Name) + pf("$ cat < $%s\n", targetFileVar) pf("%s\n", f.Data) pf("EOD\n") pf("%s\n", cmdEchoFence) + + // Now write the actual commands to the file. fence := rf.getFence() if strings.Contains(f.Name, "/") { - pf("mkdir -p '%s'\n", path.Dir(f.Name)) + pf("mkdir -p $(dirname $%s)\n", targetFileVar) } - pf("cat <<'%s' > %s\n", fence, f.Name) + pf("cat <<'%s' > $%s\n", fence, targetFileVar) pf("%s\n", f.Data) pf("%s\n", fence) + + // Check the exist code pf("%s=$?\n", exitCodeVar) pf("if [[ $%s -ne 0 ]]\n", exitCodeVar) pf("then\n") diff --git a/internal/cmd/preprocessor/cmd/testdata/execute_check_upload_structure.txtar b/internal/cmd/preprocessor/cmd/testdata/execute_check_upload_structure.txtar new file mode 100644 index 000000000..8c8390c5f --- /dev/null +++ b/internal/cmd/preprocessor/cmd/testdata/execute_check_upload_structure.txtar @@ -0,0 +1,92 @@ +# Ensure that we create uploaded files in the right place, +# regardless of working directory at the time of the upload. + +unquote content/dir/en.md + +exec preprocessor execute + +cmp hugo/content/en/dir/index.md golden/hugo/content/en/dir/index.md + +-- hugo/.keep -- +-- content/site.cue -- +package site +-- content/dir/page.cue -- +package site + +content: dir: page: { + leftDelim: "{{{" + rightDelim: "}}}" +} +-- content/dir/en.md -- +>--- +>title: JSON Superset +>--- +> +>{{{with script "en" "1"}}} +>find $HOME -type f | sort +>{{{end}}} +> +>{{{with upload "en" "2"}}} +>-- one.txt -- +>{{{end}}} +>{{{with upload "en" "3"}}} +>-- another/two.txt -- +>{{{end}}} +> +>{{{with script "en" "4"}}} +>find $HOME -type f | sort +>cd another +>{{{end}}} +> +>{{{with upload "en" "5"}}} +>-- one.txt -- +>{{{end}}} +>{{{with upload "en" "6"}}} +>-- another/two.txt -- +>{{{end}}} +> +>{{{with script "en" "7"}}} +>find $HOME -type f | sort +>{{{end}}} +> +-- golden/hugo/content/en/dir/index.md -- +--- +title: JSON Superset +--- + +```text { title="TERMINAL" codeToCopy="ZmluZCAkSE9NRSAtdHlwZSBmIHwgc29ydAo=" } +$ find $HOME -type f | sort +/home/runner/.bash_logout +/home/runner/.bashrc +/home/runner/.profile +``` + +```txt { title="one.txt" } +``` +```txt { title="another/two.txt" } +``` + +```text { title="TERMINAL" codeToCopy="ZmluZCAkSE9NRSAtdHlwZSBmIHwgc29ydApjZCBhbm90aGVyCg==" } +$ find $HOME -type f | sort +/home/runner/.bash_logout +/home/runner/.bashrc +/home/runner/.profile +/home/runner/another/two.txt +/home/runner/one.txt +$ cd another +``` + +```txt { title="one.txt" } +``` +```txt { title="another/two.txt" } +``` + +```text { title="TERMINAL" codeToCopy="ZmluZCAkSE9NRSAtdHlwZSBmIHwgc29ydAo=" } +$ find $HOME -type f | sort +/home/runner/.bash_logout +/home/runner/.bashrc +/home/runner/.profile +/home/runner/another/two.txt +/home/runner/one.txt +``` + diff --git a/internal/cmd/preprocessor/cmd/testdata/execute_multistagescript.txtar b/internal/cmd/preprocessor/cmd/testdata/execute_multistagescript.txtar index d01ae8571..efcc9ba80 100644 --- a/internal/cmd/preprocessor/cmd/testdata/execute_multistagescript.txtar +++ b/internal/cmd/preprocessor/cmd/testdata/execute_multistagescript.txtar @@ -220,7 +220,7 @@ package site "in-subdir": "Zwiy4x77+BFu61goz1QtYAalWU2pz5BnNGntPD/6asM=" } multi_step: { - "377NPLQLLSANR3SP4BDR88KQU32BUBAPDARQ3K4LHMK8MF2VI6F0====": [{ + "CCDBDCU5E1MLC2JR58VL9NCT6F49M45C3U8H598PETOOFTBJCR1G====": [{ doc: """ # script doc comment #scripttag