1// Copyright 2019 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18	"flag"
19	"fmt"
20	"os"
21	"strings"
22)
23
24var (
25	allowLists     = newMultiString("allowlist", "allowlist patterns in the form <pattern>[:<regex of line to ignore>]")
26	allowListFiles = newMultiString("allowlist_file", "files containing allowlist definitions")
27
28	filters = newMultiString("filter", "filter patterns to apply to files in target-files.zip before comparing")
29)
30
31func newMultiString(name, usage string) *multiString {
32	var f multiString
33	flag.Var(&f, name, usage)
34	return &f
35}
36
37type multiString []string
38
39func (ms *multiString) String() string     { return strings.Join(*ms, ", ") }
40func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil }
41
42func main() {
43	flag.Parse()
44
45	if flag.NArg() != 2 {
46		fmt.Fprintf(os.Stderr, "Error, exactly two arguments are required\n")
47		os.Exit(1)
48	}
49
50	allowLists, err := parseAllowLists(*allowLists, *allowListFiles)
51	if err != nil {
52		fmt.Fprintf(os.Stderr, "Error parsing allowlists: %v\n", err)
53		os.Exit(1)
54	}
55
56	priZip, err := NewLocalZipArtifact(flag.Arg(0))
57	if err != nil {
58		fmt.Fprintf(os.Stderr, "Error opening zip file %v: %v\n", flag.Arg(0), err)
59		os.Exit(1)
60	}
61	defer priZip.Close()
62
63	refZip, err := NewLocalZipArtifact(flag.Arg(1))
64	if err != nil {
65		fmt.Fprintf(os.Stderr, "Error opening zip file %v: %v\n", flag.Arg(1), err)
66		os.Exit(1)
67	}
68	defer refZip.Close()
69
70	diff, err := compareTargetFiles(priZip, refZip, targetFilesPattern, allowLists, *filters)
71	if err != nil {
72		fmt.Fprintf(os.Stderr, "Error comparing zip files: %v\n", err)
73		os.Exit(1)
74	}
75
76	fmt.Print(diff.String())
77
78	if len(diff.modified) > 0 || len(diff.onlyInA) > 0 || len(diff.onlyInB) > 0 {
79		fmt.Fprintln(os.Stderr, "differences found")
80		os.Exit(1)
81	}
82}
83