-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/add hashing #30
Changes from 5 commits
2d14563
07a80cd
8e70291
1e500ba
f432c7b
3708162
45e163d
9e51d40
caf43d9
6bf4ac6
66e0850
59c547a
87e460d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
package transform | ||
|
||
import ( | ||
"crypto" | ||
_ "crypto/sha1" //Import SHA-1 hashing function | ||
"encoding/hex" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
@@ -31,6 +36,14 @@ func Format(p *FormatParams) (val interface{}, err error) { | |
val, err = p.fullPath() | ||
case "SHORTPATH": | ||
val, err = p.shortPath() | ||
case "SHA1": | ||
var hash string | ||
hash, err = p.hash(crypto.SHA1) | ||
if err == nil && len(p.Args) > 0 && p.Args[0] != "" { | ||
if n, err := strconv.Atoi(p.Args[0]); err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't want to reinitialize |
||
val = truncate(hash, n) | ||
} | ||
} | ||
} | ||
|
||
if err != nil { | ||
|
@@ -109,6 +122,30 @@ func (p *FormatParams) shortPath() (interface{}, error) { | |
return p.Info.Name(), nil | ||
} | ||
|
||
func (p *FormatParams) hash(hasher crypto.SignerOpts) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's return an |
||
return hash(p.Info, p.Path, hasher) | ||
} | ||
|
||
func hash(info os.FileInfo, path string, hasher crypto.SignerOpts) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if info.IsDir() { | ||
return "----------------------------------------", nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use |
||
} | ||
|
||
f, err := os.Open(path) | ||
if err != nil { | ||
return "", err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can now be changed to return |
||
} | ||
defer f.Close() | ||
b, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
h := hasher.HashFunc().New() | ||
h.Write(b) | ||
return hex.EncodeToString(h.Sum(nil)), nil | ||
} | ||
|
||
// DefaultFormatValue returns the default format value for the provided | ||
// attribute attr based on path and info. | ||
func DefaultFormatValue(attr, path string, info os.FileInfo) interface{} { | ||
|
@@ -121,6 +158,12 @@ func DefaultFormatValue(attr, path string, info os.FileInfo) interface{} { | |
return info.Size() | ||
case "time": | ||
return info.ModTime().Format(time.Stamp) | ||
case "hash": | ||
v, err := hash(info, path, crypto.SHA1) | ||
if err != nil { | ||
panic(err.Error()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how I feel about panicking, but you can leave this as is for now. Might need to refactor this method to return an error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what I was thinking as well, I suppose we can let the caller handle the error? |
||
} | ||
return truncate(v, 7) | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking the same thing twice! 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol 🤦♂️