From 512489a7735c65f5890d03e2bc597ad2b160f3e1 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Thu, 29 Aug 2013 02:39:41 -0700 Subject: [PATCH 1/6] Move PNG generation library code into the ./shield package and remove autodetection of dataDir --- main.go | 24 ++++++++++------- resources.go | 44 ------------------------------- png.go => shield/png.go | 9 +++---- png_test.go => shield/png_test.go | 14 ++++++---- 4 files changed, 28 insertions(+), 63 deletions(-) delete mode 100644 resources.go rename png.go => shield/png.go (96%) rename png_test.go => shield/png_test.go (80%) diff --git a/main.go b/main.go index 2775c8b..1747dd0 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "./shield" "github.com/droundy/goopt" ) @@ -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.Init(*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..af814c1 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 Init(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 80% rename from png_test.go rename to shield/png_test.go index e768489..1d84361 100644 --- a/png_test.go +++ b/shield/png_test.go @@ -1,4 +1,4 @@ -package main +package shield import ( "bytes" @@ -7,6 +7,10 @@ import ( "testing" ) +func init() { + Init("../data") +} + func TestRenderString(t *testing.T) { i, _ := os.Open("test/vendor.data") e, _ := ioutil.ReadAll(i) @@ -18,12 +22,12 @@ func TestRenderString(t *testing.T) { } // simple regression test -func TestMakePngShield(t *testing.T) { +func TestPNG(t *testing.T) { i, _ := os.Open("test/use-buckler-blue.png") e, _ := ioutil.ReadAll(i) 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 +40,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) } } From 8bdc66930b4a05ec7e0be4c315cd9a29f442c2a8 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Sun, 5 Jan 2014 04:18:31 -0800 Subject: [PATCH 2/6] Rename func Init -> LoadResources --- shield/png.go | 2 +- shield/png_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shield/png.go b/shield/png.go index af814c1..d23f9ac 100644 --- a/shield/png.go +++ b/shield/png.go @@ -64,7 +64,7 @@ const ( ip = 4 ) -func Init(dataPath string) { +func LoadResources(dataPath string) { fi, err := os.Open(filepath.Join(dataPath, "edge.png")) if err != nil { log.Fatal(err) diff --git a/shield/png_test.go b/shield/png_test.go index 1d84361..4eca450 100644 --- a/shield/png_test.go +++ b/shield/png_test.go @@ -8,7 +8,7 @@ import ( ) func init() { - Init("../data") + LoadResources("../data") } func TestRenderString(t *testing.T) { From 92d0d08bf436c464f754365b789ca7418a7823f0 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Sun, 5 Jan 2014 04:22:50 -0800 Subject: [PATCH 3/6] Use absolute import, not relative import --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 1747dd0..76cfcc8 100644 --- a/main.go +++ b/main.go @@ -11,8 +11,8 @@ import ( "strings" "time" - "./shield" "github.com/droundy/goopt" + "github.com/gittip/img.shields.io/shield" ) var ( From ef9fa118e0aead59c3234a36ec8d845db003c5a6 Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Sun, 5 Jan 2014 04:25:02 -0800 Subject: [PATCH 4/6] Check for errors in test code, and move test data to shield/testdata to fix errors --- shield/png_test.go | 22 ++++++++++++++---- .../testdata/use-buckler-blue.out.png | Bin shield/testdata/use-buckler-blue.png | Bin 0 -> 1578 bytes {test => shield/testdata}/vendor.data | Bin 4 files changed, 18 insertions(+), 4 deletions(-) rename test/use-buckler-blue.png => shield/testdata/use-buckler-blue.out.png (100%) mode change 100644 => 100755 create mode 100644 shield/testdata/use-buckler-blue.png rename {test => shield/testdata}/vendor.data (100%) diff --git a/shield/png_test.go b/shield/png_test.go index 4eca450..d60e824 100644 --- a/shield/png_test.go +++ b/shield/png_test.go @@ -12,8 +12,15 @@ func init() { } 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) { @@ -23,8 +30,15 @@ func TestRenderString(t *testing.T) { // simple regression test func TestPNG(t *testing.T) { - i, _ := os.Open("test/use-buckler-blue.png") - e, _ := ioutil.ReadAll(i) + 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 PNG(&b, Data{"use", "buckler", Blue}) 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 0000000000000000000000000000000000000000..17e55583ba523f80d5281d46e8bfc1e483b2ba8d GIT binary patch literal 1578 zcmV+_2G#kAP)4f{675*;ZvmAM(PjKme$r#lUn)Vk2bAoXrrd> z4_iS~(*iZLF~%m1w3ukxXsJ}vG)B=9C=rW*6rpm{?rqmJjLPM?EB2u z!|8rOVN~-K!ua@jSP7w!f^xXGSTMM}VzD4eQrI_={yCg5sDi;D!riD+rZ;O;1U|@5 zP}m=}J@1Ug`b3k${_c7^78})%?N=qsGQ!xo{$jcr zv4Yb6uuY(moDQ?ujI69I)Ya9ozSHT1&*wv1TN~_lJF>I0K~t`=u@OBzJ#rlK_|GCe`o{zK@|PjVem7*FoB4MVU#V6dPPPmIvV>s3OY8!45j+Z=F|b4m zIP(`(IJ`0`B8QB+hk)z8VvVMG`V29%bTf;_sRQmGh0 zbbQ~weQ-D&AO#x-4je#yeEb8`hsHrI2q;-&h5FtgPCk>2jf9LuJ%ozVF zr6SR+!78f?RhyUK(1t}Y`R^*ur%*~D5SY5mWZ&=i!|U}jn^vnub#*l(AtNILDJdza ztgK{2wYRr30;z)LRSG$^An@3J@Hj@fC>X^NJnLA7%8P>}$l}y|*)xWq$H)4wW}5NN zXQxor#)v`;)))vJ_)uV<;>v#O~d@QBzZc^73-@_4Tn- zqUl3%?bS>AcZP|)(6^X zElEpDLw$WcA|oT2V)pFWgRNV)qM)Dv`}glBdCB4Q>C=o1p66jS8rkm*1VbMCL~#PI z!hh{XRxj42C!q00H{9fMMqMPLH7a;S39FK9=>55it#9q_Lw>p)-_#AjpyA;mwT52& zP<1UXG_|AVy<#+e+KkS--ut8k(b7EtO6!T*%eZp09|9pc!fMBJ79)u^Kf$)Fd?it& zPe(@wu3o*0s;VlCj*haFpr8f#@ZrO7xm@hxWo2bdJ$v`=#gQXN*xab7D3p|xpsTBE z<}OkogcKQ_BvJ=SPCINS43CUp`*+RgSKIJYOFM47mW3fwOmB4FWS67s-#B&-r{3O< z!d0ZM5b-WMc#+9L(nX`*Ys1H1)sot@7Hf|-O$DOgFJKo*t8d@hh9Qp#6DhV&e%?ry z`6dVnJ=cA3`T6-Xq(YTL(}F647F8N`wOY*zEwxi}C;=vuiK&SaGdw&Di^alzjb3t7U0HxLhwRw1yv1U9#(s c00030|8& Date: Sun, 5 Jan 2014 04:29:03 -0800 Subject: [PATCH 5/6] Fix call (renamed to LoadResources) --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 76cfcc8..fbc315b 100644 --- a/main.go +++ b/main.go @@ -218,7 +218,7 @@ func main() { args := goopt.Args - shield.Init(*dataDir) + shield.LoadResources(*dataDir) // if any of the cli args are given, or positional args remain, assume cli // mode. From d16566811fe274144cbfcd462d6a2471b41f106a Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Sun, 5 Jan 2014 04:43:18 -0800 Subject: [PATCH 6/6] Test running the img.shields.io program --- .travis.yml | 1 + 1 file changed, 1 insertion(+) 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