Skip to content

Fix file reading in cmd/jwt #450

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions cmd/jwt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@
return nil, err
}
rdr = f
if err := f.Close(); err != nil {
return nil, err
}
defer f.Close()

Check failure on line 94 in cmd/jwt/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `f.Close` is not checked (errcheck)
Copy link
Collaborator

@oxisto oxisto Jul 20, 2025

Choose a reason for hiding this comment

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

I think we need to do something like

defer func() {
if err := f.Close(); err != nil {
 // Somehow "handle" the error
}
}()

to silence the linter

Copy link
Contributor

Choose a reason for hiding this comment

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

One way that it can be handled to allow returning the error value to the caller, as well as other errors, is to use a helper function like this one with a named return value;

// Helper func:  Pass in the function directly rather than calling the function
func joinErr(err *error, op func() error) {
  *err = errors.Join(*err, op())
}

// Helper func:  Read input from specified file or stdin
func loadData(p string) (_ []byte, err error) {
	if p == "" {
		return nil, fmt.Errorf("no path specified")
	}

	var rdr io.Reader
	switch p {
	case "-":
		rdr = os.Stdin
	case "+":
		return []byte("{}"), nil
	default:
		f, err := os.Open(p)
		if err != nil {
			return nil, err
		}
		rdr = f
		defer joinErr(&err, f.Close)
	}
	return io.ReadAll(rdr)
}

VictoriaMetrics has a good blogpost on this as well as Uber having run into a similar issue and creating an "Invoker" interface.

Copy link
Contributor

Choose a reason for hiding this comment

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

As a note, the function needs to use a named return value, which is a first for me (I've not really used named returns before), otherwise the deferred function updates the error value that is scoped to the function (and is already discarded)

Details here; https://gophers.slack.com/archives/C02A8LZKT/p1754303913406599

}
return io.ReadAll(rdr)
}
Expand Down
Loading