Add cmd vanity
git-svn-id: file:///srv/svn/repo/toyohime/trunk@5 922d331f-388e-da47-97a9-ad700dc0b8b9
This commit is contained in:
parent
a4c7e90a02
commit
6ffdafd573
80
cmd/vanity/main.go
Normal file
80
cmd/vanity/main.go
Normal file
@ -0,0 +1,80 @@
|
||||
package main // import "kkn.fi/vanity/cmd/vanity"
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"kkn.fi/vanity"
|
||||
)
|
||||
|
||||
var (
|
||||
domainFlag = flag.String("d", "", "http domain name")
|
||||
portFlag = flag.Int("p", 80, "http server port")
|
||||
confFlag = flag.String("c", "", "configuration file")
|
||||
)
|
||||
|
||||
func usage() {
|
||||
fmt.Fprintf(os.Stderr, "usage: vanity -d domain -c vanity.conf [-p 80]\n")
|
||||
flag.PrintDefaults()
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
log.SetPrefix("vanity: ")
|
||||
log.SetFlags(0)
|
||||
|
||||
if *domainFlag == "" || *confFlag == "" {
|
||||
usage()
|
||||
}
|
||||
|
||||
c, err := os.Open(*confFlag)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
conf, err := readConfig(c)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
server := vanity.Server{domainFlag, conf}
|
||||
port := fmt.Sprintf(":%v", *portFlag)
|
||||
log.Fatal(http.ListenAndServe(port, server))
|
||||
}
|
||||
|
||||
func readConfig(r io.Reader) (map[vanity.Path]vanity.Package, error) {
|
||||
conf := make(map[vanity.Path]vanity.Package, 0)
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
fields := strings.Fields(scanner.Text())
|
||||
switch len(fields) {
|
||||
case 0:
|
||||
continue
|
||||
case 3:
|
||||
pack := vanity.Package{
|
||||
Path: parsePath(fields[0]),
|
||||
VCSSystem: fields[1],
|
||||
VCSURL: fields[2],
|
||||
}
|
||||
conf[vanity.Path(fields[0])] = pack
|
||||
default:
|
||||
return conf, errors.New("configuration error: " + scanner.Text())
|
||||
}
|
||||
}
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
func parsePath(p string) string {
|
||||
c := strings.Index(p[1:], "/")
|
||||
if c == -1 {
|
||||
return p
|
||||
}
|
||||
return p[:c+1]
|
||||
}
|
43
cmd/vanity/main_test.go
Normal file
43
cmd/vanity/main_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"kkn.fi/vanity"
|
||||
)
|
||||
|
||||
func TestParseConfig(t *testing.T) {
|
||||
config := `/gist git https://github.com/kare/gist
|
||||
/vanity git https://github.com/kare/vanity
|
||||
/vanity/cmd git https://github.com/kare/vanity
|
||||
|
||||
/vanity/cmd/vanity git https://github.com/kare/vanity`
|
||||
|
||||
expected := map[vanity.Path]vanity.Package{
|
||||
"/gist": {"/gist", "git", "https://github.com/kare/gist"},
|
||||
"/vanity": {"/vanity", "git", "https://github.com/kare/vanity"},
|
||||
"/vanity/cmd": {"/vanity", "git", "https://github.com/kare/vanity"},
|
||||
"/vanity/cmd/vanity": {"/vanity", "git", "https://github.com/kare/vanity"},
|
||||
}
|
||||
conf, err := readConfig(strings.NewReader(config))
|
||||
if err != nil {
|
||||
t.Fatalf("unexcepted configuration error: %v", err)
|
||||
}
|
||||
if len(conf) != 4 {
|
||||
t.Fatalf("expecting config for %v packages, but got %v", 4, len(conf))
|
||||
}
|
||||
for p, c := range expected {
|
||||
if c != conf[p] {
|
||||
t.Fatalf("expected %v but got %v", c, conf[p])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBrokenConfig(t *testing.T) {
|
||||
config := "/gist git"
|
||||
_, err := readConfig(strings.NewReader(config))
|
||||
if err == nil {
|
||||
t.Fatal("broken configuration did not return error")
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user