diff options
-rw-r--r-- | main.go | 75 |
1 files changed, 33 insertions, 42 deletions
@@ -4,32 +4,37 @@ import ( "bufio" "crypto/tls" "flag" - "fmt" "gemini" - "log" "os" ) func usage() { - fmt.Printf("Usage: %s [options] url\n", os.Args[0]) + println("Usage: " + os.Args[0] + " [options] url(s)") flag.PrintDefaults() } -func create_output(path string) (*os.File, error) { - if path == "" { - return os.Stdout, nil +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 + } } - return os.Create(path) } -func read_requests(path string) ([]string, error) { +func list_requests(path string) []string { if path == "" { - return nil, nil + return nil } file, err := os.Open(path) if err != nil { - return nil, err + println("Error reading input: " + err.Error()) + return nil } defer file.Close() @@ -39,56 +44,42 @@ func read_requests(path string) ([]string, error) { lines = append(lines, scanner.Text()) } - return lines, scanner.Err() + return lines } func main() { - flags := struct { - host string - port int - input string - output string - }{ - host: "", - port: gemini.GeminiPort, - input: "", - output: "", - } - + var ( + f_host string + f_port int = gemini.GeminiPort + f_input string + f_output string + ) 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.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, err := read_requests(flags.input) - if err != nil { - log.Panic(err) - } - requests = append(requests, flag.Args()...) + requests := append(list_requests(f_input), flag.Args()...) if len(requests) == 0 { + println("No work to do") flag.Usage() os.Exit(1) } - output, err := create_output(flags.output) - if err != nil { - log.Fatal(err) - } - + output := create_output(f_output) conf := tls.Config{ MinVersion: tls.VersionTLS12, InsecureSkipVerify: true, } - log.Printf("Got %d requests\n", len(requests)) for _, request := range requests { - log.Println(request) - if conn, err := gemini.Parse(request, flags.host, flags.port); err != nil { - log.Print(err) + 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 { - log.Print(err) + println(err.Error()) } } |