Skip to content

Commit 83277c5

Browse files
Clarify talismanrc method notes and names
Authored-by: Owen Nelson <[email protected]>
1 parent 06b32c0 commit 83277c5

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

talismanrc/rc_file.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ var (
2020
currentRCFileName = DefaultRCFileName
2121
)
2222

23+
// Load creates a TalismanRC struct based on a .talismanrc file, if present
2324
func Load() (*TalismanRC, error) {
2425
fileContents, err := afero.ReadFile(fs, currentRCFileName)
2526
if err != nil {
2627
// File does not exist or is not readable, proceed as if there is no .talismanrc
2728
fileContents = []byte{}
2829
}
29-
return newPersistedRC(fileContents)
30+
return talismanRCFromYaml(fileContents)
3031
}
3132

32-
func newPersistedRC(fileContents []byte) (*TalismanRC, error) {
33+
func talismanRCFromYaml(fileContents []byte) (*TalismanRC, error) {
3334
talismanRCFromFile := TalismanRC{}
3435
err := yaml.Unmarshal(fileContents, &talismanRCFromFile)
3536
if err != nil {

talismanrc/rc_file_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fileignoreconfig:
5959
custom_patterns:
6060
- 'pwd_[a-z]{8,20}'`)
6161

62-
rc, err := newPersistedRC(fileContents)
62+
rc, err := talismanRCFromYaml(fileContents)
6363
assert.Nil(t, err, "Should successfully unmarshal valid yaml")
6464
assert.Equal(t, 1, len(rc.FileIgnoreConfig))
6565
assert.Equal(t, 1, len(rc.CustomPatterns))
@@ -75,7 +75,7 @@ fileignoreconfig:
7575
- filename: testfile_3.yml
7676
checksum: file3_checksum`)
7777

78-
rc, _ := newPersistedRC(fileContent)
78+
rc, _ := talismanRCFromYaml(fileContent)
7979
assert.Equal(t, 3, len(rc.FileIgnoreConfig))
8080

8181
assert.Equal(t, rc.FileIgnoreConfig[0].GetFileName(), "testfile_1.yml")
@@ -88,7 +88,7 @@ fileignoreconfig:
8888

8989
t.Run("Should read severity level", func(t *testing.T) {
9090
talismanRCContents := []byte("threshold: high")
91-
persistedTalismanrc, _ := newPersistedRC(talismanRCContents)
91+
persistedTalismanrc, _ := talismanRCFromYaml(talismanRCContents)
9292
assert.Equal(t, persistedTalismanrc.Threshold, severity.High)
9393
})
9494

@@ -98,15 +98,15 @@ custom_severities:
9898
- detector: Base64Content
9999
severity: low
100100
`)
101-
talismanRC, _ := newPersistedRC(talismanRCContents)
101+
talismanRC, _ := talismanRCFromYaml(talismanRCContents)
102102
assert.Equal(t, talismanRC.CustomSeverities, []CustomSeverityConfig{{Detector: "Base64Content", Severity: severity.Low}})
103103
})
104104
}
105105

106106
func TestShouldIgnoreUnformattedFiles(t *testing.T) {
107107
for _, s := range []string{"#", "#monkey", "# this monkey likes bananas "} {
108108
fileContents := []byte(s)
109-
talismanRC, err := newPersistedRC(fileContents)
109+
talismanRC, err := talismanRCFromYaml(fileContents)
110110
assert.Nil(t, err, "Should successfully unmarshal commented yaml")
111111
assert.Equal(t, &TalismanRC{Version: "1.0"}, talismanRC, "Expected commented line '%s' to result in an empty TalismanRC")
112112
}

talismanrc/talismanrc.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ type TalismanRC struct {
2323
Version string `yaml:"version"`
2424
}
2525

26-
// SuggestRCFor returns the talismanRC file content corresponding to input ignore configs
26+
// SuggestRCFor returns a string representation of a .talismanrc for the specified FileIgnoreConfigs
2727
func SuggestRCFor(configs []FileIgnoreConfig) string {
2828
tRC := TalismanRC{FileIgnoreConfig: configs, Version: DefaultRCVersion}
2929
result, _ := yaml.Marshal(tRC)
3030

3131
return string(result)
3232
}
3333

34-
// Accept answers true if the Addition.Path is configured to be checked by the detectors
34+
// Accept answers true if the Addition should be checked by the specified detector
3535
func (tRC *TalismanRC) Accept(addition gitrepo.Addition, detectorName string) bool {
3636
return !tRC.Deny(addition, detectorName)
3737
}
@@ -62,6 +62,7 @@ func (tRC *TalismanRC) FilterAdditions(additions []gitrepo.Addition) []gitrepo.A
6262
return result
6363
}
6464

65+
// AddIgnores inserts the specified FileIgnoreConfigs to an existing .talismanrc file, or creates one if it doesn't exist.
6566
func (tRC *TalismanRC) AddIgnores(entriesToAdd []FileIgnoreConfig) {
6667
if len(entriesToAdd) > 0 {
6768
logr.Debugf("Adding entries: %v", entriesToAdd)
@@ -112,7 +113,7 @@ func combineFileIgnores(exsiting, incoming []FileIgnoreConfig) []FileIgnoreConfi
112113
return result
113114
}
114115

115-
// Deny answers true if the Addition.Path is configured to be ignored and not checked by the detectors
116+
// Deny answers true if the Addition should NOT be checked by the specified detector
116117
func (tRC *TalismanRC) Deny(addition gitrepo.Addition, detectorName string) bool {
117118
for _, pattern := range tRC.effectiveRules(detectorName) {
118119
if addition.Matches(pattern) {
@@ -122,7 +123,7 @@ func (tRC *TalismanRC) Deny(addition gitrepo.Addition, detectorName string) bool
122123
return false
123124
}
124125

125-
// Strip git addition
126+
// Remove globally- and per-file allowed patterns from an Addition
126127
func (tRC *TalismanRC) FilterAllowedPatternsFromAddition(addition gitrepo.Addition) string {
127128
additionPathAsString := string(addition.Path)
128129
// Processing global allowed patterns

0 commit comments

Comments
 (0)