From acc71a82a6449450498413f4b0b19372f7b97950 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) to 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 Dispatch-Trailer: {"type":"trybot","CL":1177013,"patchset":4,"ref":"refs/changes/13/1177013/4","targetBranch":"alpha"} --- 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 c71804e9bd..bf1c968c21 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 1b885856a6..36a69f5adf 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 4e1de4a1ea..9cd73179da 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": "9v5xA21u0+ZiHt6/cqBWgR29ErERw+0r7ZdN9YrBMDA=" "upload-some-cue": "tP3KoyKTIn79Gk8U9FATNuou3WfinCe3OvMMJIGDL+Y=" "upload-some-json": "xZav4QaIGOVUoGgl6FLH1FyMGWS9+cOFaTTEoD5mc1U=" "in-subdir": "WFUtw4fPeq+fBaYmRr1v6Va3OJiB3Pht2bJZgs+whQU=" } multi_step: { - "J523PNTTH7RBNDJU4R39J6VCBERKCGAQASGSROP9NVDJ7EEPFC4G====": [{ + "7HR2L5CP9OEINJ1A2AGDLFO5DR2378A0TEDTCLBMHBMVATC7BR40====": [{ doc: """ # script doc comment #scripttag diff --git a/internal/cmd/preprocessor/cmd/upload_node.go b/internal/cmd/preprocessor/cmd/upload_node.go index ffc508d08e..87e5de7aa2 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)