Sha256: 6a265b8cd54b3775a8236d954f02e7d98e01b9bf16de4e8c2173c490d595982d

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

module PDK
  module CLI
    @env_cmd = @base_cmd.define_command do
      name 'env'
      usage 'env'
      summary '(Experimental) Output environment variables for specific Puppet context'
      description <<~EOF
        [experimental] Aids in setting a CLI context for a specified version of Puppet by outputting export commands for necessary environment variables.
      EOF

      PDK::CLI.puppet_version_options(self)
      PDK::CLI.puppet_dev_option(self)

      run do |opts, _args, _cmd|
        require 'pdk/util'
        require 'pdk/util/ruby_version'

        PDK::CLI::Util.validate_puppet_version_opts(opts)

        PDK::CLI::Util.analytics_screen_view('env')

        # Ensure that the correct Ruby is activated before running command.
        puppet_env = PDK::CLI::Util.puppet_from_opts_or_env(opts)
        PDK::Util::RubyVersion.use(puppet_env[:ruby_version])

        resolved_env = {
          'PDK_RESOLVED_PUPPET_VERSION' => puppet_env[:gemset][:puppet],
          'PDK_RESOLVED_RUBY_VERSION' => puppet_env[:ruby_version]
        }

        resolved_env['GEM_HOME'] = PDK::Util::RubyVersion.gem_home
        gem_path = PDK::Util::RubyVersion.gem_path
        resolved_env['GEM_PATH'] = gem_path.empty? ? resolved_env['GEM_HOME'] : gem_path

        # Make sure invocation of Ruby prefers our private installation.
        package_binpath = PDK::Util.package_install? ? File.join(PDK::Util.pdk_package_basedir, 'bin') : nil

        resolved_env['PATH'] = [
          PDK::Util::RubyVersion.bin_path,
          File.join(resolved_env['GEM_HOME'], 'bin'),
          PDK::Util::RubyVersion.gem_paths_raw.map { |gem_path_raw| File.join(gem_path_raw, 'bin') },
          package_binpath,
          PDK::Util::Env['PATH']
        ].compact.flatten.join(File::PATH_SEPARATOR)

        resolved_env.each do |var, val|
          puts "export #{var}=\"#{val}\""
        end
        exit 0
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pdk-3.0.1 lib/pdk/cli/env.rb
pdk-3.0.0 lib/pdk/cli/env.rb