package main import ( "archive/tar" "bufio" "compress/gzip" "errors" "fmt" "io" "net/http" "os" "git.sr.ht/~gabrielgio/apkdoc/parser" 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") repositoryFormat := flag.StringP("repository-format", "f", "https://git.sr.ht/~gabrielgio/apkbuilds/tree/%s/item/apks/%s", "Template to build repository link") flag.Parse() tarStream, err := fechIndex(*url) if err != nil { panic("Error fecthing the index: " + err.Error()) } defer tarStream.Close() archive, err := gzip.NewReader(tarStream) if err != nil { panic("Error creating gzip reader: " + err.Error()) } tr := tar.NewReader(archive) for { h, err := tr.Next() if err != nil { panic("Error reading next tar entry: " + err.Error()) } if h.FileInfo().Name() == "APKINDEX" { break } } s := bufio.NewScanner(tr) entries := make([]*parser.Entry, 0) lines := make([]string, 0) for s.Scan() { l := s.Text() if l == "" { entry := parser.Parse(lines) entries = append(entries, entry) lines = make([]string, 0) } else { lines = append(lines, l) } } outputFile, err := getOutputFile(*output) if err != nil { panic("Error openning output file: " + err.Error()) } for _, e := range entries { fmt.Fprintln(outputFile, e.FomartLink(*repositoryFormat)) } } func getOutputFile(output string) (*os.File, error) { if output == "" { outputFile, err := os.Create(output) if err != nil { return nil, err } return outputFile, nil } else { return os.Stdout, nil } } func fechIndex(url string) (io.ReadCloser, error) { resp, err := http.Get(url) if err != nil { return nil, err } if resp.StatusCode != 200 { return nil, errors.New("Invlid response") } return resp.Body, nil }