secfixes.go 2.71 KB
Newer Older
Kevin Daudt's avatar
Kevin Daudt committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"os"
	"strings"

	"gitlab.alpinelinux.org/alpine/go/pkg/apkbuild"
	"gitlab.alpinelinux.org/alpine/go/pkg/releases"
	"gopkg.in/yaml.v2"
)

var (
	repo    string
	release string
	debug   bool
	outJson string
	outYaml string
)

func errOut(msg string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, msg+"\n", args...)
}

func main() {
	flag.StringVar(&repo, "repo", "", "The repository to process (required)")
	flag.StringVar(&release, "release", "", "The release that's being generated (required)")
	flag.BoolVar(&debug, "debug", false, "Turn on debug output")
	flag.StringVar(&outJson, "outJson", "", "File to write json output to")
	flag.StringVar(&outYaml, "outYaml", "", "File to write yaml output to")
	flag.Parse()

	if release == "" {
		errOut("-release argument is required")
		flag.Usage()
		os.Exit(1)
	}

	if repo == "" {
		errOut("-repo argument is required")
		flag.Usage()
		os.Exit(1)
	}

	releases, err := releases.Fetch()

	if err != nil {
		panic(err)
	}

	releaseBranch := releases.GetRelBranch(release)

	if releaseBranch == nil {
		errOut("No release branch found called: %s\n", release)
		os.Exit(1)
	}

	secfixDB := SecfixDB{}
	secfixDB.Urlprefix = "https://dl-cdn.alpinelinux.org/alpine"
	secfixDB.Apkurl = `{{urlprefix}}/{{distroversion}}/{{reponame}}/{{arch}}/{{pkg.name}}-{{pkg.ver}}.apk`
	secfixDB.Distroversion = release
	secfixDB.Reponame = repo
	secfixDB.Archs = releaseBranch.Arches
	secfixDB.Packages = []Pkg{}

	for _, pkg := range flag.Args() {
		pkgComponents := strings.Split(pkg, "/")
		pkgName := pkgComponents[len(pkgComponents)-2]

		if debug {
			fmt.Printf("Checking package %s\n", pkg)
		}
		file, err := os.Open(pkg)
		if err != nil {
			errOut("%s\n", err)
			continue
		}

		secfixes, err := apkbuild.ParseSecfixes(file)
		file.Close()

		if err != nil {
			errOut("%s\n", err)
			continue
		}

		if secfixes == nil {
			continue
		}

		secfixDB.Packages = append(secfixDB.Packages, Pkg{
			Pkg: Package{
				Name:     pkgName,
				Secfixes: secfixes,
			}})

		if debug {
			secfixesJson, _ := json.MarshalIndent(secfixes, "", "  ")
			fmt.Println(string(secfixesJson))
		}
	}

	if outYaml != "" {
		secfixDBYaml, err := yaml.Marshal(secfixDB)
		if err != nil {
			panic(err)
		}

		err = writeToFile(outYaml, secfixDBYaml)
		if err != nil {
			panic(err)
		}
	}

	if outJson != "" {
		secfixDBJson, err := json.Marshal(secfixDB)
		if err != nil {
			panic(err)
		}

		err = writeToFile(outJson, secfixDBJson)
		if err != nil {
			panic(err)
		}
	}
}

func writeToFile(filename string, output []byte) error {
	file, err := os.Create(filename)

	if err != nil {
		return err
	}

	defer file.Close()

	_, err = file.Write(output)

	if err != nil {
		return err
	}
	return nil
}