github.com/inturn/pre-commit-gobuild@v1.0.12/hooks/dockercheck/dockercheck.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 "path/filepath" 8 "regexp" 9 "strings" 10 11 "github.com/inturn/pre-commit-gobuild/internal/dockerfile" 12 "github.com/inturn/pre-commit-gobuild/internal/dockerfile/command" 13 ) 14 15 func main() { 16 wd, err := os.Getwd() 17 if err != nil { 18 log.Fatal(err) 19 } 20 21 dckFiles := make([]string, 0) 22 23 filepath.Walk(wd, func(path string, f os.FileInfo, _ error) error { 24 if !f.IsDir() { 25 r, err := regexp.MatchString(`^Dockerfile.*$`, f.Name()) 26 if err == nil && r { 27 dckFiles = append(dckFiles, path) 28 } 29 } 30 return nil 31 }) 32 33 errs := make([]error, 0) 34 defer func() { 35 if len(errs) != 0 { 36 for _, e := range errs { 37 log.Println(e) 38 } 39 os.Exit(1) 40 } 41 os.Exit(0) 42 }() 43 44 for _, f := range dckFiles { 45 directive := parser.Directive{} 46 parser.SetEscapeToken("`", &directive) 47 48 f, err := os.Open(f) 49 if err != nil { 50 errs = append(errs, err) 51 continue 52 } 53 node, err := parser.Parse(f, &directive) 54 if err != nil { 55 errs = append(errs, err) 56 continue 57 } 58 for _, n := range node.Children { 59 v := strings.ToUpper(n.Value) 60 if m, err := regexp.MatchString(fmt.Sprintf(`^%s.*$`, v), n.Original); err != nil || !m { 61 errs = append(errs, fmt.Errorf("capitalize Dockerfile Instructions: %s line %d", f.Name(), n.StartLine)) 62 } 63 if n.Value == command.From { 64 if m, err := regexp.MatchString(`(?i)^(from)\s.*:latest\s*$`, n.Original); err != nil || m { 65 errs = append(errs, fmt.Errorf("images should not use the latest tag: %s line %d", f.Name(), n.StartLine)) 66 } 67 } 68 if m, err := regexp.MatchString(`^.*apt-get.*install.*$`, n.Original); err != nil || m { 69 if !strings.Contains(n.Original, "--no-install-recommends") { 70 fmt.Printf("Consider `--no-install-recommends`: %s line %d", f.Name(), n.StartLine) 71 } 72 } 73 } 74 } 75 }