Browse Source

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 14 years ago
parent
commit
8b932fcfbb
6 changed files with 54 additions and 33 deletions
  1. 4 4
      .gitignore
  2. 0 0
      cmd/Makefile
  3. 0 11
      cmd/audio/main.go
  4. 0 7
      cmd/image/Makefile
  5. 0 11
      cmd/image/main.go
  6. 50 0
      cmd/main.go

+ 4 - 4
.gitignore

@@ -22,9 +22,9 @@ _testmain.go
 *.exe
  
 # Generated test captchas
-*.png
+cmd/*.png
+cmd/*.wav
 
-# Program
-cmd/image/captcha
-cmd/audio/captcha
+# Programs
+cmd/captcha
 originals/generate

+ 0 - 0
cmd/audio/Makefile → cmd/Makefile


+ 0 - 11
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)
-}

+ 0 - 7
cmd/image/Makefile

@@ -1,7 +0,0 @@
-include $(GOROOT)/src/Make.inc
-
-TARG=captcha
-GOFILES=\
-	main.go
-
-include $(GOROOT)/src/Make.cmd

+ 0 - 11
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)
-}

+ 50 - 0
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)
+}