diff options
-rw-r--r-- | gemini.go | 29 | ||||
-rw-r--r-- | gemini_test.go | 2 | ||||
-rw-r--r-- | main.go | 20 |
3 files changed, 32 insertions, 19 deletions
@@ -26,26 +26,29 @@ type GeminiConn struct { config *tls.Config } -func NewGeminiConn(request string, port int) (*GeminiConn, error) { - if !strings.HasPrefix(request, "gemini://") { - request = "gemini://" + request - } - - u, err := url.Parse(request) - if err != nil { - return nil, err - } - +func NewGeminiConn(host string, port int) *GeminiConn { conf := tls.Config{ MinVersion: tls.VersionTLS12, InsecureSkipVerify: true, } - if strings.Contains(u.Host, ":") { - return &GeminiConn{u.Host, &conf}, nil + if strings.Contains(host, ":") { + return &GeminiConn{host, &conf} } else { - return &GeminiConn{u.Host + ":" + strconv.Itoa(port), &conf}, nil + return &GeminiConn{host + ":" + strconv.Itoa(port), &conf} + } +} + +func NewGeminiConnFromRequest(request string, port int) (*GeminiConn, error) { + if !strings.HasPrefix(request, GeminiPrefix) { + request = GeminiPrefix + request + } + + u, err := url.Parse(request) + if err != nil { + return nil, err } + return NewGeminiConn(u.Host, port), nil } func (c *GeminiConn) Get(request []byte, out *os.File) error { diff --git a/gemini_test.go b/gemini_test.go index e029c04..efa34b9 100644 --- a/gemini_test.go +++ b/gemini_test.go @@ -17,7 +17,7 @@ func TestNewGeminiConn(t *testing.T) { } for _, table := range tables { - conn, err := NewGeminiConn(table.url, table.port) + conn, err := NewGeminiConnFromRequest(table.url, table.port) if err != nil { t.Fatalf("NewGeminiConn error: %s", err.Error()) } @@ -16,16 +16,19 @@ func main() { flags := struct { host string port int + input string output string }{ host: "", port: GeminiPort, + input: "", 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.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() if flag.NArg() != 1 { @@ -34,9 +37,16 @@ func main() { os.Exit(1) } - conn, err := NewGeminiConn(flag.Arg(0), flags.port) - if err != nil { - log.Fatalf("Error parsing hostname: %s\n", err.Error()) + var conn *GeminiConn + var err error + + if flags.host != "" { + conn = NewGeminiConn(flags.host, flags.port) + } else { + conn, err = NewGeminiConnFromRequest(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) |