Module: Sandman

Defined in:
lib/sandman.rb,
lib/sandman/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.add_key_to_provider(p, key = "", title = "") ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# 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..new_key(p., {: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 = "") ⇒ Object



117
118
119
120
121
122
# 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_keysObject



91
92
93
94
95
96
97
# 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

.configObject



79
80
81
# File 'lib/sandman.rb', line 79

def self.config
  @config
end

.configure(opts = {}) ⇒ Object



67
68
69
70
71
72
73
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

.initObject



49
50
51
52
53
# 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) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/sandman.rb', line 83

def self.keys_for_provider(p)
  if p.kind_of? Github::Client
    p.users.keys.all(p.).body
  elsif p.kind_of? BitBucket::Client
    p.users..keys(p.)
  end
end

.merge_keys_to_servicesObject



99
100
101
# File 'lib/sandman.rb', line 99

def self.merge_keys_to_services

end

.remove_key_from_provider(p, key) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# 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..delete_key(p., key[:pk])
      puts "Key removed from BitBucket"
    end
  rescue Exception => e
    puts e.to_s
  end
end

.remove_key_from_providers(key = "") ⇒ Object



138
139
140
141
142
143
144
145
146
147
# 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) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/sandman.rb', line 149

def self.show_keys(long = false)
  @providers.each do |p|
    title = p.class.name.split("::").first + ": " + p.
    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) ⇒ Object



75
76
77
# File 'lib/sandman.rb', line 75

def self.write_config(path_to_yaml_file)
  IO.write(path_to_yaml_file, @config.to_yaml)
end