aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: a4c21908833d80025a5a4fd365b00bb71dbaecd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
)

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(&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("Error: Got %d arguments, expected 1\n", flag.NArg())
		flag.Usage()
		os.Exit(1)
	}

	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)

	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())
	}
}