diff --git a/content/docs/howto/fetch-json-data-http/en.md b/content/docs/howto/fetch-json-data-http/en.md index 9f9919b77..5bb47c08f 100644 --- a/content/docs/howto/fetch-json-data-http/en.md +++ b/content/docs/howto/fetch-json-data-http/en.md @@ -7,6 +7,49 @@ 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" +) + +// Taken from 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 responder(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, response) +} + +func main() { + fmt.Println("Starting server on " + hostport) + http.HandleFunc("/", responder) + 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, @@ -14,10 +57,7 @@ which is then used by the workflow command. -{{{with code "en" "cc"}}} -#location top bottom -exec cue cmd info -cmp stdout out +{{{with upload "en" "cc"}}} -- a_tool.cue -- package example @@ -29,7 +69,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) @@ -47,8 +87,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 diff --git a/content/docs/howto/fetch-json-data-http/gen_cache.cue b/content/docs/howto/fetch-json-data-http/gen_cache.cue index 82f5eb65a..b01015c17 100644 --- a/content/docs/howto/fetch-json-data-http/gen_cache.cue +++ b/content/docs/howto/fetch-json-data-http/gen_cache.cue @@ -6,8 +6,58 @@ package site "fetch-json-data-http": { page: { cache: { - code: { - cc: "/fGkdvJxXOT35Y7SNskPvdKPhPv5VyWGtu35bO7HM0U=" + upload: { + "HIDDEN: server.go": "ot+k5tKSApepTLCiXn/n8eABxP1Nd1+jBShQsvdOUJE=" + cc: "Myu8x8nfORuqACXzx+RW+VccQ7BgUhkzR+FfCPvCdyA=" + } + multi_step: { + hash: "VK2LMI917O3H4OQSQ2P1TCJMPNS84L1D08GOP60HJPEV39L31KJG====" + scriptHash: "NGQRGV1351DRVFKLJOES3AVC47MHIDRNPSJUI64AI654PFK8PK4G====" + 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 + + """ + }] } } } diff --git a/content/docs/howto/fetch-json-data-http/page.cue b/content/docs/howto/fetch-json-data-http/page.cue index 44fd463bb..94a8c966d 100644 --- a/content/docs/howto/fetch-json-data-http/page.cue +++ b/content/docs/howto/fetch-json-data-http/page.cue @@ -1,3 +1,11 @@ package site -content: docs: howto: "fetch-json-data-http": page: _ +content: docs: howto: "fetch-json-data-http": page: { + vars: { + 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" + } + } +} diff --git a/hugo/content/en/docs/howto/fetch-json-data-http/index.md b/hugo/content/en/docs/howto/fetch-json-data-http/index.md index 87b032191..49808fd7b 100644 --- a/hugo/content/en/docs/howto/fetch-json-data-http/index.md +++ b/hugo/content/en/docs/howto/fetch-json-data-http/index.md @@ -15,7 +15,7 @@ which is then used by the workflow command. {{< 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 ( @@ -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