Class: Fjords::Cli::Core

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

Overview

require “rbconfig” WINDOWS = !!(RbConfig::CONFIG =~ /mingw|mswin32|cygwin/)

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



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/fjords/cli/core.rb', line 293

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

#helpObject



22
23
24
25
# File 'lib/fjords/cli/core.rb', line 22

def help
  say command_usage
  exit
end

#infoObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fjords/cli/core.rb', line 97

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



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

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



278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/fjords/cli/core.rb', line 278

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



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

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



317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/fjords/cli/core.rb', line 317

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



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

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 TooManyConnections
    say "Sorry, we've reached our max users for this beta period. We're growing slowly, but surely."
  end

  true
end

#sitesObject



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

def sites
  return if @help_only
  require_login!
  
  sites = Fjords::Client.sites

  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
      
      d = DateTime.parse(site['created_at'])
      [
        domain,
        site['type'],
        cost,
        site['type'] === 'temporary' ? time_to_expiry(d.to_time.to_i + (60 * 60 * 24 * 30)) : 'never'
      ]
    end

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

  true
end

#versionObject

include Interactive disable_rewind



16
17
18
19
20
# File 'lib/fjords/cli/core.rb', line 16

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