From 0f4bedb6c518408604b55fb07c1ba098314822f2 Mon Sep 17 00:00:00 2001 From: James Mills Date: Mon, 12 Jun 2017 23:56:04 -0700 Subject: [PATCH] Add CLI client --- cmd/gopher/main.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 cmd/gopher/main.go diff --git a/cmd/gopher/main.go b/cmd/gopher/main.go new file mode 100644 index 0000000..7b69242 --- /dev/null +++ b/cmd/gopher/main.go @@ -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)) + } +}