sagiegurari / cargo-make

Rust task runner and build tool.
https://sagiegurari.github.io/cargo-make/
Apache License 2.0
2.6k stars 126 forks source link

Support optional env_files #582

Closed starkat99 closed 3 years ago

starkat99 commented 3 years ago

Feature Description

Currently, it's an error if an env_files path doesn't exist. I'd like to be able to ignore that error when the file doesn't exist and continue as normal

Describe The Solution You'd Like

Add an optional flag to env_files config that when true will ignore the file if the file doesn't exist.

Code Sample

env_files = [ { path = "env.env", optional = true } ]
sagiegurari commented 3 years ago

how about something like this (didn't test)?

env_scripts = [
'''
#!@duckscript
${file} = set ./myfile.env
file_exists = is_file ${file}
if ${file_exists}
  text = readfile ${file}
  lines = split ${text} \n
  for line in ${lines}
    parts = split ${line} =
    count = array_length ${parts}

    if eq ${count} 2
      value = array_pop ${parts}
      key = array_pop ${parts}

      # just in case
      key = trim ${key}
      value = trim ${value}

      set_env ${key} ${value}
    end

    release ${parts}
  end
end
'''
]
sagiegurari commented 3 years ago

I'm releasing a new duckscript version that would make it even simpler:

# load env file
env_scripts = [
'''
#!@duckscript
text = readfile ./myfile.env
handle = map
map_load_properties ${handle} ${text}
set_env --handle ${handle}
'''
]
sagiegurari commented 3 years ago

closing due to lack of feedback and since there is a simple solution for users.

eirenik0 commented 2 years ago

Hey @sagiegurari! Actually, the solution that you've provided is good, but not the best. I've faced that CI doesn't want to work with script, while locally it works. So optional = true would be a nice option here.

sagiegurari commented 2 years ago

what donyou mean doesn't work with script? duckscript runtime is embedded inside cargo-make.

AlexAegis commented 6 months ago

Hey @sagiegurari, just migrated all my projects to cargo-make, super useful, love it so far!

I was trying to do something like this and didn't find anything related to this in the readme. When I search for the word "optional" I came across another feature that had a flag like this so I just instinctively tried it with env_files too, it didn't work so I searched the github issues if this was already requested or not.

Now I see that there's a workaround but it's not optimal because it's not documented and it's very hard to discover.

And neither workaround scripts worked for me regardless if the file exists or not:

env_files = [".env", '''
#!@duckscript
text = readfile .env.local
handle = map
map_load_properties ${handle} ${text}
set_env --handle ${handle}
''']

output:

[cargo-make] INFO - cargo make 0.37.12
[cargo-make] INFO - Calling cargo metadata to extract project info
[cargo-make] INFO - Cargo metadata done
[cargo-make] ERROR - Unable to load env file: #!@duckscript
text = readfile ./.env.local
handle = map
map_load_properties ${handle} ${text}
set_env --handle ${handle}
 Error: ReadFile(
    "Unable to read file.",
    IOError(
        "Unable to read file: \"#!@duckscript\\ntext = readfile ./.env.local\\nhandle = map\\nmap_load_properties ${handle} ${text}\\nset_env --handle ${handle}\\n\"",
        Some(
            Os {
                code: 2,
                kind: NotFound,
                message: "No such file or directory",
            },
        ),
    ),
)
[cargo-make] WARN - Build Failed.

Not sure what I'm doing wrong

sagiegurari commented 6 months ago

@AlexAegis its really hard to follow closed issues

you put duckscript in env_files instead of env_scripts. look at this example: https://github.com/sagiegurari/cargo-make/blob/master/examples/env.toml#L8 in addition you have is_file command that you can condition to make sure file exists before loading