diff --git a/main.go b/main.go
index 314ecc8ad5f43e74b6670920e62dd853335e1e7d..3f3f40cb1179c88d3381c9dfc48a7d929653e8ee 100644
--- a/main.go
+++ b/main.go
@@ -5,30 +5,54 @@ "archive/tar"
"bufio"
"compress/gzip"
"errors"
+ "fmt"
"io"
"net/http"
"os"
+ "strconv"
flag "github.com/spf13/pflag"
)
func main() {
- url := flag.StringP("url", "u", "", "Url to the APKINDEX.tar.gz")
- output := flag.StringP("output", "o", "index.md", "Output path")
- templateType := flag.StringP("template-type", "p", "text", "Template system to be used, options: html, text")
- templateFile := flag.StringP("template-file", "t", "text", "Template file to be used")
+ var (
+ url = flag.StringP("url", "u", "", "Url to the APKINDEX.tar.gz")
+ output = flag.StringP("output", "o", "index.md", "Output path")
+ templateType = flag.StringP("template-type", "p", "text", "Template system to be used, options: html, text")
+ templateFile = flag.StringP("template-file", "t", "text", "Template file to be used")
+ )
flag.Parse()
- tarStream, err := fechIndex(*url)
+ err := run(
+ *url,
+ *output,
+ *templateType,
+ *templateFile,
+ )
if err != nil {
- panic("Error fecthing the index: " + err.Error())
+ fmt.Println(err)
+ os.Exit(1)
+ }
+}
+
+func run(
+ url string,
+ output string,
+ templateType string,
+ templateFile string,
+
+) error {
+
+ tarStream, err := fechIndex(url)
+ if err != nil {
+ return fmt.Errorf("Error fecthing index file: %w", err)
}
defer tarStream.Close()
archive, err := gzip.NewReader(tarStream)
if err != nil {
- panic("Error creating gzip reader: " + err.Error())
+ return fmt.Errorf("Error creating gzip reader: %w", err)
}
tr := tar.NewReader(archive)
@@ -36,7 +60,7 @@
for {
h, err := tr.Next()
if err != nil {
- panic("Error reading next tar entry: " + err.Error())
+ return fmt.Errorf("Error reading next tar entry: %w", err)
}
if h.FileInfo().Name() == "APKINDEX" {
@@ -60,17 +84,21 @@ lines = append(lines, l)
}
}
- outputFile, err := getOutputFile(*output)
+ outputFile, err := getOutputFile(output)
if err != nil {
- panic("Error openning output file: " + err.Error())
+ return fmt.Errorf("Error openning output file: %w", err)
}
- tmpl, err := GetTemplate(*templateType, *templateFile)
+ tmpl, err := GetTemplate(templateType, templateFile)
if err != nil {
- panic("Error loading template file: " + err.Error())
+ return fmt.Errorf("Error loading template file: %w", err)
}
- tmpl.Execute(outputFile, entries)
+ err = tmpl.Execute(outputFile, entries)
+ if err != nil {
+ return fmt.Errorf("Error executing template: %w", err)
+ }
+ return nil
}
func getOutputFile(output string) (*os.File, error) {
@@ -92,7 +120,7 @@ return nil, err
}
if resp.StatusCode != 200 {
- return nil, errors.New("Invlid response")
+ return nil, errors.New("Http error " + strconv.Itoa(resp.StatusCode))
}
return resp.Body, nil
diff --git a/template.go b/template.go
index 503af64338f8d2e30b3e37390cf9e54a642b2e44..8d84f2e684864cebc79d98effb421d48cdcf831e 100644
--- a/template.go
+++ b/template.go
@@ -1,6 +1,7 @@
package main
import (
+ "errors"
html "html/template"
"io"
"os"
@@ -39,6 +40,6 @@ return html.New("html").
Funcs(templateFunc).
Parse(string(tmpl))
default:
- panic("Invalid template-type")
+ return nil, errors.New("Invalid template type")
}
}