#!/usr/bin/env ruby # frozen_string_literal: true require 'gli' require 'faraday-http-cache' require_relative '../lib/tools' module Bin class Commands extend GLI::App program_desc 'GitHub project info tool.' default_command :help switch %i[d debug], desc: 'Enable debug-level logging.', default_value: false switch %i[no-cache], desc: 'Disable local cache (.cache).', default_value: false switch %i[q quiet], desc: "Don't show progress bars.", default_value: false flag %i[t token], desc: 'Optional GitHub token.', default_value: ENV.fetch('GITHUB_API_TOKEN', nil) flag %i[vcr-cassette-name], desc: 'Offline VCR cassette.' commands_from File.expand_path('commands', __dir__) pre do |global_options, _command, _options, _args| GitHub::Progress.enabled = global_options['quiet'] ? false : true if global_options['vcr-cassette-name'] require 'webmock' WebMock.enable! require 'vcr' VCR.configure do |config| config.cassette_library_dir = File.expand_path(File.join(__FILE__, '../../spec/fixtures')) config.hook_into :webmock config.default_cassette_options = { record: :new_episodes } if global_options['token'] config.filter_sensitive_data('') do global_options['token'] end end end VCR.insert_cassette global_options['vcr-cassette-name'] end Octokit.auto_paginate = true client_options = {} client_options = { access_token: global_options[:token] } if global_options.key?(:token) unless global_options['no-cache'] stack = Faraday::RackBuilder.new do |builder| cache_store = ActiveSupport::Cache.lookup_store(:file_store, File.expand_path(File.join(__dir__, '..', '.cache'))) logger = global_options['debug'] ? Logger.new($stdout) : nil builder.use Faraday::HttpCache, serializer: Marshal, shared_cache: false, store: cache_store, logger: logger builder.use Octokit::Response::RaiseError builder.use Faraday::CacheControl builder.adapter Faraday.default_adapter end Octokit.middleware = stack end post do |global_options, _command, _options, _args| VCR.eject_cassette if global_options['vcr-cassette-name'] end $github = Octokit::Client.new(client_options) ENV['GLI_DEBUG'] = 'true' if global_options['debug'] true end end end $LOAD_PATH.push File.expand_path(__dir__) exit Bin::Commands.run(ARGV)