-
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
Conversation
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.
Looks good overall, left some comments for things that may need changing.
Haven't tested yet, will update as I do that.
transform/common.go
Outdated
@@ -24,3 +24,11 @@ func upper(name string) interface{} { | |||
func lower(name string) interface{} { | |||
return strings.ToLower(name) | |||
} | |||
|
|||
func truncate(str string, n int) string { | |||
if len(str) < n || n > len(str) { |
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 🤦♂️
transform/format.go
Outdated
|
||
func hash(info os.FileInfo, path string, hasher crypto.SignerOpts) (string, error) { | ||
if info.IsDir() { | ||
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.
Should use strings.Repeat
with hasher.HashFunc().Size
here, since sizes may vary between hash functions.
transform/format.go
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to reinitialize err
here.
transform/format.go
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Let's return an interface{}
here (instead of string
).
transform/format.go
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
interface{}
here as well.
transform/format.go
Outdated
|
||
f, err := os.Open(path) | ||
if err != nil { | ||
return "", err |
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.
This can now be changed to return nil
(below as well).
transform/format.go
Outdated
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 comment
The 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 comment
The 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?
Nice; seems to be working well! Let me know when this is ready – I'm going to move this into another branch and make some changes (mostly test stuff) before merging with master. |
I'm ok with this getting merged into a dev branch. I refactored it a bit more (extracting arguments and error handling with defaults), so you may want to review those some more. |
Merged with #31. Thanks for the contribution, @jonesbrianc26! |
Getting this out because I'd like to hear your feedback.
Details of implementation dicussion can be found at #9