Module: Fjords::Cli::Runner

Included in:
Core
Defined in:
lib/fjords/cli/runner.rb

Instance Method Summary collapse

Instance Method Details

#initialize(args = []) ⇒ Object



4
5
6
7
8
# File 'lib/fjords/cli/runner.rb', line 4

def initialize(args=[])
  @args = args
  @options = {}
  @exit_status = true
end

#parse_command!Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fjords/cli/runner.rb', line 29

def parse_command!
  verb = @args.shift
  case verb

  when 'help'
    help if @args.size == 0
    @help_only = true
    parse_command!
  when 'nameservers'
    usage('fjords nameservers')
    nameservers
  when 'sites'
    usage('fjords sites')
    sites
  when 'push'
    usage('fjords push [--path PATH] [--domain DOMAIN] [--domain-file DOMAIN_FILE]')
    push
  when 'login'
    usage('fjords login')
    
  when 'logout'
    usage('fjords logout')
    logout
  when 'signup'
    usage('fjords signup')
    
  when 'remove'
    usage('fjords remove DOMAIN')
    remove(@args[0])
  when 'bugreport'
    usage('fjords bugreport [--attach ERROR_FILE]')
    bugreport
  when 'info'
    usage('fjords info')
    info
  when 'version'
    usage('fjords version')
    version
  else
    if verb
      puts "fjords: Unknown command [#{verb}]"
      puts basic_usage
      exit(false)
    end
  end
end

#parse_options!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fjords/cli/runner.rb', line 10

def parse_options!
  opts_parser = OptionParser.new do |opts|
    opts.banner = "\nAvailable options:\n\n"

    opts.on('--attach ERROR_FILE') { |attach| @options[:attach] = attach }
    opts.on('--host PATH') { |host| @options[:host] = host }
    opts.on('--path PATH') { |path| @options[:path] = path }
    opts.on('--domain DOMAIN') { |domain| @options[:domain] = domain }
    opts.on('--overwrite') { |overwrite| @options[:overwrite] = overwrite }
    opts.on('--no-zip') { |no_zip| @options[:no_zip] = !no_zip }
    opts.on('--no-progress') { |no_progress| @options[:no_progress] = !no_progress }
    opts.on('--permanent') { |permanent| @options[:permanent] = permanent }
    opts.on('--domain-file DOMAIN_FILE') { |domain_file| @options[:domain_file] = domain_file }
    opts.on('-h', '--help') { puts "#{command_usage}\n"; exit }
  end

  @args = opts_parser.parse!(@args)
end

#runObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/fjords/cli/runner.rb', line 98

def run
  trap('TERM') { print "\nTerminated\n"; exit(false)}

  parse_options!

  if @options[:host]
    Fjords::Client.host = @options[:host]
    puts "Using API host: #{Fjords::Client.host}"
  end

  if @options[:no_zip]
    Fjords::Client.ruby_zip = @options[:no_zip]
    puts "Using built-in Ruby Zip"
  end

  begin
    res = parse_command!
  rescue Fjords::Client::PreconditionFailed => e
    puts
    puts "Error: Your login token has expired. Please login again with: fjords login"
    exit(0)
  rescue Fjords::Client::ConnectionRefused => e
    puts
    puts "Error: Could not connect to server"
    exit(0)
  rescue => e
    file_name = save_error_report(e)

    puts
    puts "Sorry, an unexpected error has occured (#{e.to_s})"
    puts
    puts "The details of this error have been logged to: "
    puts "  #{file_name}"
    puts
    puts "You can report this bug to the developer by running:"
    puts
    puts "  fjords bugreport --attach=#{file_name}"

    exit(0)
  end

  if res

  elsif @help_only || @usage
    display_usage
  else
    puts basic_usage
    exit(false)
  end

rescue OptionParser::InvalidOption => e
  puts(e.message)
  puts("\n")
  puts(basic_usage)
  @exit_status = false
rescue OptionParser::AmbiguousOption => e
  puts(e.message)
  puts("\n")
  puts(basic_usage)
  @exit_status = false
rescue SystemExit => e
  @exit_status = e.success?
rescue SyntaxError => e
  puts e.message
  puts e.backtrace
  @exit_status = false
rescue Interrupt => e
  puts("\nInterrupted")
  @exit_status = false
rescue Exception => e
  puts e.message
  puts e.backtrace
  @exit_status = false
ensure
  puts("\n")
  @exit_status == true if @exit_status.nil?
  exit(@exit_status)
end

#save_error_report(e) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fjords/cli/runner.rb', line 76

def save_error_report(e)
  dir = File.expand_path("~/.fjords-errors")

  FileUtils.mkdir_p(dir)

  filename = File.join(dir, "exception-#{Time.now.to_i}.txt")

  File.open(filename, 'w') do |file|
    file.write("#{e.to_s}\n\n")

    if e.respond_to?(:report_data)
      file.write(e.report_data.join("\n"))
    elsif e.respond_to?(:backtrace)
      file.write(e.backtrace.join("\n"))
    else
      file.write("Unknown exception format")
    end
  end

  filename
end