lib/shelly/cli/main.rb in shelly-0.0.14 vs lib/shelly/cli/main.rb in shelly-0.0.15

- old
+ new

@@ -1,37 +1,46 @@ require "shelly" require "thor/group" +require "shelly/cli/users" module Shelly module CLI class Main < Thor include Thor::Actions include Helpers + register(Users, "users", "users <command>", "Manages users using this app") map %w(-v --version) => :version desc "version", "Displays shelly version" def version say "shelly version #{Shelly::VERSION}" end desc "register [EMAIL]", "Registers new user account on Shelly Cloud" def register(email = nil) + user = User.new + user.ssh_key_registered? say "Registering with email: #{email}" if email - user = User.new(email || ask_for_email, ask_for_password) + user.email = (email || ask_for_email) + user.password = ask_for_password user.register if user.ssh_key_exists? say "Uploading your public SSH key from #{user.ssh_key_path}" end say "Successfully registered!" say "Check you mailbox for email address confirmation" rescue Client::APIError => e if e.validation? e.errors.each do |error| - say "#{error.first} #{error.last}" + say_error "#{error.first} #{error.last}", :with_exit => false end exit 1 end + rescue RestClient::Conflict + say_error "User with your ssh key already exists.", :with_exit => false + say_error "You can login using: shelly login [EMAIL]", :with_exit => false + exit 1 end desc "login [EMAIL]", "Logins user to Shelly Cloud" def login(email = nil) user = User.new(email || ask_for_email, ask_for_password(:with_confirmation => false)) @@ -42,38 +51,44 @@ say "You have following applications available:", :green user.apps.each do |app| say " #{app["code_name"]}" end rescue RestClient::Unauthorized - say "Wrong email or password or your email is unconfirmend" + say_error "Wrong email or password", :with_exit => false + say_error "You can reset password by using link:", :with_exit => false + say_error "https://admin.winniecloud.com/users/password/new", :with_exit => false exit 1 - rescue Client::APIError + rescue Client::APIError => e if e.validation? - e.errors.each { |error| say "#{error.first} #{error.last}" } + e.errors.each { |error| say_error "#{error.first} #{error.last}", :with_exit => false } exit 1 end end + method_option :code_name, :type => :string, :aliases => "-c", + :desc => "Unique code_name of your application" + method_option :environment, :type => :string, :aliases => "-e", + :desc => "Environment that your application will be running" + method_option :databases, :type => :array, :aliases => "-d", + :banner => "#{Shelly::App::DATABASE_KINDS.join(' ')}", + :desc => "Array of databases of your choice" + method_option :domains, :type => :array, + :banner => "CODE_NAME.shellycloud.com YOUR_DOMAIN.com", + :desc => "Array of your domains" desc "add", "Adds new application to Shelly Cloud" def add say_error "Must be run inside your project git repository" unless App.inside_git_repository? - + check_options(options) @app = Shelly::App.new - @app.purpose = ask_for_purpose - @app.code_name = ask_for_code_name - @app.databases = ask_for_databases + @app.purpose = options["environment"] || ask_for_purpose + @app.code_name = options["code_name"] || ask_for_code_name + @app.databases = options["databases"] || ask_for_databases + @app.domains = options["domains"] @app.create - unless @app.remote_exists? - say "Adding remote #{@app.purpose} #{@app.git_url}", :green - @app.add_git_remote - else - say "Remote #{@app.purpose} already exists" - if yes?("Would you like to overwrite remote #{@app.purpose} with #{@app.git_url} (Y/N)?:") - @app.add_git_remote(true) - end - end + say "Adding remote #{@app.purpose} #{@app.git_url}", :green + @app.add_git_remote say "Creating Cloudfile", :green @app.create_cloudfile say "Provide billing details. Opening browser...", :green @@ -81,17 +96,31 @@ info_adding_cloudfile_to_repository info_deploying_to_shellycloud rescue Client::APIError => e if e.validation? - e.errors.each { |error| say "#{error.first} #{error.last}" } + e.errors.each { |error| say_error "#{error.first} #{error.last}", :with_exit => false } exit 1 end end # FIXME: move to helpers no_tasks do + def check_options(options) + unless ["environment", "code_name", "databases", "domains"].all? do |option| + options.include?(option.to_s) && options[option.to_s] != option.to_s + end && valid_databases?(options["databases"]) + say "Wrong parameters. See 'shelly help add' for further information" + exit 1 + end unless options.empty? + end + + def valid_databases?(databases) + kinds = Shelly::App::DATABASE_KINDS + databases.all? { |kind| kinds.include?(kind) } + end + def ask_for_email email_question = User.guess_email.blank? ? "Email:" : "Email (#{User.guess_email} - default):" email = ask(email_question) email = email.blank? ? User.guess_email : email return email if email.present? @@ -108,13 +137,13 @@ say "Password confirmation: " password_confirmation = echo_disabled { $stdin.gets.strip } say_new_line if password.present? return password if password == password_confirmation - say "Password and password confirmation don't match, please type them again" + say_error "Password and password confirmation don't match, please type them again" else - say "Password can't be blank" + say_error "Password can't be blank" end end end def ask_for_purpose @@ -131,11 +160,11 @@ def ask_for_databases kinds = Shelly::App::DATABASE_KINDS databases = ask("Which database do you want to use #{kinds.join(", ")} (postgresql - default):") begin databases = databases.split(/[\s,]/).reject(&:blank?) - valid = databases.all? { |kind| kinds.include?(kind) } + valid = valid_databases?(databases) break if valid databases = ask("Unknown database kind. Supported are: #{kinds.join(", ")}:") end while not valid databases.empty? ? ["postgresql"] : databases @@ -161,5 +190,6 @@ end end end end end +