File tree Expand file tree Collapse file tree 7 files changed +79
-7
lines changed Expand file tree Collapse file tree 7 files changed +79
-7
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -275,6 +275,14 @@ The Nextflow language specification does not support implicit environment variab
275275println "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
280288The following patterns are still supported but have been restricted, i.e. some syntax variants have been removed.
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1616
1717package nextflow.config
1818
19- import ch.artecat.grengine.Grengine
20- import groovy.transform.Memoized
21-
2219import java.nio.file.NoSuchFileException
2320import java.nio.file.Path
2421
22+ import ch.artecat.grengine.Grengine
23+ import groovy.transform.Memoized
24+ import nextflow.SysEnv
2525import nextflow.exception.IllegalConfigException
2626import nextflow.file.FileHelper
2727import 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 */
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1616
1717package nextflow.config
1818
19- import spock.lang.Ignore
20-
2119import java.nio.file.Files
2220import java.nio.file.NoSuchFileException
2321import java.nio.file.Path
@@ -26,18 +24,36 @@ import com.sun.net.httpserver.Headers
2624import com.sun.net.httpserver.HttpExchange
2725import com.sun.net.httpserver.HttpHandler
2826import com.sun.net.httpserver.HttpServer
27+ import nextflow.SysEnv
2928import nextflow.exception.ConfigParseException
30- import spock.lang.Specification
31-
3229import nextflow.util.Duration
3330import nextflow.util.MemoryUnit
31+ import spock.lang.Ignore
32+ import spock.lang.Specification
3433
3534/**
3635 *
3736 * @author Paolo Di Tommaso <[email protected] > 3837 */
3938class 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 = '''
You can’t perform that action at this time.
0 commit comments