Skip to content

Commit

Permalink
remove output directory from file list for digest (#410)
Browse files Browse the repository at this point in the history
Co-authored-by: Dan Curran <[email protected]>
  • Loading branch information
dcurran90 and Dan Curran authored Apr 23, 2024
1 parent 05eb514 commit a8fa028
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
16 changes: 16 additions & 0 deletions internal/chartverifier/reportBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ type fileSorter struct {
by func(p1, p2 *helmchart.File) bool // Closure used in the Less method.
}

// If chart-verifier is run from within the helm chart directory
// the output will be sent to the OutputDirectory and affect the digest.
// This removes any verifier output files from the calculated digest
func filterOutputDirectory(files []*helmchart.File) []*helmchart.File {
n := 0
for _, file := range files {
if !strings.Contains(file.Name, utils.OutputDirectory) {
files[n] = file
n++
}
}
return files[:n]
}

func (by By) sort(files []*helmchart.File) {
fs := &fileSorter{
files: files,
Expand Down Expand Up @@ -200,6 +214,8 @@ func GenerateSha(rawFiles []*helmchart.File) string {
chartSha := sha256.New()
sortedFiles := rawFiles
By(name).sort(sortedFiles)
sortedFiles = filterOutputDirectory(sortedFiles)

for _, chartFile := range sortedFiles {
chartSha.Write(chartFile.Data)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/chartverifier/utils/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (
stderrFileName string
)

const outputDirectory string = "chartverifier"
const OutputDirectory string = "chartverifier"

func InitLog(cobraCmd *cobra.Command, stdFilename string, suppressErrorLog bool) {
cmd = cobraCmd
Expand Down Expand Up @@ -123,7 +123,7 @@ func writeToFile(output string, fileName string) bool {
LogError(fmt.Sprintf("error getting current working directory : %s", err))
return false
}
outputDir := path.Join(currentDir, outputDirectory)
outputDir := path.Join(currentDir, OutputDirectory)
outputFile := path.Join(outputDir, fileName)
if _, err := os.Stat(outputDir); err != nil {
// #nosec G301
Expand Down Expand Up @@ -200,7 +200,7 @@ func pruneLogFiles() {
LogError(fmt.Sprintf("error getting current working directory : %s", err))
return
}
logFilesPath := path.Join(currentDir, outputDirectory)
logFilesPath := path.Join(currentDir, OutputDirectory)

if _, err := os.Stat(logFilesPath); err != nil {
return
Expand Down
4 changes: 2 additions & 2 deletions internal/chartverifier/utils/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func checkAndOrDeleteFiles(fileType string, expectedContent string) bool {
"ror getting current working directory : %s", err))
return false
}
logFilesPath := path.Join(currentDir, outputDirectory)
logFilesPath := path.Join(currentDir, OutputDirectory)

files, err := os.ReadDir(logFilesPath)
if err != nil {
Expand Down Expand Up @@ -222,7 +222,7 @@ func howManyLogFiles() int {
fmt.Printf("error getting current working directory : %s\n", err)
return 0
}
logFilesPath := path.Join(currentDir, outputDirectory)
logFilesPath := path.Join(currentDir, OutputDirectory)

files, err := os.ReadDir(logFilesPath)
if err != nil {
Expand Down

0 comments on commit a8fa028

Please sign in to comment.