Debian Codegraph @

tools/antithesis-go-instrumentor/config/exclusions.go

raw
1 package config
2
3 import (
4 "bufio"
5 "os"
6 "path/filepath"
7 "strings"
8
9 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common"
10 )
11
12 // ParseExclusionsFile reads the exclusions file, skipping lines beginning with
13 // #. Golang does not have a set class, so, rather than waste space copy-pastaing
14 // code from the interwebs, we'll just return a map.
15 func ParseExclusionsFile(path string, inputDirectory string) (exclusions map[string]bool, err error) {
16 exclusions = map[string]bool{}
17 logger := common.Logger
18 var exclusionsFile *os.File
19 if exclusionsFile, err = os.Open(path); err != nil {
20 logger.Fatalf("Could not open exclusions %s: %v", path, err)
21 return
22 }
23 defer exclusionsFile.Close()
24 logger.Printf(common.Normal, "Reading exclusions from %s; relative paths will be resolved to %s", path, inputDirectory)
25 scanner := bufio.NewScanner(exclusionsFile)
26 for scanner.Scan() {
27 entry := scanner.Text()
28 if strings.HasPrefix(entry, "#") || strings.TrimSpace(entry) == "" {
29 continue
30 }
31
32 exclusion := entry
33 if !filepath.IsAbs(entry) {
34 exclusion = filepath.Join(inputDirectory, entry)
35 }
36
37 if exclusion, err = filepath.Abs(exclusion); err != nil {
38 logger.Fatalf("Exclusion %s could not be resolved to an absolute path: %v", entry, err)
39 return
40 }
41
42 if _, err = os.Stat(exclusion); err == nil {
43 exclusions[exclusion] = true
44 logger.Printf(common.Normal, "Exclusion %s added as %s", entry, exclusion)
45 } else {
46 logger.Fatalf("File %s in exclusions does not exist or is inaccessible", entry)
47 return
48 }
49 }
50
51 if err = scanner.Err(); err != nil {
52 logger.Fatalf("Error scanning file %s: %v", path, err)
53 }
54
55 return
56 }