Sha256: 622b3a2ed1cf49f53bfb1a81fa2fbf1689be76fe894eb5f8bf462aeac1e9a415

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

module Hexlet
  class MemberCLI < BaseCLI
    desc "submit PATH_TO_EXERCISE", "submit exercise"
    def submit(path)
      expanded_path = File.expand_path(path)
      lesson_path, exercise_folder_name = File.split(expanded_path)
      lesson_folder_name = File.split(lesson_path)[1]
      parts = lesson_folder_name.split("_")

      if parts[-1] != "lesson"
        puts (t "wrong_lesson_folder")
        return false
      end

      lesson_slug = parts[0, parts.size - 1].join("_")

      # process = ChildProcess.build("make", "test")
      # process.start
      _, e, s = Open3.capture3("make test -C #{path}")
      if s == 0
        result = client.submit lesson_slug, exercise_folder_name
        if result
          puts (t :created)
          true
        else
          puts (t :error)
          false
        end
      else
        puts (t :bad_tests)
        puts e
        false
      end
    end

    desc "fetch LESSON_SLUG EXERCISE_SLUG", "download lesson exercise"
    def fetch(lesson_slug, exercise_slug)
      # FIXME check login
      if content = client.fetch(lesson_slug, exercise_slug)
        lesson_path = File.join("/", "vagrant", "exercises", "#{lesson_slug}_lesson")
        exercise_path = File.join(lesson_path, exercise_slug)
        FileUtils.mkdir_p(exercise_path)

        tarball_path = File.join(exercise_path, "exercise.tar.gz")
        File.open(tarball_path, "w") do |f|
          f.write content
        end

        unless ENV['TEST'] # FIXME
          tgz = Zlib::GzipReader.new(File.open(tarball_path, 'rb'))
          Archive::Tar::Minitar.unpack(tgz, exercise_path)
        end

        puts (t :ok)
        true
      else
        puts (t :not_found)
        false
      end
    end

    private

    def client
      @client ||= Hexlet::MemberClient.new config["hexlet_api_key"], logger: logger, host: options["host"]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hexlet-0.2.1 lib/hexlet/member_cli.rb