aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-09-03 21:33:46 +0200
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-09-03 21:33:46 +0200
commit08dcb18a941a80a3b37c14c8d64dd3a229e86ab1 (patch)
tree85f8b38ffab2a8d259490ea9ec9d51fe78f3c94b
parentf361e53f711fe207c6a67c81af4507363345ef3b (diff)
downloadapkdoc-08dcb18a941a80a3b37c14c8d64dd3a229e86ab1.tar.gz
apkdoc-08dcb18a941a80a3b37c14c8d64dd3a229e86ab1.tar.bz2
apkdoc-08dcb18a941a80a3b37c14c8d64dd3a229e86ab1.zip
ref: Better handle and outputs errors
-rw-r--r--main.go56
-rw-r--r--template.go3
2 files changed, 44 insertions, 15 deletions
diff --git a/main.go b/main.go
index 314ecc8..3f3f40c 100644
--- a/main.go
+++ b/main.go
@@ -5,30 +5,54 @@ import (
"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 @@ func main() {
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 @@ func main() {
}
}
- 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 @@ func fechIndex(url string) (io.ReadCloser, error) {
}
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 503af64..8d84f2e 100644
--- a/template.go
+++ b/template.go
@@ -1,6 +1,7 @@
package main
import (
+ "errors"
html "html/template"
"io"
"os"
@@ -39,6 +40,6 @@ func GetTemplate(templateType, filePath string) (Templater, error) {
Funcs(templateFunc).
Parse(string(tmpl))
default:
- panic("Invalid template-type")
+ return nil, errors.New("Invalid template type")
}
}