#!/usr/bin/env ruby lib = File.expand_path('../lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'git/semaphore' require 'slop' options = Slop.parse(help: true) do |o| o.banner = <<~"BANNER" Usage: git-semaphore [options] v#{Git::Semaphore::VERSION} Options: BANNER o.on '--version', 'Print the version' do dependencies = [ "rugged v#{Rugged::VERSION}", "#{RUBY_ENGINE} #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}", ].join(', ') puts "#{Git::Semaphore::NAME} v#{Git::Semaphore::VERSION} (#{dependencies})" exit(0) end o.on '--help', 'Print help message' do puts o exit(0) end o.on '--refresh', 'Bypass cached information and refresh from API' o.on '--projects', 'List all projects and their current status' o.on '--browse', 'Open the project on https://semaphoreci.com/' o.on '--rebuild', 'Rebuild last revision for the current branch' o.on '--result', 'Latest un-cached build result for the current branch' o.on '--settings', 'Display most relevant settings' o.on '--internals', 'Display all internal settings' o.on '--branches', 'List all branches for the current project' o.on '--status', 'List the build status for the current branch' o.on '--history', 'List the build history for the current branch' o.on '--information', 'List the commit information for the last build' o.on '--log', 'List the build log for the last build' end project = if (git_repo = Git::Semaphore.git_repo) Git::Semaphore::Project.from_repo(git_repo) else Git::Semaphore::Project.from_config(ENV) end class SafeProject # rubocop:disable Style/MethodMissingSuper # rubocop:disable Style/MissingRespondToMissing def initialize(project) @project = project end def result return unless @project.exist? @project.branches(true) # refresh status = @project.status(true) # refresh status.fetch('result', nil) end def method_missing(name, *args, &block) if @project.exist? @project.send(name, *args, &block) else { error: "Project #{@project.full_name} doesn't exist on semaphoreci.com" } end end def respond_to?(name, include_all = false) @project.respond_to? name, include_all end # rubocop:enable Style/MissingRespondToMissing # rubocop:enable Style/MethodMissingSuper end project = SafeProject.new(project) refresh = options.refresh? case when options.projects? then puts Git::Semaphore::Project.all(refresh).to_json when options.browse? then puts project.browse.to_json when options.rebuild? then puts project.rebuild.to_json when options.result? then (result = project.result) && puts(result) when options.settings? then puts project.settings.to_json when options.internals? then puts project.internals.to_json when options.branches? then puts project.branches(refresh).to_json when options.status? then puts project.status(refresh).to_json when options.history? then puts project.history(refresh).to_json when options.information? then puts project.information(refresh).to_json when options.log? then puts project.log(refresh).to_json else puts options end # That's all Folks!