Module: Strongspace::Command

Extended by:
Helpers
Defined in:
lib/strongspace/command.rb,
lib/strongspace/commands/auth.rb,
lib/strongspace/commands/base.rb,
lib/strongspace/commands/help.rb,
lib/strongspace/commands/keys.rb,
lib/strongspace/commands/files.rb,
lib/strongspace/commands/spaces.rb,
lib/strongspace/commands/plugins.rb,
lib/strongspace/commands/version.rb

Defined Under Namespace

Classes: Auth, Base, CommandFailed, Delete, Download, Help, InvalidCommand, Keys, Mkdir, Plugins, Quota, Size, Spaces, Upload, Version

Class Method Summary collapse

Methods included from Helpers

ask, backup_space?, bin_folder, command_name, computername, confirm, confirm_command, create_pid_file, credentials_file, credentials_folder, delete_pid_file, display, error, format_date, gui_ssh_key, home_directory, home_directory, kill_via_pidfile, launchd_agents_folder, logs_folder, pid_file_path, pid_from_pid_file, pids_folder, plugins_folder, process_running?, redisplay, running_on_a_mac?, running_on_a_mac?, running_on_windows?, running_on_windows?, shell, space_exist?, support_directory, support_directory

Class Method Details

.extract_error(body) ⇒ Object



84
85
86
87
# File 'lib/strongspace/command.rb', line 84

def extract_error(body)
  msg = parse_error_json(body) || 'Internal server error'
  msg.split("\n").map { |line| ' !   ' + line }.join("\n")
end

.extract_not_found(body) ⇒ Object



80
81
82
# File 'lib/strongspace/command.rb', line 80

def extract_not_found(body)
  body =~ /^[\w\s]+ not found$/ ? body : "Resource not found"
end

.parse(command) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/strongspace/command.rb', line 59

def parse(command)
  parts = command.split(':')
  case parts.size
    when 1
      begin
        return eval("Strongspace::Command::#{command.camelize}"), :index
      rescue NameError, NoMethodError
        return Strongspace::Command::Base, command.to_sym
      end
    else
      begin
        const = Strongspace::Command
        command = parts.pop
        parts.each { |part| const = const.const_get(part.camelize) }
        return const, command.to_sym
      rescue NameError
        raise InvalidCommand
      end
  end
end

.parse_error_json(body) ⇒ Object



89
90
91
92
93
# File 'lib/strongspace/command.rb', line 89

def parse_error_json(body)
  json = JSON.parse(body.to_s)
  json['status']
rescue JSON::ParserError
end

.run(command, args, retries = 0) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/strongspace/command.rb', line 10

def run(command, args, retries=0)
  Strongspace::Plugin.load!
  begin
    # internal-only commands cannot be be run from the command line
    if command.index(":_")
      raise InvalidCommand
    end

    run_internal 'auth:reauthorize_interactve', args.dup if retries > 0
    run_internal(command, args.dup)
  rescue InvalidCommand
    error "Unknown command. Run 'strongspace help' for usage information."
  rescue Strongspace::Exceptions::InvalidCredentials
    if retries < 1
      STDERR.puts "Authentication failure"
      run(command, args, retries+1)
    else
      error "! Authentication failure"
    end
  rescue RestClient::Unauthorized
    if retries < 1
      STDERR.puts "Authentication failure"
      run(command, args, retries+1)
    else
      error "! Authentication failure"
    end
  rescue RestClient::ResourceNotFound => e
    error extract_not_found(e.http_body)
  rescue RestClient::RequestFailed => e
    error extract_error(e.http_body) unless e.http_code == 402
  rescue RestClient::RequestTimeout
    error "API request timed out. Please try again, or contact [email protected] if this issue persists."
  rescue CommandFailed => e
    error e.message
  rescue Interrupt => e
    error "\n[canceled]"
  end
end

.run_internal(command, args, strongspace = nil) ⇒ Object

Raises:



49
50
51
52
53
54
55
56
57
# File 'lib/strongspace/command.rb', line 49

def run_internal(command, args, strongspace=nil)
  if command == "web:start"
    require 'strongspace-web'
  end
  klass, method = parse(command)
  runner = klass.new(args, strongspace)
  raise InvalidCommand unless runner.respond_to?(method)
  runner.send(method)
end