1package main
2
3import (
4 "archive/tar"
5 "bufio"
6 "compress/gzip"
7 "errors"
8 "flag"
9 "io"
10 "net/http"
11 "os"
12
13 "git.sr.ht/~gabrielgio/apkdoc/parser"
14 "git.sr.ht/~gabrielgio/apkdoc/templates"
15)
16
17func fechIndex(url string) (io.ReadCloser, error) {
18 resp, err := http.Get(url)
19 if err != nil {
20 return nil, err
21 }
22
23 if resp.StatusCode != 200 {
24 return nil, errors.New("Invlid response")
25 }
26
27 return resp.Body, nil
28}
29
30func main() {
31 url := flag.String("url", "", "Url to the APKINDEX.tar.gz")
32 output := flag.String("output", "index.md", "Output path")
33 flag.Parse()
34
35 tarStream, err := fechIndex(*url)
36 if err != nil {
37 panic("Error fecthing the index: " + err.Error())
38 }
39
40 defer tarStream.Close()
41
42 archive, err := gzip.NewReader(tarStream)
43 if err != nil {
44 panic("Error creating gzip reader: " + err.Error())
45 }
46
47 tr := tar.NewReader(archive)
48
49 for {
50 h, err := tr.Next()
51 if err != nil {
52 panic("Error reading next tar entry: " + err.Error())
53 }
54
55 if h.FileInfo().Name() == "APKINDEX" {
56 break
57 }
58 }
59
60 s := bufio.NewScanner(tr)
61
62 entries := make([]*parser.Entry, 0)
63 lines := make([]string, 0)
64
65 for s.Scan() {
66 l := s.Text()
67 if l == "" {
68 entry := parser.Parse(lines)
69 entries = append(entries, entry)
70 lines = make([]string, 0)
71 } else {
72 lines = append(lines, l)
73 }
74 }
75
76 file, err := os.Create(*output)
77 if err != nil {
78 panic("Error opening output file: " + err.Error())
79 }
80
81 templates.WriteMarkdownTemplate(file, entries)
82}