can I use dotenv in a task runner? i have a bash s...
# box-products
t
can I use dotenv in a task runner? i have a bash script calling the task.cfc in a subdirectory, that subdirectory has a .env file which I would like to read, I can load it in bash before I call the task but I would prefer that it remained contained
s
Yes
Here's from one of our task runners, purloined I think directly from Brad:
Copy code
/**
	* Load up common and local env files
	*/
	function loadEnvVars() {
		// Load common config from any xxx.env files in the config dir
		if ( fileExists( './config/#getSystemSetting( 'ENVIRONMENT' )#.env' ) ) {
			command( 'dotenv load' )
				.params( resolvepath( './config/#getSystemSetting( 'ENVIRONMENT' )#.env' ) )
				.run();
		}
		else {
			print.line( 'No environment file found matching ./config/' & getSystemSetting( 'ENVIRONMENT' ) & '.env' );
		}

		// Re-load any .env file so it can override the common settings above
		if ( fileExists( resolvepath( '../.env' ) ) ) {
			command( 'dotenv load' )
				.params( resolvepath( '../.env' ) )
				.run( returnOutput = true );
		}
	}
t
👍 Perfect I was just looking at command
b
@thisOldDave just to clarify, is the
.env
file in the SAME folder as your
task.cfc
?
calling the task.cfc in a subdirectory, that subdirectory has a .env file
This sounds like that's a "yes"
And if so, you don't need to do anything! It will all "just work" with no effort from you.
Any box command you run in that folder, (including
task run
) will automatically find and load any
.env
file prior to execution.
You'd only need to use the
dotenv load
command if you have other property files in other folders or with other names that you also want loaded.
This also depends on how you're calling it, which you haven't shown us yet. if you're just calling
Copy code
cd subdir/
box task run
in your shell script, then you should be good
BUT, if you're running
Copy code
box task run taskFile=subdir/task.cfc
THEN dotenv is going to look in the working directory that the command was run, not the directory that the task happened to live in elsewhere on the hard drive.
t
it was the later but I can change that if it removes redundant code