1package main
2
3import (
4 "archive/tar"
5 "bufio"
6 "compress/gzip"
7 "errors"
8 "io"
9 "net/http"
10 "os"
11
12 flag "github.com/spf13/pflag"
13)
14
15func main() {
16 url := flag.StringP("url", "u", "", "Url to the APKINDEX.tar.gz")
17 output := flag.StringP("output", "o", "index.md", "Output path")
18 templateType := flag.StringP("template-type", "p", "text", "Template system to be used, options: html, text")
19 templateFile := flag.StringP("template-file", "t", "text", "Template file to be used")
20 flag.Parse()
21
22 tarStream, err := fechIndex(*url)
23 if err != nil {
24 panic("Error fecthing the index: " + err.Error())
25 }
26
27 defer tarStream.Close()
28
29 archive, err := gzip.NewReader(tarStream)
30 if err != nil {
31 panic("Error creating gzip reader: " + err.Error())
32 }
33
34 tr := tar.NewReader(archive)
35
36 for {
37 h, err := tr.Next()
38 if err != nil {
39 panic("Error reading next tar entry: " + err.Error())
40 }
41
42 if h.FileInfo().Name() == "APKINDEX" {
43 break
44 }
45 }
46
47 s := bufio.NewScanner(tr)
48
49 entries := make([]*Entry, 0)
50 lines := make([]string, 0)
51
52 for s.Scan() {
53 l := s.Text()
54 if l == "" {
55 entry := Parse(lines)
56 entries = append(entries, entry)
57 lines = make([]string, 0)
58 } else {
59 lines = append(lines, l)
60 }
61 }
62
63 outputFile, err := getOutputFile(*output)
64 if err != nil {
65 panic("Error openning output file: " + err.Error())
66 }
67
68 tmpl, err := GetTemplate(*templateType, *templateFile)
69 if err != nil {
70 panic("Error loading template file: " + err.Error())
71 }
72
73 tmpl.Execute(outputFile, entries)
74}
75
76func getOutputFile(output string) (*os.File, error) {
77 if output != "" {
78 outputFile, err := os.Create(output)
79 if err != nil {
80 return nil, err
81 }
82 return outputFile, nil
83 } else {
84 return os.Stdout, nil
85 }
86}
87
88func fechIndex(url string) (io.ReadCloser, error) {
89 resp, err := http.Get(url)
90 if err != nil {
91 return nil, err
92 }
93
94 if resp.StatusCode != 200 {
95 return nil, errors.New("Invlid response")
96 }
97
98 return resp.Body, nil
99}