Class: Fjords::Cli::Core

Inherits:
Object
  • Object
show all
Includes:
Runner, Usage
Defined in:
lib/fjords/cli/core.rb

Instance Method Summary collapse

Methods included from Usage

#basic_usage, #command_usage, #display_usage, #usage, #usage_error

Methods included from Runner

#initialize, #parse_command!, #parse_options!, #run, #save_error_report

Instance Method Details

#bugreportObject



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/fjords/cli/core.rb', line 396

def bugreport
  return if @help_only
  require_login!

  is_ready = agree("Ready to write your bug report? This will open your system $EDITOR: ") { |q| q.default = "yes" }

  if !is_ready
    exit(0)
  end

  body = ask_editor("Write your bug report below, then close your editor.\n----------------\n\n")

  if body && !body.nil?
    attach = @options[:attach] || nil

    puts
    say "Sending..."
    Fjords::Client.bugreport(body, attach)
    true
  else
    exit(0)
  end
end

#disable_cdn(domain) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/fjords/cli/core.rb', line 461

def disable_cdn(domain)
  return if @help_only
  require_login!

  puts

  has_cdn = Fjords::Client::has_cdn(domain)

  if !has_cdn
    say %Q{Error: CDN is not enabled for site "#{domain}".}
    exit(0)
  elsif Fjords::Client::disable_cdn(domain)
    say %Q{Disabled CDN for "#{domain}"}
    true
  else
    say %Q{Error: Could not find site "#{domain}" to disable CDN for.}
    exit(0)
  end
end

#disable_gzip(domain) ⇒ Object



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/fjords/cli/core.rb', line 501

def disable_gzip(domain)
  return if @help_only
  require_login!

  puts

  has_gzip = Fjords::Client::has_gzip(domain)

  if !has_gzip
    say %Q{Error: Gzipping is not enabled for site "#{domain}".}
    exit(0)
  elsif Fjords::Client::disable_gzip(domain)
    say %Q{Disabled Gzipping Assets for "#{domain}"}
    true
  else
    say %Q{Error: Could not find site "#{domain}" to disable Gzipping for.}
    exit(0)
  end
end

#enable_cdn(domain) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/fjords/cli/core.rb', line 434

def enable_cdn(domain)
  return if @help_only
  require_login!

  puts

  has_cdn = Fjords::Client::has_cdn(domain)

  if has_cdn
    say %Q{Error: CDN already enabled for site "#{domain}".}
    exit(0)
  elsif site = Fjords::Client::enable_cdn(domain)
    say %Q{Enabled CDN for "#{domain}".}
    say "Deploying your files around the planet takes some time."
    say "Please be patient."
    puts

    say "In the meantime,"
    say "Make sure your CNAME points to: #{site["cdn_internal_name"]}"

    true
  else
    say %Q{Error: Could not find site "#{domain}" to enable CDN for.}
    exit(0)
  end
end

#enable_gzip(domain) ⇒ Object



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/fjords/cli/core.rb', line 481

def enable_gzip(domain)
  return if @help_only
  require_login!

  puts

  has_gzip = Fjords::Client::has_gzip(domain)

  if has_gzip
    say %Q{Error: Gzipping already enabled for site "#{domain}".}
    exit(0)
  elsif Fjords::Client::enable_gzip(domain)
    say %Q{Enabled Gzipping Assets for "#{domain}".}
    true
  else
    say %Q{Error: Could not find site "#{domain}" to enable Gzipping for.}
    exit(0)
  end
end

#helpObject



40
41
42
43
# File 'lib/fjords/cli/core.rb', line 40

def help
  say command_usage
  exit
end

#infoObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/fjords/cli/core.rb', line 144

def info
  return if @help_only
  require_login!
  
  info = Fjords::Client.info
  a = info["account"]
  u = info["user"]

  puts
  print_table([
    ["Username:", u["username"]],
    ["Email:", u["email"]],
    ["Total Cost:", "$#{(a['current_price'] / 100.00).to_i} USD per month"]
  ], :indent => 2)

  puts
  say "For site information, see: fjords sites"

  true
end

#loginObject

true end



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/fjords/cli/core.rb', line 348

def 
  return if @help_only

  if Fjords::Client.logged_in?
    puts
    say "Already logged-in"
    return true
  end

  puts
  username = ask "Username: "
  password = ask("Password: ") { |q| q.echo = "*" }

  begin
    puts
    say "Contacting server..."
    Fjords::Client.(username, password)

    puts
    say "Successfully logged in"

    true
  rescue Fjords::Client::ResourceNotFound => e
    puts
    say "Login Failed"
    exit(0)
  rescue Fjords::Client::LoginInvalid => e
    puts
    say "Login Failed"
    exit(0)
  end
end

#logoutObject



381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/fjords/cli/core.rb', line 381

def logout
  return if @help_only

  if !Fjords::Client.logged_in?
    say "Not logged-in"
    return true
  end

  Fjords::Client.logout
  puts
  say "Logged Out"

  true
end

#pushObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/fjords/cli/core.rb', line 221

def push
  return if @help_only
  require_login!

  path = File.expand_path(@options[:path] || Dir.pwd)
  domain_file = @options[:domain_file] || "Fjordsfile"
  full_domain_file_path = File.expand_path(domain_file, path)

  overwrite = @options[:overwrite] || false
  permanent = @options[:permanent] || nil

  domain = if @options[:domain]
    @options[:domain]
  else
    if File.exists?(full_domain_file_path)
      overwrite = true
      File.read(full_domain_file_path).chomp
    else
      nil
    end
  end

  if domain
    valid = Fjords::Client.validate_domain(domain)
    if valid === false
      say "The domain #{domain} is already taken by another account."
      exit(0)
    elsif !overwrite && valid["site"]["exists"]
      unless agree("A site is already live at this domain. Overwrite?")
        exit(0)
      end
    end
  end
  
  puts
  say "Preparing to push: #{path}"

  begin
    zipfile, changeless, filesize = Fjords::Client.prepare_push(path, valid, domain_file)
  rescue Fjords::Client::UploadTooLarge
    say "Sorry, sites cannot be over 50M in size."
    exit(0)
  rescue Fjords::Client::NothingToUpload
    say "There are no changes to push. Checksums match live site."
    exit(0)
  end

  say "Compressed site down to #{filesize}"
  say "Pushing..."

  # seen_log_messages = {}

  if !@options[:no_progress]
    bar = ::ProgressBar.new(100, :bar, :percentage)
  end

  first_progress = true

  last_uploaded = 0

  site = Fjords::Client.push(zipfile, changeless, domain, permanent) do |data|
    # data["site_logs"].each do |log|
    #   if !seen_log_messages[log["id"]]
    #     seen_log_messages[log["id"]] = log
    #     say "* #{log["message"]}"
    #   end
    # end
    if first_progress
      puts
      say "Deploying..."
      first_progress = false
    end

    if !@options[:no_progress] && data["percentage_uploaded"] > last_uploaded
      bar.count = data["percentage_uploaded"]
      bar.write
      last_uploaded = data["percentage_uploaded"]
    end
  end

  if !@options[:no_progress] && last_uploaded < 100
    bar.count = 100
    bar.write
    puts
  end

  puts
  if site["site"]["dns_mode"] === 'alias'
    cname = site["site"]["custom_name"]
    say "Site is now available at: http://#{cname}"
    say "Make sure your CNAME points to: #{site["site"]["internal_name"]}"
    # say "Make sure your Nameservers points to us. See: fjords nameservers"
  else
    if site["site"]["dns_mode"] === 'random'
      cname = site["site"]["internal_name"]
      say %Q{Site is now available at: http://#{site["site"]["internal_name"]}}
    elsif site["site"]["dns_mode"] === 'cname'
      cname = site["site"]["custom_name"]
      say %Q{Site is now available at: http://#{site["site"]["custom_name"]}}
      if site["site"]["internal_name"] != site["site"]["custom_name"]
        say "Make sure your CNAME points to: #{site["site"]["internal_name"]}"
      end
    end
  end

  if !File.exists?(full_domain_file_path)
    say "Writing #{domain_file} for future pushes."

    File.open(full_domain_file_path, 'w') { |f| f.write(cname) }
  end

  true
end

#remove(domain) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/fjords/cli/core.rb', line 420

def remove(domain)
  return if @help_only
  require_login!

  puts
  if Fjords::Client::delete_site(domain)
    say %Q{Successfully removed site "#{domain}"}
    true
  else
    say %Q{Error: Could not find site "#{domain}" to remove.}
    exit(0)
  end
end

#signupObject



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
# File 'lib/fjords/cli/core.rb', line 45

def 
  return if @help_only

  puts
  say "This process will create a new account for you."
  say "Signup requires a valid US credit card which will"
  say "be charged at a rate of $6 USD per month for basic"
  say "service. Let's get started."
  puts

  username = get_unique_username
  email = ask "Email: "

  password = ask("Password: ") { |q| q.echo = "*" }
  confirmed_password = ask("Confirm Password: ") { |q| q.echo = "*" }

  if password != confirmed_password
    say "Passwords do not match"
  end

  puts
  say "Great! Now let's get your credit card information."
  puts

  stripe_token = get_credit_card_information

  say "Creating account..."

  begin
    Fjords::Client.(email, username, password, stripe_token.id)

    say "Success! Your account is ready and you have been logged-in."
  rescue Fjords::Client::TooManyConnections
    say "Sorry, we've reached our max users for this beta period. We're growing slowly, but surely."
  end

  true
end

#site_details(domain) ⇒ Object



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
# File 'lib/fjords/cli/core.rb', line 165

def site_details(domain)
  return if @help_only
  require_login!
  
  site = Fjords::Client.site_details(domain)

  if site
    cost = "$#{(site['cost'] / 100.00).to_i}"
    domain = site['internal_name']
    domain = site['custom_name'] if site['custom_name'] != domain
    
    if site['deleted_at']
      d = DateTime.parse(site['deleted_at'])
      deleting_at = time_to_expiry(d.to_time.to_i)
      cdn_status = 'no'
    else
      d = DateTime.parse(site['created_at'])
      deleting_at = site['type'] === 'temporary' ? time_to_expiry(d.to_time.to_i + (60 * 60 * 24 * 30)) : 'never'
      
      cdn_status = if site['cdn_enabled']
        # site['cdn_status']
        "yes ($#{(site['cost'] > 0 ? '2' : '0')})"
      else
        'no'
      end
    end

    output = []
    output << ["Domain:", domain]
    output << ["Custom DNS:", site['type'] == 'with_dns' ? "yes ($#{(site['cost'] > 0 ? '2' : '0')})" : 'no']

    if site['type'] == 'with_dns'
      output << ["Point DNS At:", site['internal_name']]
    end

    output << ["CDN Enabled:", cdn_status]
    if site['cdn_enabled']
      output << ["Point CDN DNS At:", site['cdn_internal_name']]
    end

    output << ["Gzip Enabled:", site['gzip_assets'] ? 'yes' : 'no']

    output << ["Expires At:", deleting_at]

    output << ["Montly Cost:", cost]

    puts
    print_table(output, :indent => 2)
  else
    say %Q{Error: Could not find site "#{domain}".}
    exit(0)
  end

  true
end

#sitesObject



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
133
134
135
136
137
138
139
140
141
142
# File 'lib/fjords/cli/core.rb', line 84

def sites
  return if @help_only
  require_login!
  
  show_deleted = @options[:deleted] || false
  sites = Fjords::Client.sites(show_deleted)

  if sites.length > 0
    rows = sites.map do |site|
      cost = "$#{(site['cost'] / 100.00).to_i}"
      domain = site['internal_name']
      domain = site['custom_name'] if site['custom_name'] != domain
      
      if site['deleted_at']
        d = DateTime.parse(site['deleted_at'])
        deleting_at = time_to_expiry(d.to_time.to_i)
        cdn_status = 'no'
      else
        d = DateTime.parse(site['created_at'])
        deleting_at = site['type'] === 'temporary' ? time_to_expiry(d.to_time.to_i + (60 * 60 * 24 * 30)) : 'never'
        
        cdn_status = if site['cdn_enabled']
          # site['cdn_status']
          'yes'
        else
          'no'
        end
      end

      [
        domain,
        site['type'] == 'with_dns' ? 'yes' : 'no',
        site['gzip_assets'] ? 'yes' : 'no',
        cdn_status,
        cost,
        deleting_at,
        d
      ]
    end.sort do |a, b|
      if a[4] == 'never' && b[4] == 'never'
        a[0] <=> b[0]
      elsif a[4] == 'never'
        -1
      elsif b[4] == 'never'
        1
      else
        a[5] <=> b[5]
      end
    end.map { |r| r[0...-1] }

    rows.unshift ["Domain", "DNS?", "Gzip?", "CDN?", "Cost", "Expires"]
    puts
    print_table(rows, :indent => 2)
  else
    say "You don't currently have any sites."
  end

  true
end

#versionObject



34
35
36
37
38
# File 'lib/fjords/cli/core.rb', line 34

def version
  return if @help_only
  say "fjords #{Fjords::VERSION}"
  true
end