Implement a single captcha test command. The new command replaces old test command-line utilities from cmd/image and cmd/audio directories. It can now generate both image and audio captchas, and accepts a few useful flags. Edit .gitignore to reflect changes.
Dmitry Chestnykh dmitry@codingrobots.com
Sat, 23 Apr 2011 21:17:23 +0200
6 files changed,
54 insertions(+),
33 deletions(-)
M
.gitignore
→
.gitignore
@@ -22,9 +22,9 @@
*.exe # Generated test captchas -*.png +cmd/*.png +cmd/*.wav -# Program -cmd/image/captcha -cmd/audio/captcha +# Programs +cmd/captcha originals/generate
D
cmd/audio/main.go
@@ -1,11 +0,0 @@
-package main - -import ( - "github.com/dchest/captcha" - "os" -) - -func main() { - c, _ := captcha.NewRandomAudio(captcha.StdLength) - c.WriteTo(os.Stdout) -}
D
cmd/image/Makefile
@@ -1,7 +0,0 @@
-include $(GOROOT)/src/Make.inc - -TARG=captcha -GOFILES=\ - main.go - -include $(GOROOT)/src/Make.cmd
D
cmd/image/main.go
@@ -1,11 +0,0 @@
-package main - -import ( - "github.com/dchest/captcha" - "os" -) - -func main() { - img, _ := captcha.NewRandomImage(captcha.StdLength, captcha.StdWidth, captcha.StdHeight) - img.WriteTo(os.Stdout) -}
A
cmd/main.go
@@ -0,0 +1,50 @@
+package main + +import ( + "flag" + "fmt" + "github.com/dchest/captcha" + "io" + "log" + "os" +) + +var ( + flagImage = flag.Bool("i", true, "output image captcha") + flagAudio = flag.Bool("a", false, "output audio captcha") + flagLen = flag.Int("len", captcha.StdLength, "length of captcha") + flagImgW = flag.Int("width", captcha.StdWidth, "image captcha width") + flagImgH = flag.Int("height", captcha.StdHeight, "image captcha height") +) + +func usage() { + fmt.Fprintf(os.Stderr, "usage: captcha [flags] filename\n") + flag.PrintDefaults() +} + +func main() { + flag.Parse() + fname := flag.Arg(0) + if fname == "" { + usage() + os.Exit(1) + } + f, err := os.Create(fname) + if err != nil { + log.Fatalf("%s", err) + } + defer f.Close() + var w io.WriterTo + var ns []byte + switch { + case *flagAudio: + w, ns = captcha.NewRandomAudio(*flagLen) + case *flagImage: + w, ns = captcha.NewRandomImage(*flagLen, *flagImgW, *flagImgH) + } + _, err = w.WriteTo(f) + if err != nil { + log.Fatalf("%s", err) + } + fmt.Println(ns) +}