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
31 func FindInNetrc(host string) (string, string) {
32 netrcPath, ok := os.LookupEnv("NETRC")
34 homeDir, err := os.UserHomeDir()
38 netrcPath = filepath.Join(homeDir, ".netrc")
40 data, err := ioutil.ReadFile(netrcPath)
42 if errors.Is(err, fs.ErrNotExist) {
48 var machine, login, password string
49 for _, line := range strings.Split(string(data), "\n") {
56 fields := strings.Fields(line)
58 for ; i < len(fields)-1; i += 2 {
69 password = fields[i+1]
73 if machine != "" && login != "" && password != "" {
75 return login, password
77 machine, login, password = "", "", ""
80 if i < len(fields) && fields[i] == "default" {