class Spaarti::Site

Site object, represents a group of repos

Public Class Methods

new(params = {}) click to toggle source
# File lib/spaarti/site.rb, line 26
def initialize(params = {})
  @options = DEFAULT_OPTIONS.dup.merge params
  load_config(params.include?(:config_file))
  return unless @options[:auth_file].is_a? String
  @options[:auth_file] = File.expand_path(@options[:auth_file])
end

Public Instance Methods

purge!() click to toggle source
# File lib/spaarti/site.rb, line 40
def purge!
  Dir.glob('**/.git').each do |git_dir|
    repo = Pathname.new(git_dir).dirname
    next if repos.any? { |x| x.parent_of(repo) }
    FileUtils.rmtree repo
  end
end
sync!() click to toggle source
# File lib/spaarti/site.rb, line 33
def sync!
  Dir.chdir(File.expand_path(@options[:base_path])) do
    repos.each(&:sync!)
    purge! if @options[:purge]
  end
end

Private Instance Methods

auth() click to toggle source
# File lib/spaarti/site.rb, line 69
def auth
  @auth ||= Octoauth.new({
    note: 'spaarti',
    scopes: %w[read:org read:user repo],
    file: @options[:auth_file],
    autosave: true,
    api_endpoint: @options[:api_endpoint]
  }.compact)
end
client() click to toggle source
# File lib/spaarti/site.rb, line 60
def client
  @client ||= Octokit::Client.new({
    access_token: auth.token,
    auto_paginate: true,
    default_media_type: 'application/vnd.github.moondragon+json',
    api_endpoint: @options[:api_endpoint]
  }.compact)
end
excluded(data) click to toggle source
# File lib/spaarti/site.rb, line 86
def excluded(data)
  @options[:exclude].any? do |key, patterns|
    patterns.any? { |pattern| data[key].match pattern }
  end
end
load_config(required = false) click to toggle source
# File lib/spaarti/site.rb, line 50
def load_config(required = false)
  @options[:config_file] = File.expand_path @options[:config_file]
  unless File.exist?(@options[:config_file])
    raise 'Conf file does not exist' if required
    return
  end
  config = File.open(@options[:config_file]) { |fh| YAML.safe_load fh.read }
  @options.merge! Cymbal.symbolize(config)
end
repos() click to toggle source
# File lib/spaarti/site.rb, line 79
def repos
  @repos ||= client.repos.map do |data|
    next if excluded(data)
    Repo.new data.to_h, client, @options
  end.compact
end