Module: Dotenv::Substitutions::Command

Defined in:
lib/dotenv/substitutions/command.rb

Overview

Substitute shell commands in a value.

SHA=$(git rev-parse HEAD)

Constant Summary collapse

INTERPOLATED_SHELL_COMMAND =
/
  (?<backslash>\\)?   # is it escaped with a backslash?
  \$                  # literal $
  (?<cmd>             # collect command content for eval
    \(                # require opening paren
    ([^()]|\g<cmd>)+  # allow any number of non-parens, or balanced
                      # parens (by nesting the <cmd> expression
                      # recursively)
    \)                # require closing paren
  )
/x

Class Method Summary collapse

Class Method Details

.call(value, _env, _is_load) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dotenv/substitutions/command.rb', line 23

def call(value, _env, _is_load)
  # Process interpolated shell commands
  value.gsub(INTERPOLATED_SHELL_COMMAND) do |*|
    # Eliminate opening and closing parentheses
    command = $LAST_MATCH_INFO[:cmd][1..-2]

    if $LAST_MATCH_INFO[:backslash]
      # Command is escaped, don't replace it.
      $LAST_MATCH_INFO[0][1..-1]
    else
      # Execute the command and return the value
      `#{command}`.chomp
    end
  end
end