commit 15bb2dfb0bf52b3a63cd3d412d6b702a5becb94b
parent dea1ec279276f0b9943633d0b9720c49ffaba72d
Author: phoebos <ben@bvnf.space>
Date: Wed, 8 Dec 2021 13:26:19 +0000
make separate function for gemgetting
Diffstat:
M | gm.go | | | 79 | +++++++++++++++++++++++++++++++++++++++++++++---------------------------------- |
1 file changed, 45 insertions(+), 34 deletions(-)
diff --git a/gm.go b/gm.go
@@ -13,35 +13,11 @@ import (
)
var (
- v *bool
+ v *bool
+ plain *bool
)
-// log prints timestamped diagnosis messages to stderr if verbose output is turned on.
-func log(msg ...interface{}) {
- if *v {
- fmt.Fprint(os.Stderr, "\033[32m")
- golog.Println(msg...)
- fmt.Fprint(os.Stderr, "\033[m")
- }
-}
-
-func die(msg ...interface{}) {
- fmt.Fprintf(os.Stderr, "\033[31m%s\033[m\n", fmt.Sprint(msg...))
- os.Exit(1)
-}
-
-func main() {
- port := flag.Int("p", 1965, "port number")
- plain := flag.Bool("plain", false, "print raw header and response (no formatting)")
- v = flag.Bool("v", false, "print more information to stderr")
- flag.Parse()
- // only accept one url
- if flag.NArg() != 1 {
- die("give one url")
- }
-
- u := flag.Args()[0]
-
+func gemget(u string, port int) error {
if !strings.HasPrefix(u, "gemini://") {
u = "gemini://" + u
}
@@ -51,7 +27,7 @@ func main() {
die(fmt.Sprintf("error parsing url (%s): %s", u, err.Error()))
}
- conn, err := tls.Dial("tcp", parsed.Host+fmt.Sprintf(":%d", *port), &tls.Config{InsecureSkipVerify: true})
+ conn, err := tls.Dial("tcp", parsed.Host+fmt.Sprintf(":%d", port), &tls.Config{InsecureSkipVerify: true})
if err != nil {
die("tls error: ", err)
}
@@ -65,7 +41,7 @@ func main() {
die(err)
}
fmt.Println(string(resp))
- os.Exit(0)
+ return nil
}
var mimetype string
@@ -83,6 +59,7 @@ func main() {
case '3':
// REDIRECT
log("redirecting to", statusline[3:])
+ return gemget(parsed.Host+"/"+statusline[3:], port)
case '6':
// CLIENT CERTIFICATE REQUIRED
default:
@@ -91,13 +68,47 @@ func main() {
die("error: ", statusline)
}
- resp, err := ioutil.ReadAll(bufio.NewReader(conn))
- if err != nil {
- die("error: ", err.Error())
+ // get body
+ var s string
+ for scanner.Scan() {
+ s = s + scanner.Text() + "\n"
}
- s := string(resp)
- if mimetype == "text/gemini" {
+
+ // remove final \n
+ s = s[:len(s)-1]
+
+ if strings.HasPrefix(mimetype, "text/gemini") {
s = *Prettify(&s)
}
fmt.Println(s)
+ return nil
+}
+
+// log prints timestamped diagnosis messages to stderr if verbose output is turned on.
+func log(msg ...interface{}) {
+ if *v {
+ fmt.Fprint(os.Stderr, "\033[32m")
+ golog.Println(msg...)
+ fmt.Fprint(os.Stderr, "\033[m")
+ }
+}
+
+func die(msg ...interface{}) {
+ fmt.Fprintf(os.Stderr, "\033[31m%s\033[m\n", fmt.Sprint(msg...))
+ os.Exit(1)
+}
+
+func main() {
+ port := flag.Int("p", 1965, "port number")
+ plain = flag.Bool("plain", false, "print raw header and response (no formatting)")
+ v = flag.Bool("v", false, "print more information to stderr")
+ flag.Parse()
+ // only accept one url
+ if flag.NArg() != 1 {
+ die("give one url")
+ }
+
+ u := flag.Args()[0]
+
+ gemget(u, *port)
}