Module: Strongspace::Helpers
- Included in:
- Command, Command::Base, Plugin
- Defined in:
- lib/strongspace/helpers.rb
Class Method Summary collapse
- .home_directory ⇒ Object
- .running_on_a_mac? ⇒ Boolean
- .running_on_windows? ⇒ Boolean
- .support_directory ⇒ Object
Instance Method Summary collapse
- #ask(default = nil) ⇒ Object
- #backup_space?(name) ⇒ Boolean
- #bin_folder ⇒ Object
- #command_name ⇒ Object
- #computername ⇒ Object
- #confirm(message = "Are you sure you wish to continue? (y/n)?") ⇒ Object
- #confirm_command(app = app) ⇒ Object
- #create_pid_file(name, pid) ⇒ Object
- #credentials_file ⇒ Object
- #credentials_folder ⇒ Object
- #delete_pid_file(name) ⇒ Object
- #display(msg, newline = true) ⇒ Object
- #error(msg) ⇒ Object
- #format_date(date) ⇒ Object
- #gui_ssh_key ⇒ Object
- #home_directory ⇒ Object
- #kill_via_pidfile(name) ⇒ Object
- #launchd_agents_folder ⇒ Object
- #logs_folder ⇒ Object
- #pid_file_path(name) ⇒ Object
- #pid_from_pid_file(name) ⇒ Object
- #pids_folder ⇒ Object
- #plugins_folder ⇒ Object
- #process_running?(name) ⇒ Boolean
- #redisplay(line, line_break = false) ⇒ Object
- #running_on_a_mac? ⇒ Boolean
- #running_on_windows? ⇒ Boolean
- #shell(cmd) ⇒ Object
- #space_exist?(name) ⇒ Boolean
- #ssh_binary ⇒ Object
- #ssh_keygen_binary ⇒ Object
- #support_directory ⇒ Object
Class Method Details
.home_directory ⇒ Object
8 9 10 |
# File 'lib/strongspace/helpers.rb', line 8 def self.home_directory running_on_windows? ? ENV['APPDATA'] : ENV['HOME'] end |
.running_on_a_mac? ⇒ Boolean
32 33 34 |
# File 'lib/strongspace/helpers.rb', line 32 def self.running_on_a_mac? RUBY_PLATFORM =~ /-darwin\d/ end |
.running_on_windows? ⇒ Boolean
24 25 26 |
# File 'lib/strongspace/helpers.rb', line 24 def self.running_on_windows? RUBY_PLATFORM =~ /mswin32|mingw32/ end |
.support_directory ⇒ Object
16 17 18 |
# File 'lib/strongspace/helpers.rb', line 16 def self.support_directory running_on_windows? ? "#{home_directory}\\Strongspace" : "#{home_directory}/Library/Strongspace" end |
Instance Method Details
#ask(default = nil) ⇒ Object
265 266 267 268 269 270 271 272 |
# File 'lib/strongspace/helpers.rb', line 265 def ask(default=nil) r = gets.strip if r.blank? return default else return r end end |
#backup_space?(name) ⇒ Boolean
287 288 289 290 291 292 293 294 295 296 |
# File 'lib/strongspace/helpers.rb', line 287 def backup_space?(name) space = nil strongspace.spaces.each do |s| if s["name"] == name then space = s break end end return space["type"] == "backup" end |
#bin_folder ⇒ Object
107 108 109 |
# File 'lib/strongspace/helpers.rb', line 107 def bin_folder "#{support_directory}/bin" end |
#command_name ⇒ Object
4 5 6 |
# File 'lib/strongspace/helpers.rb', line 4 def command_name self.class.name.split("::").last end |
#computername ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/strongspace/helpers.rb', line 56 def computername n = File.read(credentials_file).split("\n")[2] if n.blank? if running_on_a_mac? @computername ||= `system_profiler SPSoftwareDataType | grep "Computer Name"`.split(":").last.nice_slug elsif running_on_windows? @computername ||= ENV['COMPUTERNAME'].nice_slug else @computername ||= `hostname`.strip.nice_slug end if @computername.length < 3 @computername = ENV['USER'].nice_slug end File.open(credentials_file, 'a') do |f| f.puts @computername end else @computername = n.strip end return @computername end |
#confirm(message = "Are you sure you wish to continue? (y/n)?") ⇒ Object
228 229 230 231 |
# File 'lib/strongspace/helpers.rb', line 228 def confirm(="Are you sure you wish to continue? (y/n)?") display("#{} ", false) ask.downcase == 'y' end |
#confirm_command(app = app) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/strongspace/helpers.rb', line 233 def confirm_command(app = app) if extract_option('--force') display("Warning: The --force switch is deprecated, and will be removed in a future release. Use --confirm #{app} instead.") return true end raise(Strongspace::Command::CommandFailed, "No app specified.\nRun this command from app folder or set it adding --app <app name>") unless app confirmed_app = extract_option('--confirm', false) if confirmed_app unless confirmed_app == app raise(Strongspace::Command::CommandFailed, "Confirmed app #{confirmed_app} did not match the selected app #{app}.") end return true else display "\n ! Potentially Destructive Action" display " ! To proceed, type \"#{app}\" or re-run this command with --confirm #{@app}" display "> ", false if ask.downcase != app display " ! Input did not match #{app}. Aborted." false else true end end end |
#create_pid_file(name, pid) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/strongspace/helpers.rb', line 184 def create_pid_file(name, pid) if process_running?(name) return nil end if not File.exist?(pids_folder) FileUtils.mkdir_p(pids_folder) end file = File.new(pid_file_path(name), "w") file.puts "#{pid}" file.close return true end |
#credentials_file ⇒ Object
87 88 89 |
# File 'lib/strongspace/helpers.rb', line 87 def credentials_file "#{credentials_folder}/credentials" end |
#credentials_folder ⇒ Object
83 84 85 |
# File 'lib/strongspace/helpers.rb', line 83 def credentials_folder "#{support_directory}/credentials" end |
#delete_pid_file(name) ⇒ Object
201 202 203 204 205 |
# File 'lib/strongspace/helpers.rb', line 201 def delete_pid_file(name) if File.exist?(pid_file_path(name)) File.unlink(pid_file_path(name)) end end |
#display(msg, newline = true) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/strongspace/helpers.rb', line 207 def display(msg, newline=true) if ENV["STRONGSPACE_DISPLAY"] == "silent" return end if newline puts(msg) else print(msg) STDOUT.flush end end |
#error(msg) ⇒ Object
223 224 225 226 |
# File 'lib/strongspace/helpers.rb', line 223 def error(msg) STDERR.puts(msg) exit 1 end |
#format_date(date) ⇒ Object
260 261 262 263 |
# File 'lib/strongspace/helpers.rb', line 260 def format_date(date) date = Time.parse(date) if date.is_a?(String) date.strftime("%Y-%m-%d %H:%M %Z") end |
#gui_ssh_key ⇒ Object
40 41 42 |
# File 'lib/strongspace/helpers.rb', line 40 def gui_ssh_key "#{credentials_folder}/#{computername}.rsa" end |
#home_directory ⇒ Object
12 13 14 |
# File 'lib/strongspace/helpers.rb', line 12 def home_directory return Strongspace::Helpers.home_directory end |
#kill_via_pidfile(name) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/strongspace/helpers.rb', line 132 def kill_via_pidfile(name) existing_pid = pid_from_pid_file(name) if not existing_pid return false end begin # This process is running, Kill 0 is a no-op that only works # if the process exists Process.kill(9, existing_pid) return true rescue Errno::EPERM error "No longer have permissions to check this PID" return rescue Errno::ESRCH # Cleanup orphaned pid file and continue on as normal File.unlink(pid_file_path(name)) return rescue error "Unable to determine status for #{existing_pid} : #{$!}" return end File.unlink(pid_file_path(name)) return false end |
#launchd_agents_folder ⇒ Object
111 112 113 |
# File 'lib/strongspace/helpers.rb', line 111 def launchd_agents_folder "#{support_directory}/LaunchAgents" end |
#logs_folder ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/strongspace/helpers.rb', line 99 def logs_folder if running_on_a_mac? "#{home_directory}/Library/Logs/Strongspace" else "#{support_directory}/logs" end end |
#pid_file_path(name) ⇒ Object
115 116 117 |
# File 'lib/strongspace/helpers.rb', line 115 def pid_file_path(name) "#{pids_folder}/#{name}" end |
#pid_from_pid_file(name) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/strongspace/helpers.rb', line 119 def pid_from_pid_file(name) if File.exist?(pid_file_path(name)) f = File.open(pid_file_path(name)) existing_pid = Integer(f.gets) f.close return existing_pid end return nil end |
#pids_folder ⇒ Object
91 92 93 |
# File 'lib/strongspace/helpers.rb', line 91 def pids_folder "#{support_directory}/pids" end |
#plugins_folder ⇒ Object
95 96 97 |
# File 'lib/strongspace/helpers.rb', line 95 def plugins_folder Strongspace::Plugin.directory end |
#process_running?(name) ⇒ Boolean
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/strongspace/helpers.rb', line 160 def process_running?(name) existing_pid = pid_from_pid_file(name) if not existing_pid return false end begin # This process is running, Kill 0 is a no-op that only works # if the process exists Process.kill(0, existing_pid) return true rescue Errno::EPERM error "No longer have permissions to check this PID" rescue Errno::ESRCH # Cleanup orphaned pid file and continue on as normal File.unlink(pid_file_path(name)) rescue error "Unable to determine status for #{existing_pid} : #{$!}" end return false end |
#redisplay(line, line_break = false) ⇒ Object
219 220 221 |
# File 'lib/strongspace/helpers.rb', line 219 def redisplay(line, line_break = false) display("\r\e[0K#{line}", line_break) end |
#running_on_a_mac? ⇒ Boolean
36 37 38 |
# File 'lib/strongspace/helpers.rb', line 36 def running_on_a_mac? RUBY_PLATFORM =~ /-darwin\d/ end |
#running_on_windows? ⇒ Boolean
28 29 30 |
# File 'lib/strongspace/helpers.rb', line 28 def running_on_windows? RUBY_PLATFORM =~ /mswin32|mingw32/ end |
#shell(cmd) ⇒ Object
274 275 276 |
# File 'lib/strongspace/helpers.rb', line 274 def shell(cmd) FileUtils.cd(Dir.pwd) {|d| return `#{cmd}`} end |
#space_exist?(name) ⇒ Boolean
278 279 280 281 282 283 284 |
# File 'lib/strongspace/helpers.rb', line 278 def space_exist?(name) strongspace.spaces.each do |space| # TODO: clean up the json returned by the strongspace API requests to simplify this iteration return true if space["name"] == name end return false end |
#ssh_binary ⇒ Object
50 51 52 53 54 |
# File 'lib/strongspace/helpers.rb', line 50 def ssh_binary return "/usr/bin/ssh" if running_on_a_mac? return "#{support_directory}/bin/ssh.exe".to_cygpath if running_on_windows? return "ssh" end |
#ssh_keygen_binary ⇒ Object
44 45 46 47 48 |
# File 'lib/strongspace/helpers.rb', line 44 def ssh_keygen_binary return "/usr/bin/ssh-keygen" if running_on_a_mac? return "#{support_directory}/bin/ssh-keygen.exe" if running_on_windows? return "ssh-keygen" end |
#support_directory ⇒ Object
20 21 22 |
# File 'lib/strongspace/helpers.rb', line 20 def support_directory return Strongspace::Helpers.support_directory end |