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 1 commit
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
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
3 changes: 3 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,9 @@ class Nextflow {

private static final Random random = new Random()

static String env(String name) {
return SysEnv.get(name)
}

static private fileNamePattern( FilePatternSplitter splitter, Map opts ) {

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
Loading