Class: FaaStRuby::Command::Workspace::Migrate

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/faastruby/cli/commands/workspace/migrate.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

#has_user_logged_in?, #help, #load_credentials, #load_yaml, #say, spin, #spin, #write_file

Constructor Details

#initialize(args) ⇒ Migrate

Returns a new instance of Migrate.



11
12
13
14
15
16
17
# File 'lib/faastruby/cli/commands/workspace/migrate.rb', line 11

def initialize(args)
  @args = args
  @failed = []
  @migrated = []
  help
  load_credentials
end

Class Method Details

.helpObject



81
82
83
# File 'lib/faastruby/cli/commands/workspace/migrate.rb', line 81

def self.help
  "migrate-workspaces"
end

Instance Method Details

#backup_file(file) ⇒ Object



56
57
58
# File 'lib/faastruby/cli/commands/workspace/migrate.rb', line 56

def backup_file(file)
  FileUtils.mv(file, "#{file}.bkp")
end

#migrate(workspace_name, credentials) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/faastruby/cli/commands/workspace/migrate.rb', line 67

def migrate(workspace_name, credentials)
  spinner = spin("Migrating workspace '#{workspace_name}'...")
  api = API.new
  response = api.(workspace_name: workspace_name, api_key: credentials['api_key'], api_secret: credentials['api_secret'])
  if response.code > 299
    @failed << workspace_name
    spinner.error
    return false
  end
  @migrated << workspace_name
  spinner.success
  return true
end

#notify_failedObject



60
61
62
63
64
65
# File 'lib/faastruby/cli/commands/workspace/migrate.rb', line 60

def notify_failed
  return unless @failed.any?
  puts "\nThe following workspaces failed to be migrated: #{@failed.join(', ').red}"
  puts "Please come over to our Slack and we will assist you with this migration."
  puts "Click the following link to join our Slack: https://faastruby.io/slack\n\n"
end

#remove_migrated(file) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/faastruby/cli/commands/workspace/migrate.rb', line 48

def remove_migrated(file)
  credentials = Oj.load(File.read(file))
  @migrated.each do |workspace_name|
    credentials.delete(workspace_name)
  end
  File.write(file, JSON.pretty_generate(credentials))
end

#runObject



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
# File 'lib/faastruby/cli/commands/workspace/migrate.rb', line 19

def run
  file1 = File.expand_path('~/.faastruby')
  file2 = File.expand_path('~/.faastruby.tor1')
  if !File.file?(file1) && !File.file?(file2)
    puts "Nothing to migrate."
    exit 0
  end
  old_credential_files = [file1, file2]

  puts "@@@ WARNING @@@ WARNING @@@ WARNING @@@ WARNING @@@ ".red
  puts "This is going to migrate all your legacy credentials into your new account. This process is REQUIRED, but irreversible."
  email = NewCredentials::CredentialsFile.new.get['email']
  puts "You are currently logged in as '#{email}'."
  print "Continue? [y/N] "
  response = STDIN.gets.chomp
  FaaStRuby::CLI.error("Exiting", color: nil) unless response == 'y'
  old_credential_files.each do |file|
    next unless File.file?(file)
    FileUtils.cp(file, "#{file}.backup_before_migration") unless File.file?("#{file}.backup_before_migration")
    workspaces = Oj.load(File.read(file))
    workspaces.each do |workspace_name, credentials|
      migrate(workspace_name, credentials)
    end
    remove_migrated(file)
    backup_file(file)
  end
  notify_failed
end

#usageObject



85
86
87
88
89
# File 'lib/faastruby/cli/commands/workspace/migrate.rb', line 85

def usage
  puts "\n# Migrate legacy workspace credentials to your new FaaStRuby account."
  puts "# You must have an account and be logged in to perform the migration."
  puts "\nUsage: faastruby #{self.class.help}\n\n"
end