aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 3cfacacb164d82192c4804a4738e97235f081d34 (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
87
88
89
package main

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

func usage() {
	fmt.Printf("Usage: %s [options] url\n", os.Args[0])
	flag.PrintDefaults()
}

func create_output(path string) (*os.File, error) {
	if path == "" {
		return os.Stdout, nil
	}
	return os.Create(path)
}

func read_requests(path string) ([]string, error) {
	if path == "" {
		return nil, nil
	}

	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()

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

	return lines, scanner.Err()
}

func main() {
	flags := struct {
		host   string
		port   int
		input  string
		output string
	}{
		host:   "",
		port:   gemini.GeminiPort,
		input:  "",
		output: "",
	}

	flag.Usage = usage
	flag.StringVar(&flags.host, "host", flags.host, "server host name")
	flag.IntVar(&flags.port, "port", flags.port, "server port number")
	flag.StringVar(&flags.input, "i", flags.input, "read urls from file")
	flag.StringVar(&flags.output, "o", flags.output, "write output to path")
	flag.Parse()

	requests, err := read_requests(flags.input)
	if err != nil {
		log.Panic(err)
	}
	requests = append(requests, flag.Args()...)
	if len(requests) == 0 {
		flag.Usage()
		os.Exit(1)
	}

	output, err := create_output(flags.output)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Got %d requests\n", len(requests))
	for _, request := range requests {
		log.Println(request)
		if conn, err := gemini.NewGeminiConnFromRequest(request, flags.port); err != nil {
			log.Print(err)
		} else if err := conn.Get(gemini.FormatRequest(request), output); err != nil {
			log.Print(err)
		}
	}

}