9 "github.com/bgentry/go-netrc/netrc"
10 "github.com/mitchellh/go-homedir"
13 // addAuthFromNetrc adds auth information to the URL from the user's
14 // netrc file if it can be found. This will only add the auth info
15 // if the URL doesn't already have auth info specified and the
16 // the username is blank.
17 func addAuthFromNetrc(u *url.URL) error {
18 // If the URL already has auth information, do nothing
19 if u.User != nil && u.User.Username() != "" {
23 // Get the netrc file path
24 path := os.Getenv("NETRC")
27 if runtime.GOOS == "windows" {
32 path, err = homedir.Expand("~/" + filename)
38 // If the file is not a file, then do nothing
39 if fi, err := os.Stat(path); err != nil {
40 // File doesn't exist, do nothing
41 if os.IsNotExist(err) {
47 } else if fi.IsDir() {
48 // File is directory, ignore
52 // Load up the netrc file
53 net, err := netrc.ParseFile(path)
55 return fmt.Errorf("Error parsing netrc file at %q: %s", path, err)
58 machine := net.FindMachine(u.Host)
60 // Machine not found, no problem
65 u.User = url.UserPassword(machine.Login, machine.Password)