Skip to content

Commit

Permalink
Merge pull request #78 from ivanilves/more-work-on-structure
Browse files Browse the repository at this point in the history
Refactor to make code stop smelling like shit!
  • Loading branch information
vonrabbe authored Oct 18, 2017
2 parents 738071f + e255763 commit dd0099e
Show file tree
Hide file tree
Showing 16 changed files with 200 additions and 261 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ before_script:
- git remote set-url --push origin https://${GITHUB_TOKEN}@github.com/ivanilves/lstags.git

script:
- make unit-test
- make package-test
- make integration-test
- make shell-test
Expand Down
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ prepare:
dep:
dep ensure -v

test: unit-test package-test

unit-test:
go test -v
test: package-test integration-test

package-test:
@find \
Expand All @@ -20,7 +17,7 @@ package-test:
| xargs -i sh -c "pushd {}; go test -v || exit 1; popd"

integration-test:
go test -integration -v
go test -v

env:
env
Expand Down
1 change: 1 addition & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type TokenResponse interface {
Method() string
Token() string
ExpiresIn() int
AuthHeader() string
}

func parseAuthHeader(headers http.Header) (string, string, error) {
Expand Down
5 changes: 5 additions & 0 deletions auth/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func (tr TokenResponse) ExpiresIn() int {
return 0
}

// AuthHeader returns contents of the Authorization HTTP header
func (tr TokenResponse) AuthHeader() string {
return tr.Method() + " " + tr.Token()
}

func getTokenFromHeader(header string) string {
fields := strings.Split(header, " ")

Expand Down
5 changes: 5 additions & 0 deletions auth/bearer/bearer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func (tr TokenResponse) ExpiresIn() int {
return tr.E
}

// AuthHeader returns contents of the Authorization HTTP header
func (tr TokenResponse) AuthHeader() string {
return tr.Method() + " " + tr.Token()
}

func decodeTokenResponse(data io.ReadCloser) (*TokenResponse, error) {
tr := TokenResponse{}

Expand Down
5 changes: 5 additions & 0 deletions auth/none/none.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func (tr TokenResponse) ExpiresIn() int {
return 0
}

// AuthHeader returns contents of the Authorization HTTP header
func (tr TokenResponse) AuthHeader() string {
return tr.Method() + " " + tr.Token()
}

// RequestToken does pretty little here...
func RequestToken() (*TokenResponse, error) {
return &TokenResponse{}, nil
Expand Down
30 changes: 30 additions & 0 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,33 @@ func isHostname(s string) bool {

return false
}

// GetRepoName gets full repository name
func GetRepoName(repository, registry string) string {
if registry == "registry.hub.docker.com" {
if strings.HasPrefix(repository, "library/") {
return strings.Replace(repository, "library/", "", 1)
}

return repository
}

if strings.HasPrefix(repository, registry) {
return repository
}

return registry + "/" + repository
}

// GetRepoPath gets repository path
func GetRepoPath(repository, registry string) string {
if !strings.Contains(repository, "/") {
return "library/" + repository
}

if strings.HasPrefix(repository, registry) {
return strings.Replace(repository, registry+"/", "", 1)
}

return repository
}
65 changes: 65 additions & 0 deletions docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,68 @@ func TestGetRegistry(t *testing.T) {
}
}
}

func TestGetRepoNameForDockerHub(t *testing.T) {
examples := map[string]string{
"library/nginx": "nginx",
"hype/cube": "hype/cube",
}

for input, expected := range examples {
output := GetRepoName(input, "registry.hub.docker.com")

if output != expected {
t.Fatalf(
"Got unexpected repo name: %s => %s\n* Expected: %s",
input,
output,
expected,
)
}
}
}

func TestGetRepoNameForPrivateRegistry(t *testing.T) {
const registry = "registry.nerd.io"

examples := map[string]string{
"empollon/nginx": registry + "/empollon/nginx",
"registry.nerd.io/hype/cube": registry + "/hype/cube",
}

for input, expected := range examples {
output := GetRepoName(input, registry)

if output != expected {
t.Fatalf(
"Got unexpected repo name: %s => %s\n* Expected: %s",
input,
output,
expected,
)
}
}
}

func TestGetRepoPath(t *testing.T) {
const registry = "registry.nerd.io"

examples := map[string]string{
"nginx": "library/nginx",
"registry.nerd.io/hype/cube": "hype/cube",
"observability/metrix": "observability/metrix",
}

for input, expected := range examples {
output := GetRepoPath(input, registry)

if output != expected {
t.Fatalf(
"Got unexpected repo path: %s => %s\n* Expected: %s",
input,
output,
expected,
)
}
}
}
Loading

0 comments on commit dd0099e

Please sign in to comment.