require "meroku/cli/session" module Meroku module CLI def help <<~HEREDOC Usage: meroku command subcommand Examples meroku signup # if you havent done already meroku create meroku keys:add meroku infrastrucuture spawn # Spawns server HEREDOC end def signup print "Email: " email = STDIN.gets print "Password: " password = STDIN.noecho(&:gets).chomp print "\n" url = "https://www.meroku.com/users.json" response_json = RestClient.post url, {:user=>{:email => email, :password => password, :password_confirmation => password}}.to_json, timeout: 1, :content_type => :json, :accept => :json if JSON.parse(response_json)["errors"] && JSON.parse(response_json)["errors"].size > 0 puts "error: #{JSON.parse(response_json)["errors"].map{|x| x["detail"]}.join(".")}" else email = JSON.parse(response_json)["data"]["attributes"]["email"] token = JSON.parse(response_json)["data"]["attributes"]["token"] puts "Signed up #{email}" dirname = File.dirname("#{Dir.home}/.meroku") FileUtils.mkdir(dirname) if !File.directory?(dirname) File.open("#{Dir.home}/.meroku/.token", 'w') { |file| file.write(token) } end end def keys_add(session) if !File.exist? "#{Dir.home}/.ssh/id_rsa.pub" puts "error: File #{Dir.home}/.ssh/id_rsa.pub not found" puts "You can use this command to generate a key:" puts " ssh-keygen -t rsa" return nil end name = "id_rsa.pub" data = `cat ~/.ssh/id_rsa.pub`.chomp url = "https://www.meroku.com/publickeys.json" response_json = RestClient.post url, {:publickey=>{:name => name, :data=>data}, :token=>session.token}.to_json, timeout: 1, :content_type => :json, :accept => :json if JSON.parse(response_json)["errors"] && JSON.parse(response_json)["errors"].size > 0 puts "error: #{JSON.parse(response_json)["errors"].map{|x| x["detail"]}.join(".")}" else name = JSON.parse(response_json)["data"]["attributes"]["name"] puts "Added #{name}" end end def create url = "https://www.meroku.com/apps.json" token = `cat ~/.meroku/.token`.chomp response_json = RestClient.post url, {:app=>{:name => "unnamed"}, :token=>token}.to_json, timeout: 1, :content_type => :json, :accept => :json if JSON.parse(response_json)["errors"] && JSON.parse(response_json)["errors"].size > 0 puts JSON.parse(response_json)["errors"].map{|x| x["detail"]}.join(".") else name = JSON.parse(response_json)["data"]["attributes"]["name"] puts "Created #{name}, adding git remote" puts "git remote remove meroku" `git remote remove meroku 2>/dev/null` puts "git remote add meroku #{token}@www.meroku.com:#{name}.git" `git remote add meroku #{token}@www.meroku.com:#{name}.git` end end def self.token_check if !File.exist? "#{Dir.home}/.meroku/.token" puts "error: Have you logged in yet?" return nil end true end def self.load_secrets if !File.exist? "#{Dir.home}/.meroku/.secret" puts "error: File #{Dir.home}/.meroku/.secret not found" return nil end env_file = Dir.home+'/.meroku/meroku.env' if !File.exist? env_file secret=`cat ~/.meroku/.secret`.chomp remote_env_file = "http://www.sam-we.com/dropbox/meroku-#{secret}/meroku.env" File.write(env_file, Net::HTTP.get(URI.parse(remote_env_file))) end Dotenv.load(env_file) end end end