Skip to content

Commit d69c804

Browse files
authored
Add dotenv load command (#15)
1 parent 0c42335 commit d69c804

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

commands/dotenv/load.cfc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Load all the values from a properties file into the shell's current environment context.
3+
* .
4+
* {code:bash}
5+
* dotenv load config/common.properties
6+
* {code}
7+
* .
8+
* You can load more than one file at a time using file globbing patterns
9+
* .
10+
* {code:bash}
11+
* dotenv load config/*.properties
12+
* {code}
13+
*/
14+
component accessors="true" {
15+
property name="envFileService" inject="EnvironmentFileService@commandbox-dotenv";
16+
17+
/**
18+
* @envFile File or globbing pattern of file(s) to load.
19+
*/
20+
function run( required Globber envFile ) {
21+
envFile.apply( ( file ) => {
22+
envFileService.loadEnvToCLI( envStruct=envFileService.getEnvStruct( file ), inParent=true );
23+
print.greenLine( "Env file [#file#] loaded." );
24+
} );
25+
if( !envFile.count() ) {
26+
print.yellowLine( "No Env files were found matching [#envFile.getPattern()#]." );
27+
}
28+
}
29+
30+
}

models/EnvironmentFileService.cfc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@ component singleton="true" {
2424
.getAsStruct();
2525
}
2626

27-
public function loadEnvToCLI( required struct envStruct ) {
27+
/**
28+
* @envStruct Struct of key/value pairs to load
29+
* @inParent Loads vars into the parent context so they persist outside of this current command
30+
*/
31+
public function loadEnvToCLI( required struct envStruct, boolean inParent=false ) {
2832

2933
for (var key in envStruct) {
3034

3135
// Shim for older versions of CommandBox
3236
if( !structKeyExists( systemSettings, 'setSystemSetting' ) ) {
3337
javaSystem.setProperty( key, envStruct[ key ] );
3438
} else {
35-
systemSettings.setSystemSetting( key, envStruct[ key ] );
39+
systemSettings.setSystemSetting( key, envStruct[ key ], inParent );
3640
}
3741

3842
if( moduleSettings.printOnLoad && moduleSettings.verbose ) {

0 commit comments

Comments
 (0)