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



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
# File 'lib/fjords/cli/runner.rb', line 30

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 [--deleted]')
    sites
  when 'site:details'
    usage('fjords ite:details DOMAIN')

    if !@args[0] || @args[0].length < 1
      puts
      puts "Error: DOMAIN argument required"
      exit(0)
    end
    
    site_details(@args[0])
  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')

    if !@args[0] || @args[0].length < 1
      puts
      puts "Error: DOMAIN argument required"
      exit(0)
    end

    remove(@args[0])
  when 'cdn:enable'
    usage('fjords cdn:enable DOMAIN')

    if !@args[0] || @args[0].length < 1
      puts
      puts "Error: DOMAIN argument required"
      exit(0)
    end

    enable_cdn(@args[0])
  when 'cdn:disable'
    usage('fjords cdn:disable DOMAIN')

    if !@args[0] || @args[0].length < 1
      puts
      puts "Error: DOMAIN argument required"
      exit(0)
    end
    
    disable_cdn(@args[0])
  when 'gzip:enable'
    usage('fjords gzip:enable DOMAIN')

    if !@args[0] || @args[0].length < 1
      puts
      puts "Error: DOMAIN argument required"
      exit(0)
    end

    enable_gzip(@args[0])
  when 'gzip:disable'
    usage('fjords gzip:disable DOMAIN')

    if !@args[0] || @args[0].length < 1
      puts
      puts "Error: DOMAIN argument required"
      exit(0)
    end
    
    disable_gzip(@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
28
# 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('--deleted') { |deleted| @options[:deleted] = deleted }
    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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/fjords/cli/runner.rb', line 156

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/fjords/cli/runner.rb', line 134

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