Sha256: cde69a78f81ea8dfa9e55282a810d108f283752d953f10455f79596ce28d1e9b

Contents?: true

Size: 1.25 KB

Versions: 10

Compression:

Stored size: 1.25 KB

Contents

require "English"

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 = $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
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
dotenv-2.2.2 lib/dotenv/substitutions/command.rb
dotenv-2.2.1 lib/dotenv/substitutions/command.rb
dotenv-2.2.0 lib/dotenv/substitutions/command.rb
dotenv-2.1.2 lib/dotenv/substitutions/command.rb
dotenv-2.1.1 lib/dotenv/substitutions/command.rb
dotenv-2.1.0 lib/dotenv/substitutions/command.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/dotenv-2.0.2/lib/dotenv/substitutions/command.rb
dotenv-2.0.2 lib/dotenv/substitutions/command.rb
dotenv-2.0.1 lib/dotenv/substitutions/command.rb
dotenv-2.0.0 lib/dotenv/substitutions/command.rb