-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature/workers] handling directory recursively and computing hash w…
…ith workers (#6)
- Loading branch information
Showing
8 changed files
with
221 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,55 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"hash" | ||
"io" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type HashResult struct { | ||
res []byte | ||
path string | ||
err error | ||
} | ||
|
||
type JobsParam struct { | ||
path string | ||
h hash.Hash | ||
} | ||
|
||
// hashCmd represents the hash command | ||
var hashCmd = &cobra.Command{ | ||
Use: "hash", | ||
Short: "", | ||
Long: ``, | ||
Use: "hash", | ||
Short: "The hash command computes the hash of a given FILE.", | ||
Long: `The hash command computes the hash of a given FILE. | ||
Without FILE or when FILE is '-', read the standard input. | ||
If the list of FILE contains a directory, it will be proceed recursively. | ||
If the list of FILE contains './...' it will proceed directories recursively from the current directory.`, | ||
SilenceErrors: true, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(hashCmd) | ||
} | ||
|
||
func getFilesToCompute(args []string) []string { | ||
if len(args) == 0 || (len(args) == 1 && args[0] == "-") { | ||
return []string{"-"} | ||
} | ||
return args | ||
} | ||
|
||
func computeFiles(filesToCheck []string, h hash.Hash) (string, error) { | ||
var res string | ||
for _, filePath := range filesToCheck { | ||
hashedValue, err := computeHash(filePath, h) | ||
func computeHash(path string, h hash.Hash) ([]byte, error) { | ||
var r io.Reader | ||
if path == "-" { | ||
r = os.Stdin | ||
} else { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return "", err | ||
return nil, err | ||
} | ||
res += fmt.Sprintf("%x %s\n", hashedValue, filePath) | ||
h.Reset() | ||
defer file.Close() | ||
r = file | ||
} | ||
_, err := io.Copy(h, r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
return h.Sum(nil), 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
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
Oops, something went wrong.