Skip to content

Commit fa0e8e0

Browse files
Add env() function (#5506)
Signed-off-by: Ben Sherman <[email protected]> Signed-off-by: Paolo Di Tommaso <[email protected]> Co-authored-by: Paolo Di Tommaso <[email protected]>
1 parent d1bbd3d commit fa0e8e0

File tree

7 files changed

+79
-7
lines changed

7 files changed

+79
-7
lines changed

docs/config.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ The following constants are globally available in a Nextflow configuration file:
113113
`projectDir`
114114
: The directory where the main script is located.
115115

116+
## Functions
117+
118+
The following functions are globally available in a Nextflow configuration file:
119+
120+
`env( name )`
121+
: :::{versionadded} 24.11.0-edge
122+
:::
123+
: Get the value of the environment variable with the specified name in the Nextflow launch environment.
124+
116125
(config-params)=
117126

118127
## Parameters

docs/reference/stdlib.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ The following functions are available in Nextflow scripts:
197197
`branchCriteria( closure )`
198198
: Create a branch criteria to use with the {ref}`operator-branch` operator.
199199

200+
`env( name )`
201+
: :::{versionadded} 24.11.0-edge
202+
:::
203+
: Get the value of the environment variable with the specified name in the Nextflow launch environment.
204+
200205
`error( message = null )`
201206
: Throw a script runtime error with an optional error message.
202207

docs/vscode.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@ The Nextflow language specification does not support implicit environment variab
275275
println "PWD = ${System.getenv('PWD')}"
276276
```
277277

278+
:::{versionadded} 24.11.0-edge
279+
The `env()` function can be used instead of `System.getenv()`:
280+
281+
```nextflow
282+
println "PWD = ${env('PWD')}"
283+
```
284+
:::
285+
278286
### Restricted syntax
279287

280288
The following patterns are still supported but have been restricted, i.e. some syntax variants have been removed.

modules/nextflow/src/main/groovy/nextflow/Nextflow.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ class Nextflow {
5757

5858
private static final Random random = new Random()
5959

60+
/**
61+
* Get the value of an environment variable from the launch environment.
62+
*
63+
* @param name
64+
* The environment variable name to be referenced
65+
* @return
66+
* The value associate with the specified variable name or {@code null} if the variable does not exist.
67+
*/
68+
static String env(String name) {
69+
return SysEnv.get(name)
70+
}
6071

6172
static private fileNamePattern( FilePatternSplitter splitter, Map opts ) {
6273

modules/nextflow/src/main/groovy/nextflow/config/ConfigBase.groovy

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
package nextflow.config
1818

19-
import ch.artecat.grengine.Grengine
20-
import groovy.transform.Memoized
21-
2219
import java.nio.file.NoSuchFileException
2320
import java.nio.file.Path
2421

22+
import ch.artecat.grengine.Grengine
23+
import groovy.transform.Memoized
24+
import nextflow.SysEnv
2525
import nextflow.exception.IllegalConfigException
2626
import nextflow.file.FileHelper
2727
import org.codehaus.groovy.control.CompilerConfiguration
@@ -74,6 +74,18 @@ abstract class ConfigBase extends Script {
7474
this.configStack = stack
7575
}
7676

77+
/**
78+
* Get the value of an environment variable from the launch environment.
79+
*
80+
* @param name
81+
* The environment variable name to be referenced
82+
* @return
83+
* The value associate with the specified variable name or {@code null} if the variable does not exist.
84+
*/
85+
String env(String name) {
86+
return SysEnv.get(name)
87+
}
88+
7789
/**
7890
* Implements the config file include
7991
*/

modules/nextflow/src/test/groovy/nextflow/NextflowTest.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ class NextflowTest extends Specification {
3535
System.getenv('CI_GROOVY_VERSION') == GroovySystem.getVersion()
3636
}
3737

38+
def 'should get an environment variable' () {
39+
given:
40+
SysEnv.push(FOO: 'FOO_VALUE')
41+
42+
expect:
43+
Nextflow.env('FOO') == 'FOO_VALUE'
44+
45+
cleanup:
46+
SysEnv.pop()
47+
}
48+
3849
def testFile() {
3950
expect:
4051
Nextflow.file('file.log').toFile() == new File('file.log').canonicalFile

modules/nextflow/src/test/groovy/nextflow/config/ConfigParserTest.groovy

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package nextflow.config
1818

19-
import spock.lang.Ignore
20-
2119
import java.nio.file.Files
2220
import java.nio.file.NoSuchFileException
2321
import java.nio.file.Path
@@ -26,18 +24,36 @@ import com.sun.net.httpserver.Headers
2624
import com.sun.net.httpserver.HttpExchange
2725
import com.sun.net.httpserver.HttpHandler
2826
import com.sun.net.httpserver.HttpServer
27+
import nextflow.SysEnv
2928
import nextflow.exception.ConfigParseException
30-
import spock.lang.Specification
31-
3229
import nextflow.util.Duration
3330
import nextflow.util.MemoryUnit
31+
import spock.lang.Ignore
32+
import spock.lang.Specification
3433

3534
/**
3635
*
3736
* @author Paolo Di Tommaso <[email protected]>
3837
*/
3938
class ConfigParserTest extends Specification {
4039

40+
def 'should get an environment variable' () {
41+
given:
42+
SysEnv.push(MAX_CPUS: '1')
43+
44+
when:
45+
def CONFIG = '''
46+
process.cpus = env('MAX_CPUS')
47+
'''
48+
def config = new ConfigParser().parse(CONFIG)
49+
50+
then:
51+
config.process.cpus == '1'
52+
53+
cleanup:
54+
SysEnv.pop()
55+
}
56+
4157
def 'should parse plugins id' () {
4258
given:
4359
def CONFIG = '''

0 commit comments

Comments
 (0)