From d9e26e62f143f918a5df36c58f9387a681cbc029 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Sun, 18 Feb 2024 14:33:24 +0000 Subject: [PATCH] preprocessor: support hidden uploads This allows guides which only choose to present select files (as opposed to a full tutorial from start to end) can still be validated as working, by uploading files which the user does not see. (The files themselves are not hidden, just the upload block itself is not rendered in the Hugo output). Preprocessor-No-Write-Cache: true Signed-off-by: Paul Jolly Change-Id: Ia7289d0fc730c744375c23a4136e57493baa1fcb --- internal/cmd/preprocessor/cmd/parse.go | 8 ++++++-- internal/cmd/preprocessor/cmd/rootfile.go | 1 + .../cmd/testdata/execute_multistagescript.txtar | 15 ++++++++++++++- internal/cmd/preprocessor/cmd/upload_node.go | 13 ++++++++++++- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/internal/cmd/preprocessor/cmd/parse.go b/internal/cmd/preprocessor/cmd/parse.go index c71804e9b..bf1c968c2 100644 --- a/internal/cmd/preprocessor/cmd/parse.go +++ b/internal/cmd/preprocessor/cmd/parse.go @@ -216,12 +216,16 @@ func (rf *rootFile) parse_WithNode(n *parse.WithNode) (node, error) { // Increment first because we are numbering from 1 rf.stepNumber++ return rf.parse_stepNode(n, rf.stepNumber) - case fnUpload: + case fnUpload, fnHiddenUpload: + hidden := fn.Ident == fnHiddenUpload t, err := rf.parse_txtarNode(n, fn.Ident, c.Args[1:]) if err != nil { return nil, err } - return &uploadNode{txtarNode: t}, nil + return &uploadNode{ + txtarNode: t, + hidden: hidden, + }, nil case fnScript, fnHiddenScript: hidden := fn.Ident == fnHiddenScript t, err := rf.parse_txtarNode(n, fn.Ident, c.Args[1:]) diff --git a/internal/cmd/preprocessor/cmd/rootfile.go b/internal/cmd/preprocessor/cmd/rootfile.go index 1b885856a..36a69f5ad 100644 --- a/internal/cmd/preprocessor/cmd/rootfile.go +++ b/internal/cmd/preprocessor/cmd/rootfile.go @@ -47,6 +47,7 @@ var ( fnCode: true, fnStep: true, fnUpload: true, + fnHiddenUpload: true, fnScript: true, fnHiddenScript: true, "reference": true, diff --git a/internal/cmd/preprocessor/cmd/testdata/execute_multistagescript.txtar b/internal/cmd/preprocessor/cmd/testdata/execute_multistagescript.txtar index 6db40fade..4882b353a 100644 --- a/internal/cmd/preprocessor/cmd/testdata/execute_multistagescript.txtar +++ b/internal/cmd/preprocessor/cmd/testdata/execute_multistagescript.txtar @@ -71,6 +71,12 @@ content: dir: page: { >title: JSON Superset >--- >{{{with step}}} +> +>{{{with _upload "en" "hidden file"}}} +>-- hidden.txt -- +>Secret message +>{{{end}}} +> >{{{with upload "en" "upload-some-cue"}}} >#codetab(in.cue) linenos="table" >-- in.cue -- @@ -121,6 +127,12 @@ content: dir: page: { >title: JSON Superset >--- >{{{with step}}} +> +>{{{with _upload "en" "hidden file"}}} +>-- hidden.txt -- +>Secret message +>{{{end}}} +> >{{{with upload "en" "upload-some-cue"}}} >#codetab(in.cue) linenos="table" >-- in.cue -- @@ -233,12 +245,13 @@ package site page: { cache: { upload: { + "hidden file": "4YPap9a6NsyJd6KTPyStBOl3FcJsYecsnKRpcixFfrY=" "upload-some-cue": "mk/eGLXQi8f2VHFEAJ+OVwSB0C/zK9S8nvtSvKYNyVo=" "upload-some-json": "aOFY1SpvCxmIPWnBoZDfTCvL90OvzbTTD3gqObCa75w=" "in-subdir": "zLF5NHI4Y1eaRJ7n4t6bN//ptgOj2snRyP3DNUSUZrw=" } multi_step: { - "L4H91P78HIDIAEMM7U0CA93FTU24VD5B7K7E0BRUPJPUM8AV9RV0====": [{ + "3UA3H59NARBLHE6A3D5HAM0BD8RJH3L54NR16HOO9JUDU58IM1HG====": [{ doc: """ # script doc comment #scripttag diff --git a/internal/cmd/preprocessor/cmd/upload_node.go b/internal/cmd/preprocessor/cmd/upload_node.go index ffc508d08..87e5de7aa 100644 --- a/internal/cmd/preprocessor/cmd/upload_node.go +++ b/internal/cmd/preprocessor/cmd/upload_node.go @@ -18,10 +18,17 @@ import ( "bytes" ) -const fnUpload = "upload" +const ( + fnUpload = "upload" + fnHiddenUpload = "_upload" +) type uploadNode struct { txtarNode + + // hidden is set to indicate the script exists for side effects + // only and will not be rendered + hidden bool } var _ validatingNode = (*uploadNode)(nil) @@ -30,6 +37,10 @@ func (u *uploadNode) nodeType() string { return "upload" } +func (u *uploadNode) isHidden() bool { + return u.hidden +} + func (u *uploadNode) validate() { if l := len(u.analysis.fileNames); l != 1 { u.errorf("%v: upload nodes can only contain a single file; saw %d", u, l)