Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add env() function #5506

Merged
merged 6 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ The following constants are globally available in a Nextflow configuration file:
`projectDir`
: The directory where the main script is located.

## Functions

The following functions are globally available in a Nextflow configuration file:

`env( name )`
: :::{versionadded} 24.11.0-edge
:::
: Get the value of an environment variable in the launch environment.

(config-params)=

## Parameters
Expand Down
5 changes: 5 additions & 0 deletions docs/reference/stdlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ The following functions are available in Nextflow scripts:
`branchCriteria( closure )`
: Create a branch criteria to use with the {ref}`operator-branch` operator.

`env( name )`
bentsherman marked this conversation as resolved.
Show resolved Hide resolved
: :::{versionadded} 24.11.0-edge
:::
: Get the value of an environment variable in the launch environment.

`error( message = null )`
: Throw a script runtime error with an optional error message.

Expand Down
8 changes: 8 additions & 0 deletions docs/vscode.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ The Nextflow language specification does not support implicit environment variab
println "PWD = ${System.getenv('PWD')}"
```

:::{versionadded} 24.11.0-edge
The `env()` function can be used instead of `System.getenv()`:

```nextflow
println "PWD = ${env('PWD')}"
```
:::

### Restricted syntax

The following patterns are still supported but have been restricted, i.e. some syntax variants have been removed.
Expand Down
8 changes: 8 additions & 0 deletions modules/nextflow/src/main/groovy/nextflow/Nextflow.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class Nextflow {

private static final Random random = new Random()

/**
* Get the value of an environment variable from the launch environment.
*
* @param name
pditommaso marked this conversation as resolved.
Show resolved Hide resolved
*/
static String env(String name) {
return SysEnv.get(name)
}

static private fileNamePattern( FilePatternSplitter splitter, Map opts ) {

Expand Down
15 changes: 12 additions & 3 deletions modules/nextflow/src/main/groovy/nextflow/config/ConfigBase.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package nextflow.config

import ch.artecat.grengine.Grengine
import groovy.transform.Memoized

import java.nio.file.NoSuchFileException
import java.nio.file.Path

import ch.artecat.grengine.Grengine
import groovy.transform.Memoized
import nextflow.SysEnv
import nextflow.exception.IllegalConfigException
import nextflow.file.FileHelper
import org.codehaus.groovy.control.CompilerConfiguration
Expand Down Expand Up @@ -74,6 +74,15 @@ abstract class ConfigBase extends Script {
this.configStack = stack
}

/**
* Get the value of an environment variable from the launch environment.
*
* @param name
*/
String env(String name) {
return SysEnv.get(name)
}

/**
* Implements the config file include
*/
Expand Down
11 changes: 11 additions & 0 deletions modules/nextflow/src/test/groovy/nextflow/NextflowTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ class NextflowTest extends Specification {
System.getenv('CI_GROOVY_VERSION') == GroovySystem.getVersion()
}

def 'should get an environment variable' () {
given:
SysEnv.push(FOO: 'FOO_VALUE')

expect:
Nextflow.env('FOO') == 'FOO_VALUE'

cleanup:
SysEnv.pop()
}

def testFile() {
expect:
Nextflow.file('file.log').toFile() == new File('file.log').canonicalFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package nextflow.config

import spock.lang.Ignore

import java.nio.file.Files
import java.nio.file.NoSuchFileException
import java.nio.file.Path
Expand All @@ -26,18 +24,36 @@ import com.sun.net.httpserver.Headers
import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler
import com.sun.net.httpserver.HttpServer
import nextflow.SysEnv
import nextflow.exception.ConfigParseException
import spock.lang.Specification

import nextflow.util.Duration
import nextflow.util.MemoryUnit
import spock.lang.Ignore
import spock.lang.Specification

/**
*
* @author Paolo Di Tommaso <[email protected]>
*/
class ConfigParserTest extends Specification {

def 'should get an environment variable' () {
given:
SysEnv.push(MAX_CPUS: '1')

when:
def CONFIG = '''
process.cpus = env('MAX_CPUS')
'''
def config = new ConfigParser().parse(CONFIG)

then:
config.process.cpus == '1'

cleanup:
SysEnv.pop()
}

def 'should parse plugins id' () {
given:
def CONFIG = '''
Expand Down
Loading