Module: AwsRunAs::Utils

Defined in:
lib/aws_runas/utils.rb

Overview

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

Class Method Summary collapse

Class Method Details

.compute_message(profile:) ⇒ Object

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



82
83
84
85
86
87
88
# 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:) ⇒ Object

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.



34
35
36
37
38
39
40
41
42
43
44
45
46
# 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)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:) ⇒ Object

“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.



94
95
96
97
98
99
100
101
102
103
104
# 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:) ⇒ Object

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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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("export OLDPROMPT=\"${PROMPT}\"\n")
    rc_file.write("PROMPT=$'%{\\e[\\%}$(aws_session_status_color)m(#{message})%{\\e[0m%} $OLDPROMPT'\n")
  end
  rc_file.close
  env.store('ZDOTDIR', rc_dir)
  system(env, path)
ensure
  FileUtils.rmtree(rc_dir)
end

.shellObject

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



71
72
73
74
75
76
77
78
79
# 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_dirObject

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



27
28
29
# File 'lib/aws_runas/utils.rb', line 27

def shell_profiles_dir
  File.expand_path('../../../shell_profiles', __FILE__)
end