aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go34
1 files changed, 24 insertions, 10 deletions
diff --git a/main.go b/main.go
index 22d9fbf..a4c2190 100644
--- a/main.go
+++ b/main.go
@@ -7,35 +7,49 @@ import (
"os"
)
-var host = ""
-var port = GeminiPort
-
func usage() {
fmt.Printf("Usage: %s [options] url\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
+ flags := struct {
+ host string
+ port int
+ output string
+ }{
+ host: "",
+ port: GeminiPort,
+ output: "",
+ }
+
flag.Usage = usage
- flag.StringVar(&host, "h", host, "server host name")
- flag.StringVar(&host, "host", host, "server host name")
- flag.IntVar(&port, "p", port, "server port number")
- flag.IntVar(&port, "port", port, "server port number")
+ flag.StringVar(&flags.host, "h", flags.host, "server host name")
+ flag.IntVar(&flags.port, "p", flags.port, "server port number")
+ flag.StringVar(&flags.output, "o", flags.output, "write output to path")
flag.Parse()
if flag.NArg() != 1 {
- fmt.Printf("Got %d arguments, expected 1\n", flag.NArg())
+ fmt.Printf("Error: Got %d arguments, expected 1\n", flag.NArg())
flag.Usage()
os.Exit(1)
}
- conn, err := NewGeminiConn(flag.Arg(0), port)
+ conn, err := NewGeminiConn(flag.Arg(0), flags.port)
if err != nil {
log.Fatalf("Error parsing hostname: %s\n", err.Error())
}
log.Printf("connecting to: %s\n", conn.host)
- err = conn.Get(FormatRequest(flag.Arg(0)), os.Stdout)
+ var output = os.Stdout
+ if flags.output != "" {
+ output, err = os.Create(flags.output)
+ if err != nil {
+ log.Fatalf("Error creating file: %s\n", err.Error())
+ }
+ }
+
+ err = conn.Get(FormatRequest(flag.Arg(0)), output)
if err != nil {
log.Fatalf("Error: %s\n", err.Error())
}