Refactor random generation into a definition to make it reusable

Signed-off-by: Tom Chauveau <tom.chauveau@epitech.eu>
This commit is contained in:
Tom Chauveau
2021-06-07 23:23:16 +02:00
parent e3ee7f2568
commit fdb91a7332
20 changed files with 288 additions and 216 deletions

View File

@@ -9,8 +9,10 @@ import (
TestConfig: gcpConfig: gcp.#Config
TestGCR: {
random: #Random & {}
repository: "gcr.io/dagger-ci/test"
tag: "test-gcr-\(random)"
tag: "test-gcr-\(random.out)"
creds: gcr.#Credentials & {
config: TestConfig.gcpConfig
@@ -23,7 +25,7 @@ TestGCR: {
op.#DockerBuild & {
dockerfile: """
FROM alpine
RUN echo \(random) > /test
RUN echo \(random.out) > /test
"""
},
@@ -59,7 +61,7 @@ TestGCR: {
op.#Exec & {
always: true
args: [
"sh", "-c", "test $(cat test) = \(random)",
"sh", "-c", "test $(cat test) = \(random.out)",
]
},
]
@@ -74,7 +76,7 @@ TestGCR: {
op.#DockerBuild & {
dockerfile: #"""
FROM \#(push.ref)
RUN test $(cat test) = \#(random)
RUN test $(cat test) = \#(random.out)
"""#
},
]

View File

@@ -1,20 +1,33 @@
package gcr
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
// Generate a random number
random: {
string
#up: [
op.#Load & {from: alpine.#Image},
op.#Exec & {
args: ["sh", "-c", "cat /dev/urandom | tr -dc 'a-z' | fold -w 10 | head -n 1 | tr -d '\n' > /rand"]
},
op.#Export & {
source: "/rand"
},
]
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image},
op.#Exec & {
always: true
args: ["sh", "-c", #"""
tr -cd '[:alpha:]' < /dev/urandom | fold -w "$SIZE" | head -n 1 | tr '[A-Z]' '[a-z]' | tr -d '\n' > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}