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