Add CLI client

This commit is contained in:
James Mills 2017-06-12 23:56:04 -07:00
parent 3cea3ab217
commit 0f4bedb6c5
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
1 changed files with 56 additions and 0 deletions

56
cmd/gopher/main.go Normal file
View File

@ -0,0 +1,56 @@
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"github.com/prologic/go-gopher"
)
var (
json = flag.Bool("json", false, "display gopher directory as JSON")
)
func main() {
var uri string
flag.Parse()
if len(flag.Args()) == 1 {
uri = flag.Arg(0)
} else {
uri = "gopher://gopher.floodgap.com/"
}
res, err := gopher.Get(uri)
if err != nil {
log.Fatal(err)
}
if res.Body != nil {
contents, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
fmt.Print(string(contents))
} else {
var (
bytes []byte
err error
)
if *json {
bytes, err = res.Dir.ToJSON()
} else {
bytes, err = res.Dir.ToText()
}
if err != nil {
log.Fatal(err)
}
fmt.Println(string(bytes))
}
}