Sha256: cae7d7ba196168c44ad5a115148fbbbf2b970d156f3cd8f8e0c5eeea6468a9fb

Contents?: true

Size: 1.26 KB

Versions: 9

Compression:

Stored size: 1.26 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, _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
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/command.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/command.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/command.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/command.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/command.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/command.rb
study_line-0.2.1 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/command.rb
study_line-0.2.0 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/command.rb
dotenv-2.8.1 lib/dotenv/substitutions/command.rb