Use sync.OnceValues (CL 451356, since Go 1.21) instead of sync.Once for
cleaner code and less global variables, preventing their potential
misuse.
Change-Id: I9d7ccc42847fe77af1757672c31bb39e20007f92
Reviewed-on: https://go-review.googlesource.com/c/go/+/611016
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
// AddCredentials fills in the user's credentials for req, if any.
// The return value reports whether any matching credentials were found.
func AddCredentials(req *http.Request) (added bool) {
+ netrc, _ := readNetrc()
+ if len(netrc) == 0 {
+ return false
+ }
+
host := req.Host
if host == "" {
host = req.URL.Hostname()
}
// TODO(golang.org/issue/26232): Support arbitrary user-provided credentials.
- netrcOnce.Do(readNetrc)
for _, l := range netrc {
if l.machine == host {
req.SetBasicAuth(l.login, l.password)
password string
}
-var (
- netrcOnce sync.Once
- netrc []netrcLine
- netrcErr error
-)
-
func parseNetrc(data string) []netrcLine {
// See https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html
// for documentation on the .netrc format.
return filepath.Join(dir, base), nil
}
-func readNetrc() {
+var readNetrc = sync.OnceValues(func() ([]netrcLine, error) {
path, err := netrcPath()
if err != nil {
- netrcErr = err
- return
+ return nil, err
}
data, err := os.ReadFile(path)
if err != nil {
- if !os.IsNotExist(err) {
- netrcErr = err
+ if os.IsNotExist(err) {
+ err = nil
}
- return
+ return nil, err
}
- netrc = parseNetrc(string(data))
-}
+ return parseNetrc(string(data)), nil
+})