class GithubBackup::Config

Constants

DEFAULTS

Attributes

backup_root[R]
gitconfig_path[R]
token[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/github-backup/config.rb, line 10
def initialize(options = {})
  @backup_root    = options.fetch(:backup_root, nil)    || Dir.pwd
  @gitconfig_path = options.fetch(:gitconfig_path, nil) || DEFAULTS[:gitconfig_path]
  @token          = options.fetch(:token)               { default_token }
end

Public Instance Methods

==(other) click to toggle source
# File lib/github-backup/config.rb, line 16
def ==(other)
  backup_root == other.backup_root &&
    gitconfig_path == other.gitconfig_path &&
      token == other.token
end

Private Instance Methods

default_token() click to toggle source
# File lib/github-backup/config.rb, line 24
def default_token
  config = read_gitconfig
  if config.key?('github')
    config['github'].fetch('token', nil)
  end
end
read_gitconfig() click to toggle source
# File lib/github-backup/config.rb, line 31
def read_gitconfig
  config = {}
  group = nil

  return config unless File.exist?(gitconfig_path)

  File.foreach(gitconfig_path) do |line|
    line.strip!
    if line[0] != ?# && line =~ /\S/
      if line =~ /^\[(.*)\]$/
        group = $1
        config[group] ||= {}
      else
        key, value = line.split("=").map { |v| v.strip }
        config[group][key] = value
      end
    end
  end
  config
end