Skip to content

Commit

Permalink
Merge pull request #13 from Dalee/formatting
Browse files Browse the repository at this point in the history
Preparing to release 0.2.0
  • Loading branch information
arkady-emelyanov authored Feb 17, 2017
2 parents 5183f53 + da274b1 commit f86df59
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 53 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ install:
- yarn --pure-lockfile
- yarn global add codecov
- go get -u github.com/modocache/gover
- go get -u github.com/golang/lint/golint
- go get -u github.com/Masterminds/glide
- glide install

script:
- go list -f '"go test -v -covermode=atomic -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"' ./pkg/... | xargs -I % sh -c %
- go list -f '"go test -covermode=atomic -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"' ./pkg/... | xargs -I % sh -c %
- gover ./ ./coverage.txt
- ./node_modules/.bin/jest --coverage

after_success:
- cat ./coverage.txt
- bash <(curl -s https://codecov.io/bash) -f ./coverage.txt
- codecov
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ install:
go get -u github.com/modocache/gover
go get -u github.com/golang/lint/golint
go get -u github.com/Masterminds/glide
go get -u github.com/gordonklaus/ineffassign
go get -u github.com/client9/misspell/cmd/misspell
npm install
glide install


# build and prepare new version
Expand All @@ -26,7 +30,13 @@ test-frontend:

# test backend only
test-backend:
golint ./bin/ ./pkg/
go test -v ./pkg/...
golint -set_exit_status ./pkg/... ./bin/...
ineffassign ./
misspell -error README.md ./pkg/**/* ./bin/**/*
gofmt -d -s -e ./bin/ ./pkg/
go test -covermode=atomic ./pkg/...

format-backend:
gofmt -d -w -s -e ./bin/ ./pkg/

.PHONY: test docker
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ Software is licensed under the Apache License, Version 2.0. See LICENSE for the
* [glide](https://github.com/Masterminds/glide)
* make

Setting up:

Setting up developer dependencies:
```bash
$ npm install && glide install
$ make install
```

You can also you yarn instead:
```bash
$ yarn --pure-lockfile && glide install
$ yarn --pure-lockfile
```

Run server:
Expand Down
12 changes: 6 additions & 6 deletions bin/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main

import (
"errors"
"flag"
"fmt"
"gopkg.in/macaron.v1"
"hitman/pkg/controllers"
"hitman/pkg/application"
"flag"
"hitman/pkg/controllers"
"os"
"errors"
"fmt"
)

func main() {
Expand All @@ -28,14 +28,14 @@ func main() {
}

app := application.New(registryURL)
if app.Registry.IsValidUrl() == false {
if app.Registry.IsValidURL() == false {
panic(errors.New("Registry /v2/ request failed, check URL"))
}

m := macaron.New()
m.Use(macaron.Static("public", macaron.StaticOptions{
SkipLogging: true,
IndexFile: "index.html",
IndexFile: "index.html",
}))
m.Use(macaron.Renderer())
m.Map(app)
Expand Down
10 changes: 5 additions & 5 deletions pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ import (
)

type (
// App is base definition for application
App struct {
Registry *registry.Registry
}
)

//
func New(registryUrl string) *App {
// New creates new Application
func New(registryURL string) *App {
return &App{
Registry: registry.New(registryUrl),
Registry: registry.New(registryURL),
}
}

//
// RunForever will run web ui and background app loop
func (app *App) RunForever(web *macaron.Macaron) {
go func(app *App) {
app.loop()
}(app)
web.Run() // here we block..
}

//
func (app *App) loop() {
for {
// Any background tasks?
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"hitman/pkg/application"
)

// return image tree
// IndexHandler return repository tree
func IndexHandler(ctx *macaron.Context, app *application.App) {
tree, err := app.Registry.GetTree(ctx.Query("path"))
if err != nil {
Expand All @@ -15,7 +15,7 @@ func IndexHandler(ctx *macaron.Context, app *application.App) {
}
}

// return list of tags
// ImageHandler return contents for a given repository
func ImageHandler(ctx *macaron.Context, app *application.App) {
tags, err := app.Registry.GetImageDigestList(ctx.Query("path"))
if err != nil {
Expand All @@ -25,7 +25,7 @@ func ImageHandler(ctx *macaron.Context, app *application.App) {
}
}

// delete digest for image
// DeleteHandler delete digest from registry
func DeleteHandler(ctx *macaron.Context, app *application.App) {
err := app.Registry.DeleteImageDigest(ctx.Query("path"), ctx.Query("tag"))
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/registry/api.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package registry

import (
"errors"
"fmt"
"github.com/forestgiant/sliceutil"
"strings"
)

func (r *Registry) IsValidUrl() bool {
// IsValidURL checks that provided registryURL is pointed to a registry v2
func (r *Registry) IsValidURL() bool {
resp, err := r.reqHead("/") // actually is /v2/ request
if err != nil {
return false
Expand All @@ -25,7 +25,7 @@ func (r *Registry) IsValidUrl() bool {
return true
}

// return tree of repositories and images registered in registry
// GetTree returns tree of repositories and images registered in registry
func (r *Registry) GetTree(path string) (*RepositoryLeaf, error) {
resp, err := r.getCatalog()
if err != nil {
Expand All @@ -43,15 +43,15 @@ func (r *Registry) GetTree(path string) (*RepositoryLeaf, error) {
if path != "" {
leaf := tree.findLeafByPath(path)
if leaf == nil {
err = errors.New(fmt.Sprintf("Path: %s doesn't exists in tree", path))
err = fmt.Errorf("Path: %s doesn't exists in tree", path)
}
return leaf, err
}

return tree, nil
}

// return image information (tags are grouped by digest)
// GetImageDigestList return image information (tags are grouped by digest)
func (r *Registry) GetImageDigestList(path string) (*RepositoryDigestList, error) {
resp, err := r.getTagList(path)
if err != nil {
Expand Down Expand Up @@ -92,7 +92,7 @@ func (r *Registry) GetImageDigestList(path string) (*RepositoryDigestList, error
return tagList, nil
}

// delete digest from repository
// DeleteImageDigest delete digest from repository
func (r *Registry) DeleteImageDigest(path, tag string) error {
return r.deleteDigest(path, tag)
}
19 changes: 11 additions & 8 deletions pkg/registry/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,31 @@ import (
)

type (
// Registry represent registry client
Registry struct {
url string // base URL
}

// RepositoryDigest represent repository image digest
RepositoryDigest struct {
Path string `json:"path"`
Name string `json:"name"`
TagList []string `json:"tags"`
}

// RepositoryDigestList represent list of RepositoryDigest
RepositoryDigestList struct {
Children []*RepositoryDigest `json:"children"`
}

// RepositoryImage represent repository image
RepositoryImage struct {
Path string `json:"path"`
Name string `json:"name"`
Parent *RepositoryLeaf `json:"-"`
}

// RepositoryLeaf represent any look-like-a-folder structure within Registry
RepositoryLeaf struct {
Name string `json:"name"`
Path string `json:"path"`
Expand All @@ -35,14 +40,12 @@ type (
}
)

//
func New(registryUrl string) *Registry {
registryUrl = strings.TrimRight(registryUrl, "/")
registryUrl = fmt.Sprintf("%s/v2", registryUrl)
// New will create new Registry client
func New(registryURL string) *Registry {
registryURL = strings.TrimRight(registryURL, "/")
registryURL = fmt.Sprintf("%s/v2", registryURL)

registry := &Registry{
url: registryUrl,
return &Registry{
url: registryURL,
}

return registry
}
32 changes: 16 additions & 16 deletions pkg/registry/communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package registry

import (
"encoding/json"
"errors"
"fmt"
"gopkg.in/resty.v0"
"sort"
Expand Down Expand Up @@ -67,8 +66,9 @@ func (r *Registry) deleteDigest(name, digest string) error {
return err
}

if deleteResp.StatusCode() != 202 {
return errors.New("Deleting not accepted by registry")
code := deleteResp.StatusCode()
if code != 202 {
return fmt.Errorf("Deletion request declined by registry with code: %d", code)
}

return nil
Expand All @@ -83,38 +83,38 @@ func (r *Registry) getManifestDigest(name, tag string) (string, error) {

digest := resp.Header().Get("Docker-Content-Digest")
if digest == "" {
return "", errors.New(fmt.Sprintf("Empty digest for: %s:%s", name, tag))
return "", fmt.Errorf("Empty digest for: %s:%s", name, tag)
}

return digest, nil
}

// DELETE helper
func (r *Registry) reqDelete(requestUri string) (*resty.Response, error) {
requestUri = strings.TrimLeft(requestUri, "/")
requestUrl := fmt.Sprintf("%s/%s", r.url, requestUri)
func (r *Registry) reqDelete(requestURI string) (*resty.Response, error) {
requestURI = strings.TrimLeft(requestURI, "/")
requestURL := fmt.Sprintf("%s/%s", r.url, requestURI)

return resty.R().
SetHeader("Accept", "application/vnd.docker.distribution.manifest.v2+json").
Delete(requestUrl)
Delete(requestURL)
}

// HEAD helper
func (r *Registry) reqHead(requestUri string) (*resty.Response, error) {
requestUri = strings.TrimLeft(requestUri, "/")
requestUrl := fmt.Sprintf("%s/%s", r.url, requestUri)
func (r *Registry) reqHead(requestURI string) (*resty.Response, error) {
requestURI = strings.TrimLeft(requestURI, "/")
requestURL := fmt.Sprintf("%s/%s", r.url, requestURI)

return resty.R().
SetHeader("Accept", "application/vnd.docker.distribution.manifest.v2+json").
Head(requestUrl)
Head(requestURL)
}

// GET helper
func (r *Registry) reqGet(requestUri string) (*resty.Response, error) {
requestUri = strings.TrimLeft(requestUri, "/")
requestUrl := fmt.Sprintf("%s/%s", r.url, requestUri)
func (r *Registry) reqGet(requestURI string) (*resty.Response, error) {
requestURI = strings.TrimLeft(requestURI, "/")
requestURL := fmt.Sprintf("%s/%s", r.url, requestURI)

return resty.R().
SetHeader("Accept", "application/vnd.docker.distribution.manifest.v2+json").
Get(requestUrl)
Get(requestURL)
}

0 comments on commit f86df59

Please sign in to comment.