Coder Social home page Coder Social logo

Question: --input and environment about dagger HOT 5 CLOSED

dagger avatar dagger commented on May 25, 2024
Question: --input and environment

from dagger.

Comments (5)

dubo-dubon-duponey avatar dubo-dubon-duponey commented on May 25, 2024

By the way, my wrapper script above will fail if none of the environment variables are defined (kind of proving my point that it is challenging to get it right, even for jedi-master-extraordinaire-shell-hackers like me :D :D :D)

from dagger.

dubo-dubon-duponey avatar dubo-dubon-duponey commented on May 25, 2024

Note that none of the above works if "myenv" properties have default values (see #51 )

from dagger.

dubo-dubon-duponey avatar dubo-dubon-duponey commented on May 25, 2024

Ok, this is the complete form of what you have to do right now to get environment nicely merged into a cue struct that has default values (accounting for #51)

Cue:

env: {
  inject: {
    foo: string | *"default foo \"\n"
    bar: string | *""
  }
}

Script:

readonly root="$(cd "$(dirname "${BASH_SOURCE[0]:-$PWD}")" 2>/dev/null 1>&2 && pwd)"
readonly cuepack="$root"/dagger

# Prepare arguments
args=(compute "$cuepack")

# Extract properties from cue under the "env" struct, and iterate
readonly CUE_ENV_KEY="env"
while read -r key; do
  # If there is a corresponding environment variable by that name that is defined
  if [ "${!key+x}" ]; then
    # Evaluate and escape it
    args+=(--input "$(printf "%s" "$CUE_ENV_KEY" | tr "." ":"): resolve: $key: \"$(printf "%s" "${!key}" | sed -E 's/"/\\"/')\"")
  else
    # If not there, grab the resolved default from cue
    override="$(cd "$cuepack"; cue eval --expression "env.inject.$key" --out json)"
    args+=(--input "$(printf "%s" "$CUE_ENV_KEY" | tr "." ":"): resolve: $key: $override")
  fi
done < <(cd "$cuepack"; cue eval --expression "${CUE_ENV_KEY}.inject" --out json | jq -rc 'keys | .[]')

# Add remaining command-line arguments
args+=("$@")

dagger "${args[@]}"

Now, you have what you want under "env: resolve:"

Not the friendliest thing ever :p

Alternatively, in the spirit of doing everything inside dagger, one could just --input "env: \"$(env | base64)\"" on the shell script side and do all the donkey dancing inside the cue file with an exec.

UPDATE: actually, this does not seem to work - because input is handled too late, you cannot use any of env.resolve in your script...

from dagger.

shykes avatar shykes commented on May 25, 2024

I think it’s reasonable for client scripts to have an easy way to pass a pre-existing key-value map without having to enumerate each key in the command-line args passed to dagger. Did I summarize the requirement correctly?

I think the cleanest way to do this would be with an env file. For example dagger compute —input-envfile TARGET=PATH could parse a standard envfile into a cue map.

For example:

$ cat prod.env
PROD=1
SSL=ENABLED
ANALYTICS_KEY=BLABLABLA
$ dagger compute . —input-envfile api.env=./prod.env —input-string api.env.USER=dubondubonney

In this example, the resulting input would be:

api: env: {
  PROD: “1”
  SSL: “ENABLED”
  ANALYTICS_KEY: “BLABLABLA
  USER: “dubondubonney”
}

The best part is you can source the env file straight from your shell script to avoid duplication.

I like this approach because it allows us to not deal with environment variables at all. In my experience once you make environment variables a part of your API and UI, the user experience is made worse by the inevitable appearance of side effects which are hard to detect, painful to reproduce, and impossible to prevent.

from dagger.

shykes avatar shykes commented on May 25, 2024

Closing this since it’s not directly actionable. Happy to resume the conversation in a github discussion if needed.

from dagger.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.