Sha256: b215f1856fb954a029ee116f73364be05be835b9ddc3f5c2eca4f58e91058617

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

module Dotenv
  module Substitutions
    # Substitute shell commands in a value.
    #
    #   SHA=$(git rev-parse HEAD)
    #
    module Command
      class << self

        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

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

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

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dotenv-2.0.0.beta lib/dotenv/substitutions/command.rb
dotenv-1.0.2 lib/dotenv/substitutions/command.rb
dotenv-1.0.1 lib/dotenv/substitutions/command.rb
dotenv-1.0.0 lib/dotenv/substitutions/command.rb