diff_target_files.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. package main
  15. import (
  16. "flag"
  17. "fmt"
  18. "os"
  19. "strings"
  20. )
  21. var (
  22. allowLists = newMultiString("allowlist", "allowlist patterns in the form <pattern>[:<regex of line to ignore>]")
  23. allowListFiles = newMultiString("allowlist_file", "files containing allowlist definitions")
  24. filters = newMultiString("filter", "filter patterns to apply to files in target-files.zip before comparing")
  25. )
  26. func newMultiString(name, usage string) *multiString {
  27. var f multiString
  28. flag.Var(&f, name, usage)
  29. return &f
  30. }
  31. type multiString []string
  32. func (ms *multiString) String() string { return strings.Join(*ms, ", ") }
  33. func (ms *multiString) Set(s string) error { *ms = append(*ms, s); return nil }
  34. func main() {
  35. flag.Parse()
  36. if flag.NArg() != 2 {
  37. fmt.Fprintf(os.Stderr, "Error, exactly two arguments are required\n")
  38. os.Exit(1)
  39. }
  40. allowLists, err := parseAllowLists(*allowLists, *allowListFiles)
  41. if err != nil {
  42. fmt.Fprintf(os.Stderr, "Error parsing allowlists: %v\n", err)
  43. os.Exit(1)
  44. }
  45. priZip, err := NewLocalZipArtifact(flag.Arg(0))
  46. if err != nil {
  47. fmt.Fprintf(os.Stderr, "Error opening zip file %v: %v\n", flag.Arg(0), err)
  48. os.Exit(1)
  49. }
  50. defer priZip.Close()
  51. refZip, err := NewLocalZipArtifact(flag.Arg(1))
  52. if err != nil {
  53. fmt.Fprintf(os.Stderr, "Error opening zip file %v: %v\n", flag.Arg(1), err)
  54. os.Exit(1)
  55. }
  56. defer refZip.Close()
  57. diff, err := compareTargetFiles(priZip, refZip, targetFilesPattern, allowLists, *filters)
  58. if err != nil {
  59. fmt.Fprintf(os.Stderr, "Error comparing zip files: %v\n", err)
  60. os.Exit(1)
  61. }
  62. fmt.Print(diff.String())
  63. if len(diff.modified) > 0 || len(diff.onlyInA) > 0 || len(diff.onlyInB) > 0 {
  64. fmt.Fprintln(os.Stderr, "differences found")
  65. os.Exit(1)
  66. }
  67. }