1package main
2
3import (
4 "archive/tar"
5 "bufio"
6 "compress/gzip"
7 "errors"
8 "io"
9 "net/http"
10 "os"
11
12 "git.sr.ht/~gabrielgio/apkdoc/parser"
13 "git.sr.ht/~gabrielgio/apkdoc/templates"
14 flag "github.com/spf13/pflag"
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.StringP("url", "u", "", "Url to the APKINDEX.tar.gz")
32 output := flag.StringP("output", "o", "index.md", "Output path")
33 repositoryFormat := flag.StringP("repository-format", "f", "https://git.sr.ht/~gabrielgio/apkbuilds/tree/%s/item/apks/%s", "Template to build repository link")
34 flag.Parse()
35
36 tarStream, err := fechIndex(*url)
37 if err != nil {
38 panic("Error fecthing the index: " + err.Error())
39 }
40
41 defer tarStream.Close()
42
43 archive, err := gzip.NewReader(tarStream)
44 if err != nil {
45 panic("Error creating gzip reader: " + err.Error())
46 }
47
48 tr := tar.NewReader(archive)
49
50 for {
51 h, err := tr.Next()
52 if err != nil {
53 panic("Error reading next tar entry: " + err.Error())
54 }
55
56 if h.FileInfo().Name() == "APKINDEX" {
57 break
58 }
59 }
60
61 s := bufio.NewScanner(tr)
62
63 entries := make([]*parser.Entry, 0)
64 lines := make([]string, 0)
65
66 for s.Scan() {
67 l := s.Text()
68 if l == "" {
69 entry := parser.Parse(lines)
70 entries = append(entries, entry)
71 lines = make([]string, 0)
72 } else {
73 lines = append(lines, l)
74 }
75 }
76
77 file, err := os.Create(*output)
78 if err != nil {
79 panic("Error opening output file: " + err.Error())
80 }
81
82 templates.WriteMarkdownTemplate(file, entries, *repositoryFormat)
83}