module AwsRunAs::Utils

Utility functions that aren't specifically tied to a class.

Public Instance Methods

compute_message(profile:) click to toggle source

Compute the message given to the prompt based off supplied profile.

# File lib/aws_runas/utils.rb, line 82
def compute_message(profile:)
  if profile.nil?
    'AWS'
  else
    "AWS:#{profile}"
  end
end
handoff_bash(env:, path:, message:, skip_prompt:) click to toggle source

Run an interactive bash session with a special streamed RC file. The RC merges a local .bashrc if it exists, with a prompt that includes the computed message from handoff_to_shell.

# File lib/aws_runas/utils.rb, line 34
def handoff_bash(env:, path:, message:, skip_prompt:)
  rc_data = IO.read("#{ENV['HOME']}/.bashrc") if File.exist?("#{ENV['HOME']}/.bashrc")
  rc_file = Tempfile.new('aws_runas_bashrc')
  rc_file.write("#{rc_data}\n") unless rc_data.nil?
  rc_file.write(IO.read("#{shell_profiles_dir}/sh.profile"))
  unless skip_prompt
    rc_file.write("PS1=\"\\[\\e[\\$(aws_session_status_color \"bash\")m\\](#{message})\\[\\e[0m\\] $PS1\"\n")
  end
  rc_file.close
  system(env, path, '--rcfile', rc_file.path)
ensure
  rc_file.unlink
end
handoff_to_shell(env:, profile: nil, skip_prompt:) click to toggle source

“Handoff” to a supported interactive shell. More technically, this runs an interactive shell with the shell prompt customized to the current running AWS profile. If the shell is not something we can handle specifically, just run the shell.

# File lib/aws_runas/utils.rb, line 94
def handoff_to_shell(env:, profile: nil, skip_prompt:)
  path = shell
  if path.end_with?('/bash')
    handoff_bash(env: env, path: path, message: compute_message(profile: profile), skip_prompt: skip_prompt)
  elsif path.end_with?('/zsh')
    handoff_zsh(env: env, path: path, message: compute_message(profile: profile), skip_prompt: skip_prompt)
  else
    system(env, path)
  end
  exit $CHILD_STATUS.exitstatus
end
handoff_zsh(env:, path:, message:, skip_prompt:) click to toggle source

Run an interactive zsh session with a special streamed RC file. The RC merges a local .zshrc if it exists, with a prompt that includes the computed message from handoff_to_shell.

# File lib/aws_runas/utils.rb, line 51
def handoff_zsh(env:, path:, message:, skip_prompt:)
  rc_data = IO.read("#{ENV['HOME']}/.zshrc") if File.exist?("#{ENV['HOME']}/.zshrc")
  rc_dir = Dir.mktmpdir('aws_runas_zsh')
  rc_file = File.new("#{rc_dir}/.zshrc", 'w')
  rc_file.write("#{rc_data}\n") unless rc_data.nil?
  rc_file.write(IO.read("#{shell_profiles_dir}/sh.profile"))
  unless skip_prompt
    rc_file.write("setopt PROMPT_SUBST\n")
    rc_file.write("OLDPROMPT=\"$PROMPT\"\n")
    rc_file.write("PROMPT=\"%F{$(aws_session_status_color \"zsh\")}(#{message})%f $OLDPROMPT\"\n")
  end
  rc_file.close
  env.store('ZDOTDIR', rc_dir)
  system(env, path)
ensure
  FileUtils.rmtree(rc_dir)
end
shell() click to toggle source

load the shell for a specific operating system. if $SHELL exists, load that.

# File lib/aws_runas/utils.rb, line 71
def shell
  if RbConfig::CONFIG['host_os'] =~ /mswin|windows|mingw32/i
    'cmd.exe'
  elsif ENV.include?('SHELL')
    ENV['SHELL']
  else
    '/bin/sh'
  end
end
shell_profiles_dir() click to toggle source

Return the path to the shell_profiles directory vendored with the gem.

# File lib/aws_runas/utils.rb, line 27
def shell_profiles_dir
  File.expand_path('../../../shell_profiles', __FILE__)
end