Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@fgsch
Copy link
Member

@fgsch fgsch commented Jun 1, 2022

This moves the hash calculation into validate(), now validateAndHash(),
to avoid opening the package twice.

Fixes #243.

@fgsch fgsch force-pushed the fgsch/issue_243 branch 3 times, most recently from f1cb094 to e0c5517 Compare June 1, 2022 10:58
@fgsch fgsch requested a review from Integralist June 1, 2022 11:49
@fgsch fgsch marked this pull request as ready for review June 1, 2022 11:49
@fgsch fgsch force-pushed the fgsch/issue_243 branch from e0c5517 to 5f05843 Compare June 1, 2022 11:56
Copy link
Collaborator

@Integralist Integralist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So some initial feedback.

I was thinking maybe we could instead have the validate function accept an optional callback function. So that the validate.go file could continue to call validate() as normal and there will be no change to the interface or the return values.

But for deploy.go it can call validate() and pass a callback function which is passed the main.wasm file as an io.Reader so we can do the hashing stuff as a separate function.

e.g.

type FileValidator func(io.Reader)

func HashSum(r io.Reader) {
  // hash generation
}

func validate(path string, fvs ...FileValidator) error {
  ...
}

// validate.go
validate("/some/path")

// deploy.go
validate("/some/path", HashSum)

This would make the change set smaller, and also mean the validate() function avoids doing too much.

@fgsch fgsch force-pushed the fgsch/issue_243 branch 2 times, most recently from 4804bb0 to 19784db Compare June 2, 2022 21:40
@fgsch
Copy link
Member Author

fgsch commented Jun 2, 2022

I've updated the PR based on your feedback.

I went for rebase to make things simpler for me but you can see the changes from the original PR here: https://github.com/fastly/cli/compare/5f058435994336ba942678f8e2f9c5165f577548..fgsch/issue_243

Let me know what you think.

Comment on lines 97 to 107
if fileValidator != nil {
err = fileValidator(f)
}

closeErr := f.Close()

if err != nil {
return fmt.Errorf("error closing package: %w", err)
return err
}
if closeErr != nil {
return fmt.Errorf("error closing file: %w", closeErr)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the if err != nil check can be moved inside of the if fileValidator != nil statement,
and closeErr renamed to just err.

So something like:

if fileValidator != nil {
	if err := fileValidator(f); err != nil {
		return err
	}
}

err = f.Close()
if err != nil {
	return fmt.Errorf("error closing file: %w", err)
}

Copy link
Member Author

@fgsch fgsch Jun 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for this is to avoid having to call f.Close() in two different places.
Are you saying that you want to omit the call entirely, or are you happy with the call being within the fileValidator error path?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, the alternative I wanted to avoid was:

if fileValidator != nil {
  if err := fileValidator(f); err != nil {
    f.Close()
    return err
  }
}

Copy link
Collaborator

@Integralist Integralist Jun 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK that's understandable. I actually missed the fact that you would have had to close in two places.

So what would be the practical outcome of not closing the file when we're in a CLI environment? My understanding is the moment the process stops the associated file descriptors will be automatically cleaned up.

If this was a long running server process, then I'd agree to always explicitly calling f.Close() but for a CLI I don't think it matters as much.

Also from a user experience, I personally don't feel it's a useful error to report back as a user can't do anything about it if it did fail so maybe if we do want to close the file we instead use defer and just report it into the error log using c.Globals.ErrLog.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the same could be said about many paths in the code.
Let's just keep the Close call for completeness but ignore the error as in the snippet above.

This moves the hash calculation at validate() tim to avoid opening
the package twice.

Fixes #243.
@fgsch fgsch force-pushed the fgsch/issue_243 branch from 19784db to 43ba5c2 Compare June 6, 2022 17:12
@fgsch fgsch merged commit 4f39b50 into main Jun 7, 2022
@fgsch fgsch deleted the fgsch/issue_243 branch June 7, 2022 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compute Edge hashsum should be the hash of main.wasm

3 participants