Class: Docker::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/docker/search.rb,
lib/docker/search/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Search

Returns a new instance of Search.



22
23
24
25
26
27
28
# File 'lib/docker/search.rb', line 22

def initialize(config)
  @hosts = {}
  config['auths'].each do |host, c|
    user, pass = c["auth"].unpack('m')[0].split(':', 2)
    @hosts[host] = [user, pass]
  end
end

Class Method Details

.runObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/docker/search.rb', line 10

def self.run
  config_path = "~/.docker/config.json"
  action = :search
  opt = OptionParser.new
  def opt.version; VERSION end
  opt.on('-c config_file_path'){|v| config_path = v}
  opt.on('-l', 'list up registries'){action = :list}
  opt.parse! ARGV
  search = new JSON.load(open(File.expand_path config_path).read)
  search.send action, *ARGV
end

Instance Method Details

#listObject



54
55
56
57
58
# File 'lib/docker/search.rb', line 54

def list
  @hosts.each_with_index do |(k,v),i|
    puts "#{i + 1}. #{k}"
  end
end

#search(*names) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/docker/search.rb', line 30

def search(*names)
  @hosts.each do |(k,v)|
    puts "Searching #{k} ...".blue
    uri = URI(k)
    case uri.path
    when %r{/v1\b}
      STDERR.puts "v1 is not supported.".yellow
    else
      http = Net::HTTP.new k, 443
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      http.start do
        req = Net::HTTP::Get.new "/v2/_catalog"
        req.basic_auth *v
        res = http.request req
        json = JSON.load res.body
        repos = json['repositories']
        names.each{|n| repos = repos.select{|r| r.index n}}
        puts repos
      end
    end
  end
end