Class: EnfCli::Shell::CLI

Inherits:
EnfThor
  • Object
show all
Defined in:
lib/enfcli.rb

Overview

Shell CLI class

Instance Method Summary collapse

Methods inherited from EnfThor

capture_stdout, command_help, handle_argument_error, help

Instance Method Details

#cat(file) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
# File 'lib/enfcli.rb', line 404

def cat(file)
  try_with_rescue do
    # expand path
    file = EnfCli::expand_path(file)

    ## return if keyfile not found
    raise EnfCli::ERROR, "#{file} not found!" unless File.exists?(file)

    say File.readlines(file).join
  end
end

#cd(dir = "~") ⇒ Object



426
427
428
429
430
431
432
433
# File 'lib/enfcli.rb', line 426

def cd(dir = "~")
  try_with_rescue do
    dir = EnfCli::expand_path(dir)
    raise EnfCli::ERROR, "No such directory #{dir}" unless Dir.exist?(dir)

    Dir.chdir(dir)
  end
end

#clearObject



445
446
447
448
449
450
# File 'lib/enfcli.rb', line 445

def clear
  try_with_rescue do
    clear_code = %x{clear}
    print clear_code or system("cls")
  end
end

#display_session_tokenObject



454
455
456
457
458
# File 'lib/enfcli.rb', line 454

def display_session_token
  try_with_rescue_in_session do
    say EnfCli::CTX.instance.auth_token.to_s
  end
end

#hostObject



437
438
439
440
441
# File 'lib/enfcli.rb', line 437

def host
  try_with_rescue do
    say EnfCli::CTX.instance.host, :bold
  end
end

#ls(dir = nil) ⇒ Object



391
392
393
394
395
396
397
398
399
400
# File 'lib/enfcli.rb', line 391

def ls(dir = nil)
  try_with_rescue do
    dir ||= "."
    dir = EnfCli::expand_path(dir)

    Dir.entries(dir).each { |f|
      puts f unless f.start_with?(".")
    }
  end
end

#pwdObject



418
419
420
421
422
# File 'lib/enfcli.rb', line 418

def pwd
  try_with_rescue do
    say Dir.pwd
  end
end

#refresh_session_tokenObject



462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/enfcli.rb', line 462

def refresh_session_token
  try_with_rescue_in_session do
    # Get user and host
    host = EnfCli::CTX.instance.host
    user = EnfCli::CTX.instance.user

    # Ask for password
    say "Refreshing session token.....", :bold
    password = EnfCli::ask_password()

    # Authenticate
    resp = EnfApi::API.instance.authenticate(host, user, password)

    # update session
    EnfCli::CTX.instance.session = resp[:data][0]

    # display success
    say "Refreshed session token!", :green
  end
end