Skip to content

Commit

Permalink
docs/howto: fix fetch-data-over-http flake
Browse files Browse the repository at this point in the history
This updates /docs/howto/fetch-json-data-http so that it fetches its
example JSON data from a local server whilst displaying a
publically-available URL on the rendered page. The preprocessor
pattern/replacement system is used to perform the switch, instead of
swapping out the file with a hidden upload behind the scenes, in order
to ensure that the tool file presented is being tested in CI.

Fixes cue-lang/cue#3359

Preview-Path: /docs/howto/fetch-json-data-http/
Signed-off-by: Jonathan Matthews <[email protected]>
Change-Id: Idc532da6cc8a40c1f1c793b2c4c26ae1db436bd8
Dispatch-Trailer: {"type":"trybot","CL":1199216,"patchset":2,"ref":"refs/changes/16/1199216/2","targetBranch":"master"}
  • Loading branch information
jpluscplusm authored and cueckoo committed Aug 12, 2024
1 parent 590f6a5 commit c925e37
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 15 deletions.
55 changes: 48 additions & 7 deletions content/docs/howto/fetch-json-data-http/en.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,56 @@ tags:
- workflow command
---

{{{with _script_ "en" "HIDDEN: set caches to speed up re-running"}}}
export GOMODCACHE=/caches/gomodcache
export GOCACHE=/caches/gobuild
{{{end}}}

{{{with _upload_ "en" "HIDDEN: server.go"}}}
-- server.go --
package main

import (
"fmt"
"net/http"
)

// Mirror of https://proxy.golang.org/cached-only/cuelang.org/go/@v/v0.8.2.info
var response = `{"Version":"v0.8.2","Time":"2024-04-26T11:47:18Z","Origin":{"VCS":"git","URL":"https://review.gerrithub.io/cue-lang/cue","Ref":"refs/tags/v0.8.2","Hash":"596c99191ad1eb7c39d547e59bc7085751d7952b"}}`
var hostport = "{{{.HOSTPORT}}}"

func main() {
fmt.Println("Starting server on " + hostport)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, response)
})

if err := http.ListenAndServe(hostport, nil); err != nil {
fmt.Println("Server failed:", err)
}
}
{{{end}}}

{{{with _script_ "en" "HIDDEN: go run server"}}}
# TODO: This is technically racey, but we adopt the same posture as
# https://cuelang.org/cl/1176732: let's wait for it to actually become a
# problem before we worry about it. That said, we do build the executable
# before running it (instead of "go run server.go") to try and /help/ avoid an
# HTTP connection being attempted before the server is listening.
go build server.go
nohup ./server > /tmp/howto-fetch-json-data-http.log 2>&1 &
sleep 1
rm server
{{{end}}}

This guide demonstrates how to use a CUE workflow command to fetch some JSON over HTTP.

It is converted from its JSON representation into data,
which is then used by the workflow command.

<!-- TODO: consider demonstrating a validation failure when https://github.com/cue-lang/cue/issues/3186 is resolved -->

{{{with code "en" "cc"}}}
#location top bottom
exec cue cmd info
cmp stdout out
{{{with upload "en" "cc"}}}
-- a_tool.cue --
package example

Expand All @@ -29,7 +68,7 @@ import (

command: info: {
fetch: http.Get & {
url: "https://proxy.golang.org/cached-only/cuelang.org/go/@v/v0.8.2.info"
url: "{{{.URI}}}"
}
display: cli.Print & {
_data: #info & json.Unmarshal(fetch.response.body)
Expand All @@ -47,8 +86,10 @@ command: info: {
}
...
}
-- out --
CUE version v0.8.2 was released with commit hash 596c99191ad1eb7c39d547e59bc7085751d7952b
{{{end}}}

{{{with script "en" "command"}}}
cue cmd info
{{{end}}}

## Related content
Expand Down
54 changes: 52 additions & 2 deletions content/docs/howto/fetch-json-data-http/gen_cache.cue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,58 @@ package site
"fetch-json-data-http": {
page: {
cache: {
code: {
cc: "/fGkdvJxXOT35Y7SNskPvdKPhPv5VyWGtu35bO7HM0U="
upload: {
"HIDDEN: server.go": "JHkx7wmlvN9Mm/e5Njc45zyl9qA5nsCe639Kd9nReB4="
cc: "Myu8x8nfORuqACXzx+RW+VccQ7BgUhkzR+FfCPvCdyA="
}
multi_step: {
hash: "1LV2P49HO59VUT3PJIASINTAO98F97PLBHTRSNQ978SPNRVUN000===="
scriptHash: "HFFNH43U957JTQV8V6FJPCNFU0132TPRFDVKPL6ILOUIJKE4S4HG===="
steps: [{
doc: ""
cmd: "export GOMODCACHE=/caches/gomodcache"
exitCode: 0
output: ""
}, {
doc: ""
cmd: "export GOCACHE=/caches/gobuild"
exitCode: 0
output: ""
}, {
doc: """
# TODO: This is technically racey, but we adopt the same posture as
# https://cuelang.org/cl/1176732: let's wait for it to actually become a
# problem before we worry about it. That said, we do build the executable
# before running it (instead of "go run server.go") to try and /help/ avoid an
# HTTP connection being attempted before the server is listening.
"""
cmd: "go build server.go"
exitCode: 0
output: ""
}, {
doc: ""
cmd: "nohup ./server >/tmp/howto-fetch-json-data-http.log 2>&1 &"
exitCode: 0
output: ""
}, {
doc: ""
cmd: "sleep 1"
exitCode: 0
output: ""
}, {
doc: ""
cmd: "rm server"
exitCode: 0
output: ""
}, {
doc: ""
cmd: "cue cmd info"
exitCode: 0
output: """
CUE version v0.8.2 was released with commit hash 596c99191ad1eb7c39d547e59bc7085751d7952b
"""
}]
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion content/docs/howto/fetch-json-data-http/page.cue
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
package site

content: docs: howto: "fetch-json-data-http": page: _
content: docs: howto: "fetch-json-data-http": page: {
vars: {
// A random /32+port from the loopback's 127.0.0.1/8 to reduce the chance
// of pages with network listeners colliding (if indeed they even share the
// IP range, because Docker).
HOSTPORT: "127.1.2.3:4567"
URI: {
pattern: "http://\(HOSTPORT)/"
replacement: "https://proxy.golang.org/cached-only/cuelang.org/go/@v/v0.8.2.info"
}
}
}
10 changes: 5 additions & 5 deletions hugo/content/en/docs/howto/fetch-json-data-http/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ which is then used by the workflow command.
<!-- TODO: consider demonstrating a validation failure when https://github.com/cue-lang/cue/issues/3186 is resolved -->

{{< code-tabs >}}
{{< code-tab name="a_tool.cue" language="cue" area="top" >}}
{{< code-tab name="a_tool.cue" language="cue" area="top-left" >}}
package example

import (
Expand Down Expand Up @@ -44,12 +44,12 @@ command: info: {
}
...
}
{{< /code-tab >}}
{{< code-tab name="TERMINAL" language="" area="bottom" type="terminal" codetocopy="Y3VlIGNtZCBpbmZv" >}}
{{< /code-tab >}}{{< /code-tabs >}}

```text { title="TERMINAL" type="terminal" codeToCopy="Y3VlIGNtZCBpbmZv" }
$ cue cmd info
CUE version v0.8.2 was released with commit hash 596c99191ad1eb7c39d547e59bc7085751d7952b
{{< /code-tab >}}
{{< /code-tabs >}}
```

## Related content

Expand Down

0 comments on commit c925e37

Please sign in to comment.