module Sandman

Constants

VERSION

Public Class Methods

add_key_to_provider(p, key = "", title = "") click to toggle source
# File lib/sandman.rb, line 103
def self.add_key_to_provider(p, key = "", title = "")
  begin
    if p.kind_of? Github::Client
      p.users.keys.create({:title => title, :key => key})
      puts "Key added to GitHub"
    elsif p.kind_of? BitBucket::Client
      p.users.account.new_key(p.login, {:label => title, :key => key})
      puts "Key added to BitBucket"
    end
  rescue Exception => e
    puts e.to_s
  end
end
add_key_to_providers(key = "", title = "") click to toggle source
# File lib/sandman.rb, line 117
def self.add_key_to_providers(key = "", title = "")
  puts "Adding #{key} to providers..."
  @providers.each do |p|
    Sandman.add_key_to_provider(p, key, title)
  end
end
all_keys() click to toggle source
# File lib/sandman.rb, line 91
def self.all_keys
  keys = Array.new
  @providers.each do |p|
    keys << Sandman.keys_for_provider(p)
  end
  keys.flatten
end
config() click to toggle source
# File lib/sandman.rb, line 79
def self.config
  @config
end
configure(opts = {}) click to toggle source
# File lib/sandman.rb, line 67
def self.configure(opts = {})
  @config = opts
  opts[:accounts].each do |acct|
    provider = Kernel.const_get(acct[:type]).new acct
    @providers << provider
  end
end
configure_with(path_to_yaml_file) click to toggle source
# File lib/sandman.rb, line 55
def self.configure_with(path_to_yaml_file)
  begin
    config = YAML::load(IO.read(path_to_yaml_file))
  rescue Errno::ENOENT
    log(:warning, "YAML config file not found, using defaults")
  rescue Psych::SyntaxError
    log(:warning, "YAML config file contains invalid syntax, using defaults")
  end

  configure(config)
end
filter_args(args) click to toggle source
# File lib/sandman.rb, line 9
def self.filter_args(args)
  Sandman.init
  if args.count > 1
    # try to automatically detect if we're being handed a file or key string
    key = args[1]
    if key.start_with?("ssh") == false && File.exist?(key)
      key = File.read(key)
    end
    if args.count > 2
      name = args[2]
    else
      name = `hostname`.strip
    end
    command = args[0]
    if command == "add"
      Sandman.add_key_to_providers(key, name)
    elsif command.start_with?("rem") || command.start_with?("del")
      Sandman.remove_key_from_providers(key)
    end
  elsif args.count == 1
    command = args[0]
    if command == "show"
      Sandman.show_keys
    elsif command == "showfull"
      Sandman.show_keys(true)
    elsif command == "sync" || command == "merge"
      #TODO: merge this stuff
    elsif command == "createconfig"
      if File.exist? "#{Dir.home}/.sandman"
        puts "Config file already exists"
      else
        @config[:accounts] << {:login => "", :password => "", :type => "Github"}
        @config[:accounts] << {:login => "", :password => "", :type => "BitBucket"}
        Sandman.write_config "#{Dir.home}/.sandman"
      end
    end
    
  end
end
init() click to toggle source
# File lib/sandman.rb, line 49
def self.init
  if File.exist? "#{Dir.home}/.sandman"
    Sandman.configure_with "#{Dir.home}/.sandman"
  end
end
keys_for_provider(p) click to toggle source
# File lib/sandman.rb, line 83
def self.keys_for_provider(p)
  if p.kind_of? Github::Client
    p.users.keys.all(p.login).body
  elsif p.kind_of? BitBucket::Client
    p.users.account.keys(p.login)
  end
end
merge_keys_to_services() click to toggle source
# File lib/sandman.rb, line 99
def self.merge_keys_to_services

end
remove_key_from_provider(p, key) click to toggle source
# File lib/sandman.rb, line 124
def self.remove_key_from_provider(p, key)
  begin
    if p.kind_of? Github::Client
      p.users.keys.delete(key[:id])
      puts "Key removed from GitHub"
    elsif p.kind_of? BitBucket::Client
      p.users.account.delete_key(p.login, key[:pk])
      puts "Key removed from BitBucket"
    end
  rescue Exception => e
    puts e.to_s
  end
end
remove_key_from_providers(key = "") click to toggle source
# File lib/sandman.rb, line 138
def self.remove_key_from_providers(key = "")
  puts "Removing #{key} from providers..."
  @providers.each do |p|
    Sandman.keys_for_provider(p).each do |k|
      if ( k[:key].split(" ")[0..1].join(" ").start_with?(key) || (k[:title].nil? == false && k[:title].downcase == key.downcase) || (k[:label].nil? == false && k[:label].downcase == key.downcase) )
        Sandman.remove_key_from_provider(p, k)
      end
    end
  end
end
show_keys(long = false) click to toggle source
# File lib/sandman.rb, line 149
def self.show_keys(long = false)
  @providers.each do |p|
    title = p.class.name.split("::").first + ": " + p.login
    puts title
    puts "=" * title.length
    Sandman.keys_for_provider(p).each do |k|
      if p.kind_of? Github::Client
        line = "\t#{k[:title]}: #{k[:key]}"
      elsif p.kind_of? BitBucket::Client
        line = "\t#{k[:label]}: #{k[:key]}"
      end
      puts (long ? line : line[0..75])
    end
    puts "\n"
  end
end
write_config(path_to_yaml_file) click to toggle source
# File lib/sandman.rb, line 75
def self.write_config(path_to_yaml_file)
  IO.write(path_to_yaml_file, @config.to_yaml)
end