diff --git a/.travis.yml b/.travis.yml index c114ae0..baf1cf3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,3 +3,4 @@ go: 1.2 script: - go test -v ./... + - go run main.go --vendor=foo --status=bar --color=blue /dev/null diff --git a/main.go b/main.go index 2775c8b..fbc315b 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "time" "github.com/droundy/goopt" + "github.com/gittip/img.shields.io/shield" ) var ( @@ -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) { @@ -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, "-") @@ -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 } @@ -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" @@ -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) @@ -158,7 +159,7 @@ func cliMode(vendor string, status string, color string, args []string) { } } - makePngShield(f, d) + shield.PNG(f, d) return } @@ -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) } } @@ -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") @@ -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 != "" { diff --git a/resources.go b/resources.go deleted file mode 100644 index a957a8e..0000000 --- a/resources.go +++ /dev/null @@ -1,44 +0,0 @@ -package main - -import ( - "go/build" - "log" - "os" - "path/filepath" - - "bitbucket.org/kardianos/osext" -) - -// exists returns whether the given file or directory exists or not -func exists(path string) bool { - _, err := os.Stat(path) - if err == nil { - return true - } - if os.IsNotExist(err) { - return false - } - return false -} - -func resourcePaths() (staticPath string, dataPath string) { - base, err := osext.ExecutableFolder() - if err != nil { - log.Fatal("Could not read base dir") - } - - staticPath = filepath.Join(base, "static") - dataPath = filepath.Join(base, "data") - if exists(dataPath) && exists(staticPath) { - return - } - - p, err := build.Default.Import(basePkg, "", build.FindOnly) - if err != nil { - log.Fatal("Could not find package dir") - } - - staticPath = filepath.Join(p.Dir, "static") - dataPath = filepath.Join(p.Dir, "data") - return -} diff --git a/png.go b/shield/png.go similarity index 96% rename from png.go rename to shield/png.go index 59a7e79..d23f9ac 100644 --- a/png.go +++ b/shield/png.go @@ -1,4 +1,4 @@ -package main +package shield import ( "errors" @@ -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) @@ -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) @@ -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) diff --git a/png_test.go b/shield/png_test.go similarity index 55% rename from png_test.go rename to shield/png_test.go index e768489..d60e824 100644 --- a/png_test.go +++ b/shield/png_test.go @@ -1,4 +1,4 @@ -package main +package shield import ( "bytes" @@ -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) { @@ -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") } @@ -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) } } diff --git a/test/use-buckler-blue.png b/shield/testdata/use-buckler-blue.out.png old mode 100644 new mode 100755 similarity index 100% rename from test/use-buckler-blue.png rename to shield/testdata/use-buckler-blue.out.png diff --git a/shield/testdata/use-buckler-blue.png b/shield/testdata/use-buckler-blue.png new file mode 100644 index 0000000..17e5558 Binary files /dev/null and b/shield/testdata/use-buckler-blue.png differ diff --git a/test/vendor.data b/shield/testdata/vendor.data similarity index 100% rename from test/vendor.data rename to shield/testdata/vendor.data