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



303
304
305
# File 'lib/enfcli.rb', line 303

def __print_version
  puts EnfCli::VERSION
end

#connect(*names) ⇒ Object



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
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/enfcli.rb', line 261

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



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

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:



338
339
340
341
342
343
344
345
# File 'lib/enfcli.rb', line 338

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



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

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

#updateObject



309
310
311
312
313
# File 'lib/enfcli.rb', line 309

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