-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement golang native hash command
- Loading branch information
Showing
6 changed files
with
54 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,45 @@ | ||
package version | ||
|
||
import ( | ||
"crypto/sha256" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/radiofrance/dib/exec" | ||
"github.com/wolfeidau/humanhash" | ||
"golang.org/x/mod/sumdb/dirhash" | ||
) | ||
|
||
// getDockerVersionHash returns the revision hash of the docker directory. | ||
func getDockerVersionHash(exec exec.Executor) (string, error) { | ||
cmd := "find docker -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum | cut -d ' ' -f 1" | ||
res, err := exec.Execute("bash", "-c", cmd) | ||
// GetDockerVersionHash returns the revision hash of the build directory. | ||
func GetDockerVersionHash(buildPath string) (string, error) { | ||
return dirhash.HashDir(buildPath, "", humanReadableHashFn) | ||
} | ||
|
||
func humanReadableHashFn(files []string, open func(string) (io.ReadCloser, error)) (string, error) { | ||
hash := sha256.New() | ||
files = append([]string(nil), files...) | ||
sort.Strings(files) | ||
for _, file := range files { | ||
if strings.Contains(file, "\n") { | ||
return "", errors.New("dirhash: filenames with newlines are not supported") | ||
} | ||
readCloser, err := open(file) | ||
if err != nil { | ||
return "", err | ||
} | ||
hashFile := sha256.New() | ||
_, err = io.Copy(hashFile, readCloser) | ||
readCloser.Close() | ||
if err != nil { | ||
return "", err | ||
} | ||
fmt.Fprintf(hash, "%x %s\n", hashFile.Sum(nil), file) | ||
} | ||
humanReadableHash, err := humanhash.Humanize(hash.Sum(nil), 4) | ||
if err != nil { | ||
return "", err | ||
return "", fmt.Errorf("could not humanize hash: %w", err) | ||
} | ||
return strings.TrimSuffix(res, "\n"), nil | ||
return humanReadableHash, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters