From b94b8c0c7056631fb886a790c501c4bce58bfbaa Mon Sep 17 00:00:00 2001 From: jffarge Date: Tue, 19 Oct 2021 15:06:14 +0200 Subject: [PATCH 1/5] create http universe package Signed-off-by: jffarge --- docs/reference/README.md | 1 + docs/reference/http.md | 74 ++++++++++++++++++++++++++ stdlib/.dagger/env/http/.gitignore | 2 + stdlib/.dagger/env/http/values.yaml | 23 ++++++++ stdlib/http/http.cue | 81 +++++++++++++++++++++++++++++ stdlib/http/tests/http.cue | 42 +++++++++++++++ stdlib/universe.bats | 4 ++ 7 files changed, 227 insertions(+) create mode 100644 docs/reference/http.md create mode 100644 stdlib/.dagger/env/http/.gitignore create mode 100644 stdlib/.dagger/env/http/values.yaml create mode 100644 stdlib/http/http.cue create mode 100644 stdlib/http/tests/http.cue diff --git a/docs/reference/README.md b/docs/reference/README.md index 4a97ba0e..724fc122 100644 --- a/docs/reference/README.md +++ b/docs/reference/README.md @@ -26,6 +26,7 @@ - [gcp/secretmanager](./gcp/secretmanager.md) - Google Cloud Secret Manager - [git](./git.md) - Git operations - [go](./go.md) - Go build operations +- [http](./http.md) - - - [io](./io.md) - IO operations - [java/maven](./java/maven.md) - Maven is a build automation tool for Java - [js/yarn](./js/yarn.md) - Yarn is a package manager for Javascript applications diff --git a/docs/reference/http.md b/docs/reference/http.md new file mode 100644 index 00000000..7d2440ab --- /dev/null +++ b/docs/reference/http.md @@ -0,0 +1,74 @@ +--- +sidebar_label: http +--- + +# alpha.dagger.io/http + +```cue +import "alpha.dagger.io/http" +``` + +## http.#Delete + +### http.#Delete Inputs + +_No input._ + +### http.#Delete Outputs + +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*response.body* | `string` |- | +|*response.statusCode* | `_\|_` |- | + +## http.#Do + +### http.#Do Inputs + +_No input._ + +### http.#Do Outputs + +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*response.body* | `string` |- | +|*response.statusCode* | `_\|_` |- | + +## http.#Get + +### http.#Get Inputs + +_No input._ + +### http.#Get Outputs + +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*response.body* | `string` |- | +|*response.statusCode* | `_\|_` |- | + +## http.#Post + +### http.#Post Inputs + +_No input._ + +### http.#Post Outputs + +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*response.body* | `string` |- | +|*response.statusCode* | `_\|_` |- | + +## http.#Put + +### http.#Put Inputs + +_No input._ + +### http.#Put Outputs + +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*response.body* | `string` |- | +|*response.statusCode* | `_\|_` |- | diff --git a/stdlib/.dagger/env/http/.gitignore b/stdlib/.dagger/env/http/.gitignore new file mode 100644 index 00000000..01ec19b0 --- /dev/null +++ b/stdlib/.dagger/env/http/.gitignore @@ -0,0 +1,2 @@ +# dagger state +state/** diff --git a/stdlib/.dagger/env/http/values.yaml b/stdlib/.dagger/env/http/values.yaml new file mode 100644 index 00000000..76b6c328 --- /dev/null +++ b/stdlib/.dagger/env/http/values.yaml @@ -0,0 +1,23 @@ +plan: + package: ./http/tests +name: http +sops: + kms: [] + gcp_kms: [] + azure_kv: [] + hc_vault: [] + age: + - recipient: age1gxwmtwahzwdmrskhf90ppwlnze30lgpm056kuesrxzeuyclrwvpsupwtpk + enc: | + -----BEGIN AGE ENCRYPTED FILE----- + YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBhYjNpR1NHc3V4aGxGNzQ1 + YlZuQ0hrZHBDWGw0elJWZWU3c0U4KzVodEhzCkowME9JVlpWVEc1L2dHUE80Q29t + bEpKTzRwYmhyc2N2a3dhaWg4aWZVWTQKLS0tIHNPWDVaQmh6SllzK2V3QjNJSWVH + NHkxc1VXWHBwZFN5WFNxamdPaGZLM3MKaZra2gWvKl7+pXSihV70/mQYel9697z2 + eBSMuRPuZSjrg3JCBLhXTdIgUYxbMgWhILLpN5UqhTBVboUlRsURQA== + -----END AGE ENCRYPTED FILE----- + lastmodified: "2021-10-19T11:50:42Z" + mac: ENC[AES256_GCM,data:iG/JWnCk8UpThscQBqd9l7R7k1Xae1ED7WbYqPQmK5GRXhb7hIZGENnjn1aARnFjlCtEob6WvutWaZe7q1P90ZCirwXVBGewAeHchHqFA0KmUORRdtjMkZjnlRLMDKYu6CeNkaVK1al+B87CPlCBKz5UMeL9gnvug0k298AxWLY=,iv:RZEtSdbkxwgq5xEucnC+qyD2RCMk1DuaShjCh5Ihe5M=,tag:swW4R7ni0zoxOrc9lrV9Ww==,type:str] + pgp: [] + encrypted_suffix: secret + version: 3.7.1 diff --git a/stdlib/http/http.cue b/stdlib/http/http.cue new file mode 100644 index 00000000..fb1949a5 --- /dev/null +++ b/stdlib/http/http.cue @@ -0,0 +1,81 @@ +package http + +import ( + "encoding/json" + "strconv" + + "alpha.dagger.io/alpine" + "alpha.dagger.io/dagger" + "alpha.dagger.io/os" +) + +#Get: #Do & {method: "GET"} +#Post: #Do & {method: "POST"} +#Put: #Do & {method: "PUT"} +#Delete: #Do & {method: "DELETE"} +#Do: { + url: string + method: "GET" | "POST" | "PUT" | "DELETE" | "PATH" | "HEAD" + + request: { + body: string | *"" + header: [string]: string | [...string] + token: dagger.#Secret | *null + } + + ctr: os.#Container & { + image: alpine.#Image & { + package: curl: true + package: bash: "=5.1.0-r0" + package: jq: "~=1.6" + } + shell: path: "/bin/bash" + always: true + + env: { + METHOD: method + HEADERS: json.Marshal(request.header) + BODY: request.body + URL: url + } + if request.token != null { + secret: "/token": request.token + } + command: #""" + curlArgs=( + "$URL" + -L --fail --silent --show-error + --write-out "%{http_code}" + -X "$METHOD" + -d "$BODY" + -o /response + ) + + headers="$(echo $HEADERS | jq -r 'to_entries | map(.key + ": " + (.value | tostring) + "\n") | add')" + while read h; do + curlArgs+=("-H" "$h") + done <<< "$headers" + + if [ -e /token ]; then + curlArgs+=("-H" "Authorization: bearer $(cat /token)") + fi + + curl "${curlArgs[@]}" > /status + """# + } + + response: { + body: { + os.#File & { + from: ctr + path: "/response" + } + }.contents @dagger(output) + statusCode: strconv.Atoi({ + os.#File & { + from: ctr + path: "/status" + } + }.contents) @dagger(output) + } +} diff --git a/stdlib/http/tests/http.cue b/stdlib/http/tests/http.cue new file mode 100644 index 00000000..feea0ce4 --- /dev/null +++ b/stdlib/http/tests/http.cue @@ -0,0 +1,42 @@ +package http + +import ( + "encoding/json" + + "alpha.dagger.io/alpine" + "alpha.dagger.io/os" +) + +TestRequest: { + req: #Get & { + url: "https://api.github.com/" + request: header: { + Accept: "application/json" + Test: ["A", "B"] + } + } + + testRaw: os.#Container & { + image: alpine.#Image & { + package: jq: "~=1.6" + } + env: STATUS: "\(req.response.statusCode)" + files: "/content.json": { + content: req.response.body + mode: 0o500 + } + command: #""" + test "$STATUS" = 200 + test "$(cat /content.json | jq -r .current_user_url)" = "https://api.github.com/user" + """# + } + + testJSON: os.#Container & { + env: STATUS: "\(req.response.statusCode)" + env: CONTENT: json.Unmarshal(req.response.body).current_user_url + command: """ + test "$STATUS" = 200 + test "$CONTENT" = "https://api.github.com/user" + """ + } +} diff --git a/stdlib/universe.bats b/stdlib/universe.bats index 4a42172b..330527e3 100644 --- a/stdlib/universe.bats +++ b/stdlib/universe.bats @@ -23,6 +23,10 @@ setup() { dagger -e go up } +@test "http" { + dagger -e http up +} + @test "js/yarn" { dagger -e js-yarn up } From 04781f4e07e076d1aa3648e03a451d78db404728 Mon Sep 17 00:00:00 2001 From: jffarge Date: Fri, 22 Oct 2021 16:13:55 +0200 Subject: [PATCH 2/5] add more args for http container test Signed-off-by: jffarge --- stdlib/http/http.cue | 13 +++++++------ stdlib/http/tests/http.cue | 27 ++++++++++++--------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/stdlib/http/http.cue b/stdlib/http/http.cue index fb1949a5..48672474 100644 --- a/stdlib/http/http.cue +++ b/stdlib/http/http.cue @@ -2,7 +2,7 @@ package http import ( "encoding/json" - "strconv" + // "strconv" "alpha.dagger.io/alpine" "alpha.dagger.io/dagger" @@ -32,6 +32,7 @@ import ( shell: path: "/bin/bash" always: true + env: { METHOD: method HEADERS: json.Marshal(request.header) @@ -47,15 +48,15 @@ import ( -L --fail --silent --show-error --write-out "%{http_code}" -X "$METHOD" - -d "$BODY" -o /response ) + [ -n "$BODY" ] && curlArgs+=("-d" "'$BODY'") + headers="$(echo $HEADERS | jq -r 'to_entries | map(.key + ": " + (.value | tostring) + "\n") | add')" while read h; do - curlArgs+=("-H" "$h") + curlArgs+=("-H" "'$h'") done <<< "$headers" - if [ -e /token ]; then curlArgs+=("-H" "Authorization: bearer $(cat /token)") fi @@ -71,11 +72,11 @@ import ( path: "/response" } }.contents @dagger(output) - statusCode: strconv.Atoi({ + statusCode: { os.#File & { from: ctr path: "/status" } - }.contents) @dagger(output) + }.contents @dagger(output) } } diff --git a/stdlib/http/tests/http.cue b/stdlib/http/tests/http.cue index feea0ce4..a4506ef4 100644 --- a/stdlib/http/tests/http.cue +++ b/stdlib/http/tests/http.cue @@ -1,7 +1,8 @@ package http import ( - "encoding/json" + // "encoding/json" + // "strconv" "alpha.dagger.io/alpine" "alpha.dagger.io/os" @@ -12,31 +13,27 @@ TestRequest: { url: "https://api.github.com/" request: header: { Accept: "application/json" - Test: ["A", "B"] } } testRaw: os.#Container & { image: alpine.#Image & { package: jq: "~=1.6" + package: bash: true } env: STATUS: "\(req.response.statusCode)" files: "/content.json": { content: req.response.body mode: 0o500 } - command: #""" - test "$STATUS" = 200 - test "$(cat /content.json | jq -r .current_user_url)" = "https://api.github.com/user" - """# - } - - testJSON: os.#Container & { - env: STATUS: "\(req.response.statusCode)" - env: CONTENT: json.Unmarshal(req.response.body).current_user_url - command: """ - test "$STATUS" = 200 - test "$CONTENT" = "https://api.github.com/user" - """ + shell: { + args: ["--noprofile", "--norc", "-eo", "pipefail", "-c"] + } + command: #Command } } + +#Command: #""" + test "$(cat /content.json | jq -r .current_user_url)" = 'https://api.github.com/user' + test "$STATUS" = "200" + """# From 0e3ca30189ccee9b1a6464aabaef6d3c21455b20 Mon Sep 17 00:00:00 2001 From: jffarge Date: Fri, 22 Oct 2021 16:18:15 +0200 Subject: [PATCH 3/5] remove comments Signed-off-by: jffarge --- stdlib/http/http.cue | 1 - stdlib/http/tests/http.cue | 3 --- 2 files changed, 4 deletions(-) diff --git a/stdlib/http/http.cue b/stdlib/http/http.cue index 48672474..c1d2f538 100644 --- a/stdlib/http/http.cue +++ b/stdlib/http/http.cue @@ -2,7 +2,6 @@ package http import ( "encoding/json" - // "strconv" "alpha.dagger.io/alpine" "alpha.dagger.io/dagger" diff --git a/stdlib/http/tests/http.cue b/stdlib/http/tests/http.cue index a4506ef4..1a25a6c4 100644 --- a/stdlib/http/tests/http.cue +++ b/stdlib/http/tests/http.cue @@ -1,9 +1,6 @@ package http import ( - // "encoding/json" - // "strconv" - "alpha.dagger.io/alpine" "alpha.dagger.io/os" ) From 40f132b77b767604e8b8b875b6b32103c8c3930e Mon Sep 17 00:00:00 2001 From: jffarge Date: Fri, 22 Oct 2021 16:48:30 +0200 Subject: [PATCH 4/5] Execute os file before Atoi execution Signed-off-by: jffarge --- stdlib/http/http.cue | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/stdlib/http/http.cue b/stdlib/http/http.cue index c1d2f538..44e495db 100644 --- a/stdlib/http/http.cue +++ b/stdlib/http/http.cue @@ -2,6 +2,7 @@ package http import ( "encoding/json" + "strconv" "alpha.dagger.io/alpine" "alpha.dagger.io/dagger" @@ -64,18 +65,23 @@ import ( """# } + statusCode: { + os.#File & { + from: ctr + path: "/status" + } + }.contents @dagger(output) + + body: { + os.#File & { + from: ctr + path: "/response" + } + }.contents @dagger(output) + + // Force os.#File exec before Atoi response: { - body: { - os.#File & { - from: ctr - path: "/response" - } - }.contents @dagger(output) - statusCode: { - os.#File & { - from: ctr - path: "/status" - } - }.contents @dagger(output) + "body": body + "statusCode": strconv.Atoi(statusCode) } } From 8f6b3e95a8deeae7264d1887bfcaca5932216098 Mon Sep 17 00:00:00 2001 From: guillaume Date: Thu, 28 Oct 2021 17:20:25 +0200 Subject: [PATCH 5/5] Fix lint issues Signed-off-by: guillaume --- docs/reference/http.md | 40 +++++++++++++++++++------------------- stdlib/http/http.cue | 7 +++---- stdlib/http/tests/http.cue | 18 +++++++---------- stdlib/universe.bats | 2 +- 4 files changed, 31 insertions(+), 36 deletions(-) diff --git a/docs/reference/http.md b/docs/reference/http.md index 7d2440ab..71d6e0b9 100644 --- a/docs/reference/http.md +++ b/docs/reference/http.md @@ -16,10 +16,10 @@ _No input._ ### http.#Delete Outputs -| Name | Type | Description | -| ------------- |:-------------: |:-------------: | -|*response.body* | `string` |- | -|*response.statusCode* | `_\|_` |- | +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*statusCode* | `string` |- | +|*body* | `string` |- | ## http.#Do @@ -29,10 +29,10 @@ _No input._ ### http.#Do Outputs -| Name | Type | Description | -| ------------- |:-------------: |:-------------: | -|*response.body* | `string` |- | -|*response.statusCode* | `_\|_` |- | +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*statusCode* | `string` |- | +|*body* | `string` |- | ## http.#Get @@ -42,10 +42,10 @@ _No input._ ### http.#Get Outputs -| Name | Type | Description | -| ------------- |:-------------: |:-------------: | -|*response.body* | `string` |- | -|*response.statusCode* | `_\|_` |- | +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*statusCode* | `string` |- | +|*body* | `string` |- | ## http.#Post @@ -55,10 +55,10 @@ _No input._ ### http.#Post Outputs -| Name | Type | Description | -| ------------- |:-------------: |:-------------: | -|*response.body* | `string` |- | -|*response.statusCode* | `_\|_` |- | +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*statusCode* | `string` |- | +|*body* | `string` |- | ## http.#Put @@ -68,7 +68,7 @@ _No input._ ### http.#Put Outputs -| Name | Type | Description | -| ------------- |:-------------: |:-------------: | -|*response.body* | `string` |- | -|*response.statusCode* | `_\|_` |- | +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*statusCode* | `string` |- | +|*body* | `string` |- | diff --git a/stdlib/http/http.cue b/stdlib/http/http.cue index 44e495db..d5b99145 100644 --- a/stdlib/http/http.cue +++ b/stdlib/http/http.cue @@ -32,7 +32,6 @@ import ( shell: path: "/bin/bash" always: true - env: { METHOD: method HEADERS: json.Marshal(request.header) @@ -77,11 +76,11 @@ import ( from: ctr path: "/response" } - }.contents @dagger(output) + }.contents @dagger(output) - // Force os.#File exec before Atoi + // Force os.#File exec before Atoi response: { - "body": body + "body": body "statusCode": strconv.Atoi(statusCode) } } diff --git a/stdlib/http/tests/http.cue b/stdlib/http/tests/http.cue index 1a25a6c4..ed7b6945 100644 --- a/stdlib/http/tests/http.cue +++ b/stdlib/http/tests/http.cue @@ -8,29 +8,25 @@ import ( TestRequest: { req: #Get & { url: "https://api.github.com/" - request: header: { - Accept: "application/json" - } + request: header: Accept: "application/json" } testRaw: os.#Container & { image: alpine.#Image & { - package: jq: "~=1.6" - package: bash: true + package: jq: "~=1.6" + package: bash: true } env: STATUS: "\(req.response.statusCode)" files: "/content.json": { content: req.response.body mode: 0o500 } - shell: { - args: ["--noprofile", "--norc", "-eo", "pipefail", "-c"] - } + shell: args: ["--noprofile", "--norc", "-eo", "pipefail", "-c"] command: #Command } } #Command: #""" - test "$(cat /content.json | jq -r .current_user_url)" = 'https://api.github.com/user' - test "$STATUS" = "200" - """# + test "$(cat /content.json | jq -r .current_user_url)" = 'https://api.github.com/user' + test "$STATUS" = "200" + """# diff --git a/stdlib/universe.bats b/stdlib/universe.bats index 330527e3..dde83a32 100644 --- a/stdlib/universe.bats +++ b/stdlib/universe.bats @@ -24,7 +24,7 @@ setup() { } @test "http" { - dagger -e http up + dagger -e http up } @test "js/yarn" {