diff options
-rw-r--r-- | main.go | 56 | ||||
-rw-r--r-- | template.go | 3 |
2 files changed, 44 insertions, 15 deletions
@@ -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") } } |