Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4 from realestate-com-au/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
diist committed Mar 1, 2016
2 parents 3f87366 + 79c23dc commit 7aa6690
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 195 deletions.
113 changes: 0 additions & 113 deletions lib/tagfish/docker_api.rb

This file was deleted.

24 changes: 24 additions & 0 deletions lib/tagfish/docker_registry_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'tagfish/docker_registry_v1_client'
require 'tagfish/docker_registry_v2_client'

module Tagfish
module DockerRegistryClient

def self.for(*args)
[DockerRegistryV2Client, DockerRegistryV1Client].each do |client_class|
begin
return client_class.new(*args)
rescue APIVersionError
end
end
raise APIVersionError, "API version unrecognized!"
end

class AuthenticationError < StandardError
end

class APIVersionError < StandardError
end

end
end
54 changes: 54 additions & 0 deletions lib/tagfish/docker_registry_v1_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'tagfish/docker_registry_vboth_client'

module Tagfish
class DockerRegistryV1Client < DockerRegistryVbothClient

def api_version
'v1'
end

def search(keyword)
APICall.new(search_uri(keyword)).get_json(http_auth)
end

def tag_names
tag_map.tag_names
end

def tag_map
tags_list = tags_api(tags)
Tagfish::Tags.new(tags_list)
end

private

def tags
APICall.new(tags_uri).get_json(http_auth)
end

def tags_api(api_response_data)
case api_response_data
when Hash
api_response_data
when Array
api_response_data.reduce({}) do |images, tag|
images.merge({tag["name"] => tag["layer"]})
end
else
raise "unexpected type #{api_response_data.class}"
end
end

def ping_uri
"#{base_uri}/v1/_ping"
end

def search_uri(keyword)
"#{base_uri}/v1/search?q=#{keyword}"
end

def tags_uri
"#{base_uri}/v1/repositories/#{docker_uri.repository}/tags"
end
end
end
59 changes: 59 additions & 0 deletions lib/tagfish/docker_registry_v2_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'tagfish/docker_registry_vboth_client'

module Tagfish
class DockerRegistryV2Client < DockerRegistryVbothClient

def api_version
'v2'
end

def catalog
APICall.new(catalog_uri).get_json(http_auth)
end

def tag_names
tags["tags"]
end

def tag_map
Tagfish::Tags.new(tags_logic)
end

private

def tags
APICall.new(tags_uri).get_json(http_auth)
end

def hash(tag)
APICall.new(hash_uri(tag)).get_json(http_auth)
end

def tags_logic
if tag_names.nil?
abort("No Tags found for this repository")
end

tags_with_hashes = tag_names.inject({}) do |dict, tag|
dict[tag] = hash(tag)["fsLayers"][0]["blobSum"]
dict
end
end

def ping_uri
"#{base_uri}/v2/"
end

def catalog_uri
"#{base_uri}/v2/_catalog"
end

def tags_uri
"#{base_uri}/v2/#{docker_uri.repository}/tags/list"
end

def hash_uri(tag)
"#{base_uri}/v2/#{docker_uri.repository}/manifests/#{tag}"
end
end
end
32 changes: 32 additions & 0 deletions lib/tagfish/docker_registry_vboth_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'net/http'
require 'json'
require 'tagfish/docker_uri'
require 'tagfish/api_call'
require 'tagfish/tags'

module Tagfish
class DockerRegistryVbothClient

attr_accessor :docker_uri
attr_accessor :http_auth

def initialize(docker_uri)
@docker_uri = docker_uri
code = APICall.new(ping_uri).response_code
if code == 401
@http_auth = DockerHttpAuth.new(docker_uri.registry)
code = APICall.new(ping_uri).response_code(http_auth)
end
if code == 401
raise DockerRegistryClient::AuthenticationError, "Please `docker login <REGISTRY>` and try again"
elsif code != 200
raise DockerRegistryClient::APIVersionError, "Not recognized"
end
end

def base_uri
"#{docker_uri.protocol}#{docker_uri.registry}"
end

end
end
1 change: 0 additions & 1 deletion lib/tagfish/docker_uri.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'tagfish/docker_http_auth'
require 'tagfish/docker_api'

module Tagfish
class DockerURI
Expand Down
16 changes: 7 additions & 9 deletions lib/tagfish/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@
module Tagfish
class Tags

def initialize(tags)
@tags = tags
attr_reader :tag_map

def initialize(tag_map)
@tag_map = tag_map
end

def tag_names
@tags.keys.sort
tag_map.keys.sort
end

def latest_tag
tag_names.select do |tag_name|
(@tags[tag_name] == @tags["latest"]) && (tag_name != 'latest')
tag_names.detect do |tag_name|
(tag_map[tag_name] == tag_map["latest"]) && (tag_name != "latest")
end
end

def latest_tag_to_s
latest_tag.empty? ? nil : latest_tag[0]
end

end
end
29 changes: 13 additions & 16 deletions lib/tagfish/tags_command.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "tagfish/tags_logic"
require "tagfish/docker_uri"
require "tagfish/docker_registry_client"

module Tagfish
class TagsCommand < Clamp::Command
Expand All @@ -8,23 +8,20 @@ class TagsCommand < Clamp::Command
option ["-s", "--short"], :flag, "only return tag, not full image path"

def execute
tags_only = latest? ? false : true

docker_uri = DockerURI.parse(repository)
docker_api = DockerAPI.new(docker_uri)
tags = TagsLogic.find_tags_by_repository(docker_api, tags_only)

begin
tags_found = latest? ? tags.latest_tag : tags.tag_names
rescue Exception => e
puts e.message
return
end

if tags_found.size == 0
puts "ERROR: No image explicitly tagged in this Repository, " +
"only `latest` tag available."
return
docker_api = DockerRegistryClient.for(docker_uri)

if latest?
tags = docker_api.tag_map
latest_tag = tags.latest_tag
if latest_tag.nil?
signal_error "No image explicitly tagged in this Repository, " +
"only `latest` tag available."
end
tags_found = [latest_tag]
else
tags_found = docker_api.tag_names
end

pretty_tags = tags_found.map do |tag_name|
Expand Down
Loading

0 comments on commit 7aa6690

Please sign in to comment.