aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: f95bfe9643cf176bdcfff1830302c3f91bfc996d (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main

import (
	"bufio"
	"crypto/tls"
	"flag"
	"gemini"
	"os"
)

func usage() {
	println("Usage: " + os.Args[0] + " [options] url(s)")
	flag.PrintDefaults()
}

func create_output(path string) *os.File {
	switch path {
	case "":
		return os.Stdout
	default:
		if output, err := os.Create(path); err != nil {
			panic(err)
		} else {
			return output
		}
	}
}

func list_requests(path string) []string {
	if path == "" {
		return nil
	}

	file, err := os.Open(path)
	if err != nil {
		println("Error reading input: " + err.Error())
		return nil
	}
	defer file.Close()

	var lines []string
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}

	return lines
}

func main() {
	var (
		f_host   string
		f_port   int = gemini.GeminiPort
		f_input  string
		f_output string
	)
	flag.Usage = usage
	flag.StringVar(&f_host, "host", f_host, "server host name")
	flag.IntVar(&f_port, "port", f_port, "server port number")
	flag.StringVar(&f_input, "i", f_input, "read urls from file")
	flag.StringVar(&f_output, "o", f_output, "write output to path")
	flag.Parse()

	requests := append(list_requests(f_input), flag.Args()...)
	if len(requests) == 0 {
		println("No work to do")
		flag.Usage()
		os.Exit(1)
	}

	output := create_output(f_output)
	conf := tls.Config{
		MinVersion:         tls.VersionTLS12,
		InsecureSkipVerify: true,
	}

	for _, request := range requests {
		if conn, err := gemini.Parse(request, f_host, f_port); err != nil {
			println("Error parsing request: " + request)
			println(err.Error())
		} else if err := conn.Get(&conf, output); err != nil {
			println(err.Error())
		}
	}

}