This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
dagger/pkg/alpha.dagger.io/random/string.cue
Andrea Luzzardi 282759c0e5 cue modules: move stdlib to pkg/alpha.dagger.io
In preparation for Europa, we will vendor multiple CUE modules:

- `pkg/alpha.dagger.io`: legacy non-europa packages
- `pkg/dagger.io`: core Europa packages
- `pkg/universe.dagger.io`: Europa universe

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
2022-01-11 13:16:37 -08:00

62 lines
1.1 KiB
CUE

// Random generation utilities
package random
import (
"strconv"
"alpha.dagger.io/alpine"
"alpha.dagger.io/dagger/op"
)
// Generate a random string
#String: {
// Seed of the random string to generate.
// When using the same `seed`, the same random string will be generated
// because of caching.
// FIXME: this is necessary because of https://github.com/dagger/dagger/issues/591
seed: string @dagger(input)
// length of the string
length: *12 | number @dagger(input)
// generated random string
out: {
string
#up: [
op.#Load & {from: alpine.#Image & {
package: python3: true
}},
op.#WriteFile & {
dest: "/entrypoint.py"
content: #"""
import random
import string
import os
length = int(os.environ['LENGTH'])
letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(length)) )
"""#
},
op.#Exec & {
args: ["sh", "-c", #"""
printf "$(python3 /entrypoint.py)" > /rand
"""#,
]
env: LENGTH: strconv.FormatInt(length, 10)
env: SEED: seed
always: true
},
op.#Export & {
source: "/rand"
},
]
} @dagger(output)
}