From 9f472db239eb54608a00c3d1ae3b806d0ba5e18a Mon Sep 17 00:00:00 2001 From: aqua Date: Thu, 29 Jul 2021 18:50:09 +0300 Subject: Implement -host flag --- gemini.go | 29 ++++++++++++++++------------- gemini_test.go | 2 +- main.go | 20 +++++++++++++++----- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/gemini.go b/gemini.go index 79a7c97..1cdef8b 100644 --- a/gemini.go +++ b/gemini.go @@ -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()) } diff --git a/main.go b/main.go index a4c2190..f5d241c 100644 --- a/main.go +++ b/main.go @@ -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) -- cgit v1.2.1