Skip to content

Commit

Permalink
Merge pull request #42 from /issues/27/custom-sort
Browse files Browse the repository at this point in the history
Sort by DateTime
  • Loading branch information
ivanilves authored Sep 16, 2017
2 parents 372a40c + b1ffc55 commit 4fac660
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 32 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

# Compiled binary
# Compiled binaries
/lstags
/lstags.*

# Vendored dependencies
/vendor/*
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ install:
- make dep

script:
- make unit-test
- make package-test
- make integration-test
- make lint
Expand Down
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ package-test:
integration-test:
go test -integration -v

lint: ERRORS=$(shell find . -name "*.go" ! -path "./vendor/*" | xargs -i golint {})
lint: ERRORS:=$(shell find . -name "*.go" ! -path "./vendor/*" | xargs -i golint {})
lint: fail-on-errors

vet: ERRORS=$(shell find . -name "*.go" ! -path "./vendor/*" | xargs -i go tool vet {})
vet: ERRORS:=$(shell find . -name "*.go" ! -path "./vendor/*" | xargs -i go tool vet {})
vet: fail-on-errors

fail-on-errors:
Expand All @@ -34,3 +34,13 @@ fail-on-errors:

build:
go build

build-linux: GOOS:=linux
build-linux:
GOOS=${GOOS} go build -o lstags.${GOOS}

build-darwin: GOOS:=darwin
build-darwin:
GOOS=${GOOS} go build -o lstags.${GOOS}

xbuild: build-linux build-darwin
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ func main() {
suicide(err)
}

sortKeys, joinedTags := tag.Join(registryTags, localTags)
sortedKeys, names, joinedTags := tag.Join(registryTags, localTags)

const format = "%-12s %-45s %-15s %-25s %s\n"
fmt.Printf(format, "<STATE>", "<DIGEST>", "<(local) ID>", "<Created At>", "<TAG>")
for _, sortKey := range sortKeys {
tg := joinedTags[sortKey]
for _, key := range sortedKeys {
name := names[key]

tg := joinedTags[name]

fmt.Printf(
format,
Expand Down
5 changes: 5 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func (tr MockedTokenResponse) ExpiresIn() int {
}

func TestGetAuthorization(t *testing.T) {
flag.Parse()
if *runIntegrationTests {
t.SkipNow()
}

const expected = "Mocked 8c896241e2774507489849ab1981e582"

authorization := getAuthorization(MockedTokenResponse{})
Expand Down
2 changes: 1 addition & 1 deletion tag/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func FetchTags(repo string) (map[string]*tag.Tag, error) {

tg.SetCreated(imageSummary.Created)

tags[tg.SortKey()] = tg
tags[tg.GetName()] = tg
}
}

Expand Down
2 changes: 1 addition & 1 deletion tag/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func FetchTags(registry, repo, authorization string, concurrency int) (map[strin

tt.SetCreated(dr.Created)

tags[tt.SortKey()] = tt
tags[tt.GetName()] = tt
}
}

Expand Down
44 changes: 27 additions & 17 deletions tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Tag struct {

// SortKey returns a sort key
func (tg *Tag) SortKey() string {
return tg.name
return tg.GetCreatedKey() + tg.name
}

// GetName gets tag name
Expand Down Expand Up @@ -112,9 +112,9 @@ func New(name, digest string) (*Tag, error) {
nil
}

func calculateState(sortKey string, registryTags, localTags map[string]*Tag) string {
r, definedInRegistry := registryTags[sortKey]
l, definedLocally := localTags[sortKey]
func calculateState(name string, registryTags, localTags map[string]*Tag) string {
r, definedInRegistry := registryTags[name]
l, definedLocally := localTags[name]

if definedInRegistry && !definedLocally {
return "ABSENT"
Expand All @@ -137,35 +137,45 @@ func calculateState(sortKey string, registryTags, localTags map[string]*Tag) str

// Join joins local tags with ones from registry, performs state processing and returns:
// * sorted slice of sort keys
// * joined map of *tag.Tag
func Join(registryTags, localTags map[string]*Tag) ([]string, map[string]*Tag) {
// * joined map of [sortKey]name
// * joined map of [name]*Tag
func Join(registryTags, localTags map[string]*Tag) ([]string, map[string]string, map[string]*Tag) {
sortedKeys := make([]string, 0)
names := make(map[string]string)
joinedTags := make(map[string]*Tag)

for sortKey := range registryTags {
for name := range registryTags {
sortKey := registryTags[name].SortKey()

sortedKeys = append(sortedKeys, sortKey)
joinedTags[sortKey] = registryTags[sortKey]
names[sortKey] = name

joinedTags[name] = registryTags[name]

ltg, defined := localTags[sortKey]
ltg, defined := localTags[name]
if defined {
joinedTags[sortKey].SetImageID(ltg.GetImageID())
joinedTags[name].SetImageID(ltg.GetImageID())
} else {
joinedTags[sortKey].SetImageID("n/a")
joinedTags[name].SetImageID("n/a")
}
}

for sortKey := range localTags {
_, defined := registryTags[sortKey]
for name := range localTags {
_, defined := registryTags[name]
if !defined {
sortKey := localTags[name].SortKey()

sortedKeys = append(sortedKeys, sortKey)
joinedTags[sortKey] = localTags[sortKey]
names[sortKey] = name

joinedTags[name] = localTags[name]
}
}

for sortKey, jtg := range joinedTags {
for name, jtg := range joinedTags {
jtg.SetState(
calculateState(
sortKey,
name,
registryTags,
localTags,
),
Expand All @@ -174,5 +184,5 @@ func Join(registryTags, localTags map[string]*Tag) ([]string, map[string]*Tag) {

sort.Strings(sortedKeys)

return sortedKeys, joinedTags
return sortedKeys, names, joinedTags
}
8 changes: 4 additions & 4 deletions tag/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func localTags() map[string]*Tag {
func TestJoinLength(t *testing.T) {
const expected = 4

_, tags := Join(registryTags(), localTags())
_, _, tags := Join(registryTags(), localTags())

c := len(tags)

Expand All @@ -129,7 +129,7 @@ func TestJoinDigest(t *testing.T) {
"v1.2": "sha256:7f7f94f26d23f7aca80a33732161af068f9f62fbe0e824a58cf3a39d209cfa77",
}

_, tags := Join(registryTags(), localTags())
_, _, tags := Join(registryTags(), localTags())

for name, digest := range expected {
if tags[name].GetDigest() != digest {
Expand All @@ -151,7 +151,7 @@ func TestJoinImageID(t *testing.T) {
"v1.2": "4c4ebb9614ef",
}

_, tags := Join(registryTags(), localTags())
_, _, tags := Join(registryTags(), localTags())

for name, imageID := range expected {
if tags[name].GetImageID() != imageID {
Expand All @@ -173,7 +173,7 @@ func TestJoinState(t *testing.T) {
"v1.2": "PRESENT",
}

_, tags := Join(registryTags(), localTags())
_, _, tags := Join(registryTags(), localTags())

for name, state := range expected {
if tags[name].GetState() != state {
Expand Down

0 comments on commit 4fac660

Please sign in to comment.