#!/usr/bin/ruby

require 'debci'
require 'debci/job'
require 'thor'

module Debci
  class JobCLI < Thor
    desc 'import STATUS_FILE [STATUS_FILE ...]', 'Import data from status file'
    method_option :remove, type: :boolean, default: false
    method_option :verbose, type: :boolean, default: false
    def import(*status_files)
      status_files.each do |status_file|
        begin
          Debci::Job.import(status_file)
          puts('I: imported %<status_file>s' % { status_file: status_file }) if options { :verbose }
          if options[:remove]
            File.unlink(status_file)
            puts('I: removed %<status_file>s' % { status_file: status_file }) if options { :verbose }
          end
        rescue Debci::Job::InvalidStatusFile => exc
          puts("E: #{exc}")
        end
      end
    end

    desc 'history-json PACKAGE', 'Generate package history in JSON'
    def history_json(pkg)
      history = Debci::Job.history(pkg, suite, arch)
      puts JSON.pretty_generate(history.as_json)
    end

    desc 'latest-json PACKAGE', 'Generate JSON for the most recent test job that has no pin-packages'
    def latest_json(pkg)
      latest = Debci::Job.history(pkg, suite, arch).last
      puts JSON.pretty_generate(latest.as_json)
    end

    no_commands do
      def arch
        Debci.config.arch
      end

      def suite
        Debci.config.suite
      end
    end
  end
end

Debci::JobCLI.start
