apkdoc @ 08dcb18a941a80a3b37c14c8d64dd3a229e86ab1

ref: Better handle and outputs errors
  1diff --git a/main.go b/main.go
  2index 314ecc8ad5f43e74b6670920e62dd853335e1e7d..3f3f40cb1179c88d3381c9dfc48a7d929653e8ee 100644
  3--- a/main.go
  4+++ b/main.go
  5@@ -5,30 +5,54 @@ 	"archive/tar"
  6 	"bufio"
  7 	"compress/gzip"
  8 	"errors"
  9+	"fmt"
 10 	"io"
 11 	"net/http"
 12 	"os"
 13+	"strconv"
 14 
 15 	flag "github.com/spf13/pflag"
 16 )
 17 
 18 func main() {
 19-	url := flag.StringP("url", "u", "", "Url to the APKINDEX.tar.gz")
 20-	output := flag.StringP("output", "o", "index.md", "Output path")
 21-	templateType := flag.StringP("template-type", "p", "text", "Template system to be used, options: html, text")
 22-	templateFile := flag.StringP("template-file", "t", "text", "Template file to be used")
 23+	var (
 24+		url          = flag.StringP("url", "u", "", "Url to the APKINDEX.tar.gz")
 25+		output       = flag.StringP("output", "o", "index.md", "Output path")
 26+		templateType = flag.StringP("template-type", "p", "text", "Template system to be used, options: html, text")
 27+		templateFile = flag.StringP("template-file", "t", "text", "Template file to be used")
 28+	)
 29 	flag.Parse()
 30 
 31-	tarStream, err := fechIndex(*url)
 32+	err := run(
 33+		*url,
 34+		*output,
 35+		*templateType,
 36+		*templateFile,
 37+	)
 38 	if err != nil {
 39-		panic("Error fecthing the index: " + err.Error())
 40+		fmt.Println(err)
 41+		os.Exit(1)
 42+	}
 43+}
 44+
 45+func run(
 46+	url string,
 47+	output string,
 48+	templateType string,
 49+	templateFile string,
 50+
 51+) error {
 52+
 53+	tarStream, err := fechIndex(url)
 54+	if err != nil {
 55+		return fmt.Errorf("Error fecthing index file: %w", err)
 56 	}
 57 
 58 	defer tarStream.Close()
 59 
 60 	archive, err := gzip.NewReader(tarStream)
 61 	if err != nil {
 62-		panic("Error creating gzip reader: " + err.Error())
 63+		return fmt.Errorf("Error creating gzip reader: %w", err)
 64 	}
 65 
 66 	tr := tar.NewReader(archive)
 67@@ -36,7 +60,7 @@
 68 	for {
 69 		h, err := tr.Next()
 70 		if err != nil {
 71-			panic("Error reading next tar entry: " + err.Error())
 72+			return fmt.Errorf("Error reading next tar entry: %w", err)
 73 		}
 74 
 75 		if h.FileInfo().Name() == "APKINDEX" {
 76@@ -60,17 +84,21 @@ 			lines = append(lines, l)
 77 		}
 78 	}
 79 
 80-	outputFile, err := getOutputFile(*output)
 81+	outputFile, err := getOutputFile(output)
 82 	if err != nil {
 83-		panic("Error openning output file: " + err.Error())
 84+		return fmt.Errorf("Error openning output file: %w", err)
 85 	}
 86 
 87-	tmpl, err := GetTemplate(*templateType, *templateFile)
 88+	tmpl, err := GetTemplate(templateType, templateFile)
 89 	if err != nil {
 90-		panic("Error loading template file: " + err.Error())
 91+		return fmt.Errorf("Error loading template file: %w", err)
 92 	}
 93 
 94-	tmpl.Execute(outputFile, entries)
 95+	err = tmpl.Execute(outputFile, entries)
 96+	if err != nil {
 97+		return fmt.Errorf("Error executing template: %w", err)
 98+	}
 99+	return nil
100 }
101 
102 func getOutputFile(output string) (*os.File, error) {
103@@ -92,7 +120,7 @@ 		return nil, err
104 	}
105 
106 	if resp.StatusCode != 200 {
107-		return nil, errors.New("Invlid response")
108+		return nil, errors.New("Http error " + strconv.Itoa(resp.StatusCode))
109 	}
110 
111 	return resp.Body, nil
112diff --git a/template.go b/template.go
113index 503af64338f8d2e30b3e37390cf9e54a642b2e44..8d84f2e684864cebc79d98effb421d48cdcf831e 100644
114--- a/template.go
115+++ b/template.go
116@@ -1,6 +1,7 @@
117 package main
118 
119 import (
120+	"errors"
121 	html "html/template"
122 	"io"
123 	"os"
124@@ -39,6 +40,6 @@ 		return html.New("html").
125 			Funcs(templateFunc).
126 			Parse(string(tmpl))
127 	default:
128-		panic("Invalid template-type")
129+		return nil, errors.New("Invalid template type")
130 	}
131 }