lib/cli.rb in exercism-0.0.18 vs lib/cli.rb in exercism-0.0.19

- old
+ new

@@ -1,5 +1,6 @@ +require 'rubygems' if RUBY_VERSION == '1.8.7' require 'thor' class Exercism class CLI < Thor @@ -48,16 +49,26 @@ desc "submit FILE", "Submit code to exercism.io on your current assignment" method_option :host, :aliases => '-h', :default => 'http://exercism.io', :desc => 'the url of the exercism application' def submit(file) require 'exercism' - path = File.join(FileUtils.pwd, file) + submission = Submission.new(file) + + if submission.test? + say "It looks like this is a test, you probably don't want to do that." + if no?("Do you want to submit it anyway? [y/n]") + return + end + end + begin - response = Exercism::Api.new(options[:host], Exercism.user).submit(file) - puts "Your assignment has been submitted." - url = submission_url(response.body, options[:host]) - puts "For feedback on your submission visit #{url}" + response = Exercism::Api.new(options[:host], Exercism.user).submit(submission.file) + say "Your assignment has been submitted." + + body = JSON.parse(response.body) + url = "#{options[:host]}/#{Exercism.user.github_username}/#{body['language']}/#{body['exercise']}" + say "For feedback on your submission visit #{url}" rescue Exception => e puts "There was an issue with your submission." puts e.message end end @@ -66,18 +77,29 @@ def login require 'exercism' username = ask("Your GitHub username:") key = ask("Your exercism.io API key:") - default_path = FileUtils.pwd - path = ask("What is your exercism exercises project path? (#{default_path})") - if path.empty? - path = default_path + default_dir = FileUtils.pwd + say "What is your exercism exercises project path?" + say "Press Enter to select the default (#{default_dir}):\n" + dir = ask ">" + if dir.empty? + dir = default_dir end - path = File.expand_path(path) - Exercism.login(username, key, path) + project_dir = File.expand_path(dir) + say "Where do you want your configuration stored? (type a number)" + say "1. #{File.join(Exercism.home, '.exercism')}" + say "2. #{File.join(Exercism.home, '.config', 'exercism')}" + + if ask(">").to_i == 2 + Exercism.login username, key, project_dir, File.join(Exercism.home, '.config') + else + Exercism.login username, key, project_dir, Exercism.home + end + say("Your credentials have been written to #{Exercism.config.file}") end desc "logout", "Clear exercism.io api credentials" def logout @@ -93,25 +115,23 @@ puts Exercism.user.github_username rescue Errno::ENOENT puts "You are not logged in." end -private + private + def api(host = options[:host]) Exercism::Api.new(host, Exercism.user, Exercism.project_dir) end - def submission_url(response_body, host) - body = JSON.parse(response_body) - "#{host}/user/#{body['language']}/#{body['exercise']}" - end - def report(assignments) if assignments.empty? - puts "No assignments fetched." + say "No assignments fetched." else assignments.each do |assignment| - puts "Fetched #{File.join(assignment.assignment_dir)}" + say "\nFetched #{assignment.exercise}" + say File.join(assignment.exercise, 'README.md') + say File.join(assignment.exercise, assignment.test_file) end end end end