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
- #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['USERPROFILE'] : 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
247 248 249 250 251 252 253 254 |
# File 'lib/strongspace/helpers.rb', line 247 def ask(default=nil) r = gets.strip if r.blank? return default else return r end end |
#backup_space?(name) ⇒ Boolean
269 270 271 272 273 274 275 276 277 278 |
# File 'lib/strongspace/helpers.rb', line 269 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
89 90 91 |
# File 'lib/strongspace/helpers.rb', line 89 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/strongspace/helpers.rb', line 44 def computername n = File.read(credentials_file).split("\n")[2] if n.blank? @computername ||= `system_profiler SPSoftwareDataType | grep "Computer Name"`.split(":").last.gsub(/[[:punct:][:cntrl:]]/, '') if @computername.include?(".local") @computername = @computername.split(".")[0] 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
210 211 212 213 |
# File 'lib/strongspace/helpers.rb', line 210 def confirm(="Are you sure you wish to continue? (y/n)?") display("#{} ", false) ask.downcase == 'y' end |
#confirm_command(app = app) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/strongspace/helpers.rb', line 215 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
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/strongspace/helpers.rb', line 166 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
69 70 71 |
# File 'lib/strongspace/helpers.rb', line 69 def credentials_file "#{credentials_folder}/credentials" end |
#credentials_folder ⇒ Object
65 66 67 |
# File 'lib/strongspace/helpers.rb', line 65 def credentials_folder "#{support_directory}/credentials" end |
#delete_pid_file(name) ⇒ Object
183 184 185 186 187 |
# File 'lib/strongspace/helpers.rb', line 183 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
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/strongspace/helpers.rb', line 189 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
205 206 207 208 |
# File 'lib/strongspace/helpers.rb', line 205 def error(msg) STDERR.puts(msg) exit 1 end |
#format_date(date) ⇒ Object
242 243 244 245 |
# File 'lib/strongspace/helpers.rb', line 242 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
114 115 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 |
# File 'lib/strongspace/helpers.rb', line 114 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
93 94 95 |
# File 'lib/strongspace/helpers.rb', line 93 def launchd_agents_folder "#{support_directory}/LaunchAgents" end |
#logs_folder ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/strongspace/helpers.rb', line 81 def logs_folder if running_on_a_mac? "#{home_directory}/Library/Logs/Strongspace" else "#{support_directory}/logs" end end |
#pid_file_path(name) ⇒ Object
97 98 99 |
# File 'lib/strongspace/helpers.rb', line 97 def pid_file_path(name) "#{pids_folder}/#{name}" end |
#pid_from_pid_file(name) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/strongspace/helpers.rb', line 101 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
73 74 75 |
# File 'lib/strongspace/helpers.rb', line 73 def pids_folder "#{support_directory}/pids" end |
#plugins_folder ⇒ Object
77 78 79 |
# File 'lib/strongspace/helpers.rb', line 77 def plugins_folder Strongspace::Plugin.directory end |
#process_running?(name) ⇒ Boolean
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/strongspace/helpers.rb', line 142 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
201 202 203 |
# File 'lib/strongspace/helpers.rb', line 201 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
256 257 258 |
# File 'lib/strongspace/helpers.rb', line 256 def shell(cmd) FileUtils.cd(Dir.pwd) {|d| return `#{cmd}`} end |
#space_exist?(name) ⇒ Boolean
260 261 262 263 264 265 266 |
# File 'lib/strongspace/helpers.rb', line 260 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 |
#support_directory ⇒ Object
20 21 22 |
# File 'lib/strongspace/helpers.rb', line 20 def support_directory return Strongspace::Helpers.support_directory end |