summaryrefslogtreecommitdiff
path: root/cmd/blubber/main.go
blob: 53009eaebe1c5f932631a406c3885c4f62eed4e2 (plain)
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
// Package main provides the command line interface.
//
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/pborman/getopt/v2"

	"gerrit.wikimedia.org/r/blubber/config"
	"gerrit.wikimedia.org/r/blubber/docker"
	"gerrit.wikimedia.org/r/blubber/meta"
)

const parameters = "config.yaml variant"

var (
	showHelp    *bool   = getopt.BoolLong("help", 'h', "show help/usage")
	showVersion *bool   = getopt.BoolLong("version", 'v', "show version information")
	policyURI   *string = getopt.StringLong("policy", 'p', "", "policy file URI", "uri")
)

func main() {
	getopt.SetParameters(parameters)
	getopt.Parse()

	if *showHelp {
		getopt.Usage()
		os.Exit(1)
	}

	if *showVersion {
		fmt.Println(meta.FullVersion())
		os.Exit(0)
	}

	args := getopt.Args()

	if len(args) < 2 {
		getopt.Usage()
		os.Exit(1)
	}

	cfgPath, variant := args[0], args[1]

	cfg, err := config.ReadConfigFile(cfgPath)

	if err != nil {
		if config.IsValidationError(err) {
			log.Printf("%s is invalid:\n%v", cfgPath, config.HumanizeValidationError(err))
			os.Exit(4)
		} else {
			log.Printf("Error reading %s: %v\n", cfgPath, err)
			os.Exit(2)
		}
	}

	if *policyURI != "" {
		policy, err := config.ReadPolicyFromURI(*policyURI)

		if err != nil {
			log.Printf("Error loading policy from %s: %v\n", *policyURI, err)
			os.Exit(5)
		}

		err = policy.Validate(*cfg)

		if err != nil {
			log.Printf("Configuration fails policy check against:\npolicy: %s\nviolation: %v\n", *policyURI, err)
			os.Exit(6)
		}
	}

	dockerFile, err := docker.Compile(cfg, variant)

	if err != nil {
		log.Printf("Error compiling config: %v\n", err)
		os.Exit(3)
	}

	dockerFile.WriteTo(os.Stdout)
}