lib/meroku/cli.rb in meroku-2.0.22 vs lib/meroku/cli.rb in meroku-2.0.23

- old
+ new

@@ -1,104 +1,36 @@ -require 'meroku/cli/help' -require 'meroku/cli/secrets' -require 'meroku/cli/session' +# frozen_string_literal: true module Meroku - # The CLI is used by end users and also maintainers - module CLI - include Meroku::Aws::Ec2 + # The CLI is a class responsible of handling all the command line interface + # logic. + class CLI + include Meroku::Shared + include Meroku::Api + attr_reader :options - def signup - email, pw = Meroku::Extensions.mgets(%i[email password]) - Meroku::Api::Request.post( - 'https://www.meroku.com/users.json', - user: { email: email, password: pw, password_confirmation: pw } - ) do |response| - puts "Signed up #{response['attributes']['email']}" - save_setting('apiusername', response['attributes']['apiusername']) - save_setting('apisecret', response['attributes']['apisecret']) - end - true + def initialize + @options = {} end - def login - email, password = Meroku::Extensions.mgets(%i[email password]) - Meroku::Api::Request.post( - 'https://www.meroku.com/users/sign_in.json', - user: { email: email, password: password } - ) do |response| - puts "Signed in as #{response['attributes']['email']}" - save_setting('apiusername', response['attributes']['apiusername']) - save_setting('apisecret', response['attributes']['apisecret']) - end - true + def run(args = ARGV) + @options = Options.new.parse(args) + act_on_options + rescue Meroku::Error => e + puts "Error: #{e.message}" + return 2 end - def logout - [ - "#{Dir.home}/.meroku/.apiusername", - "#{Dir.home}/.meroku/.apisecret" - ].each do |f| - File.delete(f) if File.exist?(f) - end - true - end + private - def create - Meroku::Api::Request.post( - 'https://www.meroku.com/apps.json', - app: { name: 'unnamed' }, apisecret: `cat ~/.meroku/.apisecret`.chomp - ) do |response| - puts "Created #{response['attributes']['name']}, adding git remote" - add_git_remote( - 'meroku', - "#{apiusername}@www.meroku.com:#{response['attributes']['name']}.git" - ) + def act_on_options + if @options[:meroku_secret] + Meroku::Shared.secrets.meroku_secret = @options[:meroku_secret] end + Node.new if @options[:spawn] + User.login(@options[:email], @options[:password]) if @options[:login] + User.logout if @options[:logout] + Meroku::Aws.terminate_all(tag: 'node') if @options[:despawn] true - end - - def apiusername - `cat ~/.meroku/.apiusername.chomp` - end - - def add_git_remote(name, uri) - puts "git remote remove #{name}" - `git remote remove #{name} 2>/dev/null` - puts "git remote add #{name} #{uri}" - `git remote add #{name} #{uri}` - end - - def keys_add - file = "#{Dir.home}/.ssh/id_rsa.pub" - apisecret = `cat ~/.meroku/.apisecret`.chomp - abort "error: #{file} not found" unless File.exist?(file) - Meroku::Api::Request.post( - 'https://www.meroku.com/publickeys.json', - publickey: { name: 'id_rsa.pub', data: `cat ~/.ssh/id_rsa.pub`.chomp }, - apisecret: apisecret - ) do |response| - puts "Added #{response['attributes']['name']}" - end - true - end - - def spawn - Meroku::CLI::Secrets.load(admin: true) - node = Meroku::Node.new - node.associate_address.configure_keys.add_sources.ubuntu_updates - node.install_packages.database_inits.git_clone.nginx_configs.start_rails - true - end - - def despawn - Meroku::CLI::Secrets.load(admin: true) - Meroku::Infrastructure.each_server(&:shutdown) - true - end - - def save_setting(name, value) - FileUtils.mkdir_p "#{Dir.home}/.meroku" - IO.write("#{Dir.home}/.meroku/.#{name}", value) end end end