Module: ShellUtils
- Defined in:
- lib/shellutils.rb
Constant Summary collapse
- SUDO_PWD_PATH =
File.("~/.sudo_pwd")
- @@color =
Thor::Shell::Color.new
Class Method Summary collapse
- .add_config(file_path, config_title, config_content, comment_char = "#") ⇒ Object
- .current_method(index = 0) ⇒ Object
- .current_user ⇒ Object
- .error(msg) ⇒ Object
- .exec_cmd_and_clear_password(cmd) ⇒ Object
- .file_add(filepath, str) ⇒ Object
- .file_read(filepath) ⇒ Object
- .file_write(filepath, str) ⇒ Object
- .get_platform ⇒ Object
- .get_sudo_pwd ⇒ Object
- .is_installed?(program) ⇒ Boolean
- .is_linux? ⇒ Boolean
- .is_mac? ⇒ Boolean
- .jruby(cmd) ⇒ Object
- .make_file_from_template(template_path, dest_path, hash) ⇒ Object
- .ruby(cmd) ⇒ Object
- .rvmsudo(cmd) ⇒ Object
- .say(cmd, color = nil) ⇒ Object
- .set_sudo_pwd(pw) ⇒ Object
- .sh(cmd) ⇒ Object
- .sudo(cmd) ⇒ Object
- .user_input(desc, example = nil, choices = nil) ⇒ Object
Class Method Details
.add_config(file_path, config_title, config_content, comment_char = "#") ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/shellutils.rb', line 156 def add_config(file_path, config_title, config_content, comment_char="#") text = File.read(file_path) # delete config if already exists text.gsub!(/#{get_config_header(config_title, comment_char)}.*#{get_config_footer(comment_char)}/m, "") # add config text << get_config_header(config_title, comment_char) text << config_content text << (comment_char) File.open(file_path, "w") {|f| f.write(text)} end |
.current_method(index = 0) ⇒ Object
112 113 114 |
# File 'lib/shellutils.rb', line 112 def current_method(index=0) caller[index].scan(/`(.*)'/).flatten.to_s end |
.current_user ⇒ Object
169 170 171 |
# File 'lib/shellutils.rb', line 169 def current_user `whoami`.chomp end |
.error(msg) ⇒ Object
107 108 109 110 |
# File 'lib/shellutils.rb', line 107 def error(msg) STDERR.puts @@color.set_color("### ERROR:#{msg}", :red) raise end |
.exec_cmd_and_clear_password(cmd) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/shellutils.rb', line 116 def exec_cmd_and_clear_password(cmd) puts cmd begin PTY.spawn(cmd) do |r, w| w.flush w.sync = true w.flush if r.expect(/[Pp]assword.*:.*$/, 1) w.flush w.puts(get_sudo_pwd) w.flush begin w.flush if r.expect(/[Pp]assword.*:.*$/, 1) error("the sudo password is incorrect. password=#{get_sudo_pwd}\nPlease change the password ex) set_sudo_pwd PASSWORD") end rescue end end w.flush begin puts r.read rescue Errno::EIO # GNU/Linux raises EIO. rescue IOError end end rescue Errno::EIO # If the password prompt does not appear. `#{cmd}` end end |
.file_add(filepath, str) ⇒ Object
36 37 38 |
# File 'lib/shellutils.rb', line 36 def file_add(filepath, str) File.open(filepath, "a") {|f| f.write str} end |
.file_read(filepath) ⇒ Object
40 41 42 |
# File 'lib/shellutils.rb', line 40 def file_read(filepath) File.open(filepath) {|f| f.read} end |
.file_write(filepath, str) ⇒ Object
32 33 34 |
# File 'lib/shellutils.rb', line 32 def file_write(filepath, str) File.open(filepath, "w") {|f| f.write str} end |
.get_platform ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/shellutils.rb', line 54 def get_platform if is_linux? 'linux' elsif is_mac? 'macos' else 'unknown' end end |
.get_sudo_pwd ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/shellutils.rb', line 68 def get_sudo_pwd if File.exist?(SUDO_PWD_PATH) == false || file_read(SUDO_PWD_PATH) == "" pw = user_input("please input sudo password") set_sudo_pwd(pw) end file_read(SUDO_PWD_PATH) end |
.is_installed?(program) ⇒ Boolean
64 65 66 |
# File 'lib/shellutils.rb', line 64 def is_installed?(program) `which #{program}`.chomp != "" end |
.is_linux? ⇒ Boolean
44 45 46 47 |
# File 'lib/shellutils.rb', line 44 def is_linux? return true if `uname` =~ /Linux/ false end |
.is_mac? ⇒ Boolean
49 50 51 52 |
# File 'lib/shellutils.rb', line 49 def is_mac? return true if `uname` =~ /Darwin/ false end |
.jruby(cmd) ⇒ Object
28 29 30 |
# File 'lib/shellutils.rb', line 28 def jruby(cmd) sh "jruby -S #{cmd}" end |
.make_file_from_template(template_path, dest_path, hash) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/shellutils.rb', line 98 def make_file_from_template(template_path, dest_path, hash) puts "create #{dest_path}" template = ERB.new File.read(template_path) open(dest_path, "w") do |f| binded_txt = template.result binding f.puts binded_txt end end |
.ruby(cmd) ⇒ Object
24 25 26 |
# File 'lib/shellutils.rb', line 24 def ruby(cmd) sh "ruby #{cmd}" end |
.rvmsudo(cmd) ⇒ Object
152 153 154 |
# File 'lib/shellutils.rb', line 152 def rvmsudo(cmd) exec_cmd_and_clear_password("rvmsudo #{cmd}") end |
.say(cmd, color = nil) ⇒ Object
15 16 17 |
# File 'lib/shellutils.rb', line 15 def say(cmd, color=nil) puts @@color.set_color(cmd, color) end |
.set_sudo_pwd(pw) ⇒ Object
76 77 78 |
# File 'lib/shellutils.rb', line 76 def set_sudo_pwd(pw) file_write(SUDO_PWD_PATH, pw) end |
.sh(cmd) ⇒ Object
19 20 21 22 |
# File 'lib/shellutils.rb', line 19 def sh(cmd) say(cmd, :green) `#{cmd}` end |
.sudo(cmd) ⇒ Object
148 149 150 |
# File 'lib/shellutils.rb', line 148 def sudo(cmd) exec_cmd_and_clear_password("sudo #{cmd}") end |
.user_input(desc, example = nil, choices = nil) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/shellutils.rb', line 80 def user_input(desc, example = nil, choices = nil) if example puts "#{desc}: ex) #{example}" elsif choices puts "#{desc}: [#{choices.join("|")}]" else puts "#{desc}:" end print ">>" input = STDIN.gets.chomp while choices && !choices.include?(input) puts "The value you entered is incorrect(#{input}). Please re-enter" print ">>" input = STDIN.gets.chomp end input end |