Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Move PNG generation library code into a separate package and remove autodetection of dataDir #13

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ go: 1.2

script:
- go test -v ./...
- go run main.go --vendor=foo --status=bar --color=blue /dev/null
24 changes: 15 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/droundy/goopt"
"github.com/gittip/img.shields.io/shield"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to change per badges/shields#90 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than having a shield subfolder, what do you think of flipping it around? Go stdlib and a few other places have a cmd folder.

so github.com/badges/buckler/cmd/buckler is the folder containing the buckler CLI.
so we could make github.com/badges/buckler the library import (stdlib actually has a pkg/ subfolder as well, but meh).

Also, let's just call this buckler (not shield).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm!

)

var (
Expand All @@ -23,7 +24,7 @@ var (
lastModifiedStr = lastModified.UTC().Format(http.TimeFormat)
oneYear = time.Duration(8700) * time.Hour

staticPath, _ = resourcePaths()
staticPath = "static"
)

func shift(s []string) ([]string, string) {
Expand All @@ -35,7 +36,7 @@ func invalidRequest(w http.ResponseWriter, r *http.Request) {
http.Error(w, "bad request", 400)
}

func parseFileName(name string) (d Data, err error) {
func parseFileName(name string) (d shield.Data, err error) {
imageName := wsReplacer.Replace(name)
imageParts := strings.Split(imageName, "-")

Expand Down Expand Up @@ -72,12 +73,12 @@ func parseFileName(name string) (d Data, err error) {
}

cp := newParts[2][0 : len(newParts[2])-4]
c, err := getColor(cp)
c, err := shield.GetColor(cp)
if err != nil {
return
}

d = Data{newParts[0], newParts[1], c}
d = shield.Data{newParts[0], newParts[1], c}
return
}

Expand Down Expand Up @@ -106,7 +107,7 @@ func buckle(w http.ResponseWriter, r *http.Request) {
w.Header().Add("cache-control", "public")
w.Header().Add("last-modified", lastModifiedStr)

makePngShield(w, d)
shield.PNG(w, d)
}

const basePkg = "github.com/gittip/img.shields.io"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to change per badges/shields#90 (comment)

Expand All @@ -132,11 +133,11 @@ func cliMode(vendor string, status string, color string, args []string) {
}

if vendor != "" {
c, err := getColor(color)
c, err := shield.GetColor(color)
if err != nil {
fatal("Invalid color: " + color)
}
d := Data{vendor, status, c}
d := shield.Data{vendor, status, c}

name := fmt.Sprintf("%s-%s-%s.png", revWsReplacer.Replace(vendor),
revWsReplacer.Replace(status), color)
Expand All @@ -158,7 +159,7 @@ func cliMode(vendor string, status string, color string, args []string) {
}
}

makePngShield(f, d)
shield.PNG(f, d)
return
}

Expand All @@ -174,7 +175,7 @@ func cliMode(vendor string, status string, color string, args []string) {
if err != nil {
fatal(err.Error())
}
makePngShield(f, d)
shield.PNG(f, d)
}
}

Expand Down Expand Up @@ -202,6 +203,9 @@ func main() {

goopt.Usage = usage

// common options
dataDir := goopt.String([]string{"-d", "--data-dir"}, "data", "data dir containing base PNG files and font")

// server mode options
host := goopt.String([]string{"-h", "--host"}, hostEnv, "host ip address to bind to")
port := goopt.Int([]string{"-p", "--port"}, p, "port to listen on")
Expand All @@ -214,6 +218,8 @@ func main() {

args := goopt.Args

shield.LoadResources(*dataDir)

// if any of the cli args are given, or positional args remain, assume cli
// mode.
if len(args) > 0 || *vendor != "" || *status != "" || *color != "" {
Expand Down
44 changes: 0 additions & 44 deletions resources.go

This file was deleted.

9 changes: 4 additions & 5 deletions png.go → shield/png.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package shield

import (
"errors"
Expand Down Expand Up @@ -64,8 +64,7 @@ const (
ip = 4
)

func init() {
_, dataPath := resourcePaths()
func LoadResources(dataPath string) {
fi, err := os.Open(filepath.Join(dataPath, "edge.png"))
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -120,7 +119,7 @@ func hexColor(c string) (color.RGBA, bool) {
return color.RGBA{uint8(r), uint8(g), uint8(b), 255}, true
}

func getColor(cs string) (c color.RGBA, err error) {
func GetColor(cs string) (c color.RGBA, err error) {
c, ok := Colors[cs]
if !ok {
c, ok = hexColor(cs)
Expand Down Expand Up @@ -177,7 +176,7 @@ func buildMask(mask *image.RGBA, imageWidth int, tmpl image.Image, imgOp draw.Op
draw.Draw(mask, r, tmpl, sr.Min, imgOp)
}

func makePngShield(w io.Writer, d Data) {
func PNG(w io.Writer, d Data) {
// render text to determine how wide the image has to be
// we leave 6 pixels at the start and end, and 3 for each in the middle
v, vw := renderString(d.Vendor, c)
Expand Down
36 changes: 27 additions & 9 deletions png_test.go → shield/png_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package shield

import (
"bytes"
Expand All @@ -7,9 +7,20 @@ import (
"testing"
)

func init() {
LoadResources("../data")
}

func TestRenderString(t *testing.T) {
i, _ := os.Open("test/vendor.data")
e, _ := ioutil.ReadAll(i)
i, err := os.Open("testdata/vendor.data")
if err != nil {
t.Fatal(err)
}

e, err := ioutil.ReadAll(i)
if err != nil {
t.Fatal(err)
}

r, _ := renderString("Vendor", c)
if !bytes.Equal(r.Pix, e) {
Expand All @@ -18,12 +29,19 @@ func TestRenderString(t *testing.T) {
}

// simple regression test
func TestMakePngShield(t *testing.T) {
i, _ := os.Open("test/use-buckler-blue.png")
e, _ := ioutil.ReadAll(i)
func TestPNG(t *testing.T) {
i, err := os.Open("testdata/use-buckler-blue.png")
if err != nil {
t.Fatal(err)
}

e, err := ioutil.ReadAll(i)
if err != nil {
t.Fatal(err)
}

var b bytes.Buffer
makePngShield(&b, Data{"use", "buckler", Blue})
PNG(&b, Data{"use", "buckler", Blue})
if !bytes.Equal(b.Bytes(), e) {
t.Error("render string 'Vendor' bytes not equal")
}
Expand All @@ -36,9 +54,9 @@ func BenchmarkRenderString(b *testing.B) {
}
}

func BenchmarkMakePngShield(b *testing.B) {
func BenchmarkPNG(b *testing.B) {
d := Data{"test", "output", Blue}
for i := 0; i < b.N; i++ {
makePngShield(ioutil.Discard, d)
PNG(ioutil.Discard, d)
}
}
File renamed without changes
Binary file added shield/testdata/use-buckler-blue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.