// mmc -- Mattermost client // Copyright (C) 2023 Sergey Matveev // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 of the // License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . // Nearly all code is taken from src/cmd/go/internal/auth/netrc.go package mmc import ( "errors" "io/fs" "io/ioutil" "log" "os" "path/filepath" "strings" ) func FindInNetrc(host string) (string, string) { netrcPath, ok := os.LookupEnv("NETRC") if !ok { homeDir, err := os.UserHomeDir() if err != nil { log.Fatalln(err) } netrcPath = filepath.Join(homeDir, ".netrc") } data, err := ioutil.ReadFile(netrcPath) if err != nil { if errors.Is(err, fs.ErrNotExist) { return "", "" } log.Fatalln(err) } inMacro := false var machine, login, password string for _, line := range strings.Split(string(data), "\n") { if inMacro { if line == "" { inMacro = false } continue } fields := strings.Fields(line) i := 0 for ; i < len(fields)-1; i += 2 { switch fields[i] { case "machine": machine = fields[i+1] login = "" password = "" case "default": break case "login": login = fields[i+1] case "password": password = fields[i+1] case "macdef": inMacro = true } if machine != "" && login != "" && password != "" { if machine == host { return login, password } machine, login, password = "", "", "" } } if i < len(fields) && fields[i] == "default" { break } } return "", "" }