Class: EnfCli::CLI

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

Overview

CLI for launching shell

Instance Method Summary collapse

Methods inherited from EnfThor

capture_stdout, command_help, handle_argument_error, help

Instance Method Details

#__print_versionObject



292
293
294
# File 'lib/enfcli.rb', line 292

def __print_version
  puts EnfCli::VERSION
end

#connect(*names) ⇒ Object



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
282
283
284
285
286
287
# File 'lib/enfcli.rb', line 250

def connect(*names)
  host = ""
  user = ""

  try_with_rescue do
    ## first check for command options
    if options[:host] && options[:user]
      host = options[:host]
      user = options[:user]
    elsif File.file?(CONFIG_FILE) # then check for config file
      config_json = JSON.load(File.open(CONFIG_FILE))
      host = config_json["host"]
      user = config_json["user"]
    else
      raise EnfCli::ERROR, "You must either specify the --host and --user parameters or create a Xaptum config file to connect."
    end

    # Make sure to use https as default
    host = "https://#{host}" unless host =~ /^(http|https):\/\//

    # Ask for password
    say "Connecting to '#{host}'.....", :bold
    password = EnfCli::ask_password()

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

    # initialize CTX
    EnfCli::CTX.instance.init host, user, resp[:data][0]

    # launch shell/exec cmd
    if names.empty?
      EnfCli::Shell::Console::start
    else
      EnfCli::Shell::CLI.start names
    end
  end
end

#create_config_fileObject



316
317
318
319
320
321
322
323
# File 'lib/enfcli.rb', line 316

def create_config_file
  host = options[:host]
  user = options[:user]
  config_file = File.new(CONFIG_FILE, "w+")
  config_file.puts({ host: host, user: user }.to_json)
  config_file.close
  say "Config file created successfully at #{CONFIG_FILE}!", :green
end

#display_config_fileObject

Raises:



327
328
329
330
331
332
333
334
# File 'lib/enfcli.rb', line 327

def display_config_file
  file = CONFIG_FILE

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

  say File.readlines(file).join
end

#searchObject



306
307
308
309
310
# File 'lib/enfcli.rb', line 306

def search
  cmd = Gem::Commands::SearchCommand.new
  cmd.handle_options ["enfcli"]
  execute_gem_cmd cmd
end

#updateObject



298
299
300
301
302
# File 'lib/enfcli.rb', line 298

def update
  cmd = Gem::Commands::UpdateCommand.new
  cmd.handle_options ["enfcli"]
  execute_gem_cmd cmd
end