Class: TrainSH::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/trainsh/cli.rb

Constant Summary collapse

EXIT_COMMANDS =
%w[!!! exit quit logout disconnect].freeze
INTERACTIVE_COMMANDS =
%w[more less vi vim nano].freeze
NON_OS_TRANSPORTS =
%w[aws core kubernetes azure pgsql vsphere vault digitalocean rest].freeze
CORE_TRANSPORTS =
%w[docker ssh].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/trainsh/cli.rb', line 21

def self.exit_on_failure?
  true
end

Instance Method Details

#__print_versionObject



27
28
29
# File 'lib/trainsh/cli.rb', line 27

def __print_version
  say "#{TrainSH::PRODUCT} #{TrainSH::VERSION} (Ruby #{RUBY_VERSION}-#{RUBY_PLATFORM})"
end

#connect(url = nil) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/trainsh/cli.rb', line 221

def connect(url = nil)
  # TODO: Pass options to `use_session`
  unless url
    show_message 'No URL given, trying to detect ...'
    url = detect_target

    show_message "Detected URL to be #{url}" if url
  end

  unless url
    show_error 'No target could be detected'
    exit
  end

  exit unless use_session(url)

  say format('Connected to %<url>s', url: session.url).bold
  say 'Running platform detection...'
  __detect

  # History persistence (TODO: Extract)
  user_conf_dir = File.join(ENV['HOME'], TrainSH::USER_CONF_DIR)
  history_file = File.join(user_conf_dir, 'history')
  FileUtils.mkdir_p(user_conf_dir)
  FileUtils.touch(history_file)
  File.readlines(history_file).each { |line| Readline::HISTORY.push line.strip }
  at_exit {
    history_file = File.join(user_conf_dir, 'history')
    File.open(history_file, 'w') { |f|
      f.write Readline::HISTORY.to_a.join("\n")
    }
  }

  # Catch Ctrl-C and exit cleanly
  stty_save = `stty -g`.chomp
  trap('INT') do
    puts '^C'
    system('stty', stty_save)
    exit
  end

  # Autocompletion
  Readline.completion_proc = method(:auto_complete).to_proc
  Readline.completion_append_character = ' '

  while (input = Readline.readline(prompt, true))
    if input.empty?
      Readline::HISTORY.pop
      next
    end

    Readline::HISTORY.pop if input.start_with? '!history'

    break if exit_command? input
    next if interactive_command? input

    execute input
  end
rescue Interrupt
  show_error 'Interrupted execution'
end

#detect(url) ⇒ Object



292
293
294
295
# File 'lib/trainsh/cli.rb', line 292

def detect(url)
  exit unless use_session(url)
  __detect
end

#list_transportsObject



305
306
307
308
309
310
# File 'lib/trainsh/cli.rb', line 305

def list_transports
  installed = local_gems.select { |name| name.start_with? 'train-' }.keys.map { |name| name.delete_prefix('train-') }
  transports = installed - NON_OS_TRANSPORTS + CORE_TRANSPORTS

  say "Installed transports: #{transports.sort.join(', ')}"
end