lib/meroku/cli.rb in meroku-2.0.3 vs lib/meroku/cli.rb in meroku-2.0.4
- old
+ new
@@ -1,9 +1,12 @@
+require "meroku/cli/session"
+
module Meroku
module CLI
- HELP = <<~HEREDOC
+ def help
+ <<~HEREDOC
Usage: meroku command subcommand
Examples
meroku signup # if you havent done already
@@ -12,54 +15,76 @@
meroku keys:add
meroku infrastrucuture spawn # Spawns server
- HEREDOC
-
- def self.start(*args)
- case args.join(" ")
- when "infrastructure spawn"
- load_secrets || exit
- node = Meroku::Infrastructure::Node.new.associate_address.install_packages.install_frontend_app
- puts "spawned #{node.instance.try(:instance_id)}"
- when "infrastructure despawn"
- load_secrets || exit
- Meroku::Infrastructure.despawn
- when "signup"
- signup
- when "keys:add"
- token_check || exit
- keys_add
- when "create"
- token_check || exit
- create
- else
- puts HELP
- end
+ HEREDOC
end
+
+ #def self.start(*args)
+ # case args.join(" ")
+ # when "infrastructure spawn"
+ # load_secrets || exit
+ # node = Meroku::Infrastructure::Node.new.associate_address.install_packages.install_frontend_app
+ # puts "spawned #{node.instance.try(:instance_id)}"
+ # when "infrastructure despawn"
+ # load_secrets || exit
+ # Meroku::Infrastructure.despawn
+ # when "signup"
+ # signup
+ # when "keys:add"
+ # token_check || exit
+ # keys_add
+ # when "create"
+ # token_check || exit
+ # create
+ # else
+ # puts HELP
+ # end
+ #end
- def self.signup
+ def signup
print "Email: "
email = STDIN.gets
print "Password: "
password = STDIN.noecho(&:gets).chomp
print "\n"
url = "https://www.meroku.com/users.json"
- #url = "http://localhost:3000/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 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}"
- save_token(token)
+ 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 self.create
+ 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 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
@@ -94,37 +119,31 @@
File.write(env_file, Net::HTTP.get(URI.parse(remote_env_file)))
end
Dotenv.load(env_file)
end
- def self.save_token(token)
- dirname = File.dirname("#{Dir.home}/.meroku")
- unless File.directory?(dirname)
- FileUtils.mkdir(dirname)
- end
- File.open("#{Dir.home}/.meroku/.token", 'w') { |file| file.write(token) }
- end
- def self.keys_add
- 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"
- token = `cat ~/.meroku/.token`.chomp
- response_json = RestClient.post url, {:publickey=>{:name => name, :data=>data}, :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 "Added #{name}"
- end
- end
+ #def self.keys_add
+ # 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"
+ # token = `cat ~/.meroku/.token`.chomp
+ # response_json = RestClient.post url, {:publickey=>{:name => name, :data=>data}, :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 "Added #{name}"
+ # end
+ #end
end
end