1 // mmc -- Mattermost client
2 // Copyright (C) 2023 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // Nearly all code is taken from src/cmd/go/internal/auth/netrc.go
30 func FindInNetrc(host string) (string, string) {
31 netrcPath, ok := os.LookupEnv("NETRC")
33 homeDir, err := os.UserHomeDir()
37 netrcPath = filepath.Join(homeDir, ".netrc")
39 data, err := os.ReadFile(netrcPath)
41 if errors.Is(err, fs.ErrNotExist) {
47 var machine, login, password string
48 for _, line := range strings.Split(string(data), "\n") {
55 fields := strings.Fields(line)
57 for ; i < len(fields)-1; i += 2 {
67 password = fields[i+1]
71 if machine != "" && login != "" && password != "" {
73 return login, password
75 machine, login, password = "", "", ""
78 if i < len(fields) && fields[i] == "default" {