Skip to content

Commit

Permalink
[feature/recursivity] recursiveness as option
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilou4 committed Apr 10, 2024
1 parent 22dd0bb commit ec65a30
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 16 deletions.
5 changes: 4 additions & 1 deletion cmd/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/spf13/cobra"
)

var recursiveFlag bool

type HashResult struct {
res []byte
path string
Expand All @@ -26,12 +28,13 @@ var hashCmd = &cobra.Command{
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 a directory, it can be proceed recursively.`,
SilenceErrors: true,
}

func init() {
rootCmd.AddCommand(hashCmd)
hashCmd.PersistentFlags().BoolVar(&recursiveFlag, "recursive", false, "When this flag is set, every directories in your input will be proceed recursively.")
}

func computeHash(path string, h hash.Hash) ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/md5Hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ var md5HashCmd = &cobra.Command{
Long: `Display MD5 checksums (128 bits).
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 a directory, it can be proceed recursively.`,
RunE: func(cmd *cobra.Command, args []string) error {
filesToCheck, err := getFilesToCompute(args)
filesToCheck, err := getFilesToCompute(args, recursiveFlag)
if err != nil {
return err
}
Expand Down
12 changes: 9 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func remove(s []string, i int) []string {
return s[:len(s)-1]
}

func getFilesToCompute(args []string) ([]string, error) {
func getFilesToCompute(args []string, recursive bool) ([]string, error) {
if len(args) == 0 || (len(args) == 1 && args[0] == "-") {
return []string{"-"}, nil
}
Expand All @@ -54,7 +54,7 @@ func getFilesToCompute(args []string) ([]string, error) {
}
if fInfo.IsDir() {
args = remove(args, idx)
args, err = computeDirRecursively(args, elem)
args, err = computeDir(args, elem, recursive)
if err != nil {
return nil, err
}
Expand All @@ -65,13 +65,19 @@ func getFilesToCompute(args []string) ([]string, error) {
return args, nil
}

func computeDirRecursively(l []string, rootFolder string) ([]string, error) {
func computeDir(l []string, rootFolder string, recursive bool) ([]string, error) {
err := filepath.WalkDir(rootFolder, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
l = append(l, path)
} else {
// d is a directory
// skipping dirs if not in recursive mode - except for the rootFolder (obviously).
if filepath.Base(rootFolder) != d.Name() && !recursive {
return filepath.SkipDir
}
}
return nil
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/sha1Hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ var sha1HashCmd = &cobra.Command{
Long: `Display SHA-1 checksums (160 bits).
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 a directory, it can be proceed recursively.`,
RunE: func(cmd *cobra.Command, args []string) error {
filesToCheck, err := getFilesToCompute(args)
filesToCheck, err := getFilesToCompute(args, recursiveFlag)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/sha224Hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ var sha224HashCmd = &cobra.Command{
Long: `Display SHA-224 checksums (224 bits).
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 a directory, it can be proceed recursively.`,
RunE: func(cmd *cobra.Command, args []string) error {
filesToCheck, err := getFilesToCompute(args)
filesToCheck, err := getFilesToCompute(args, recursiveFlag)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/sha256Hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ var sha256HashCmd = &cobra.Command{
Long: `Display SHA256 checksums (256 bits).
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 a directory, it can be proceed recursively.`,
RunE: func(cmd *cobra.Command, args []string) error {
filesToCheck, err := getFilesToCompute(args)
filesToCheck, err := getFilesToCompute(args, recursiveFlag)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/sha384Hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ var sha384HashCmd = &cobra.Command{
Long: `Display SHA-384 checksums (384 bits).
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 a directory, it can be proceed recursively.`,
RunE: func(cmd *cobra.Command, args []string) error {
filesToCheck, err := getFilesToCompute(args)
filesToCheck, err := getFilesToCompute(args, recursiveFlag)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/sha512Hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ var sha512HashCmd = &cobra.Command{
Long: `Display SHA-512 checksums (512 bits).
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 a directory, it can be proceed recursively.`,
RunE: func(cmd *cobra.Command, args []string) error {
filesToCheck, err := getFilesToCompute(args)
filesToCheck, err := getFilesToCompute(args, recursiveFlag)
if err != nil {
return err
}
Expand Down

0 comments on commit ec65a30

Please sign in to comment.