]> Sergey Matveev's repositories - mmc.git/blob - netrc.go
ioutil is deprecated
[mmc.git] / netrc.go
1 // mmc -- Mattermost client
2 // Copyright (C) 2023 Sergey Matveev <stargrave@stargrave.org>
3 //
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
7 // License.
8 //
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.
13 //
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/>.
16
17 // Nearly all code is taken from src/cmd/go/internal/auth/netrc.go
18
19 package mmc
20
21 import (
22         "errors"
23         "io/fs"
24         "log"
25         "os"
26         "path/filepath"
27         "strings"
28 )
29
30 func FindInNetrc(host string) (string, string) {
31         netrcPath, ok := os.LookupEnv("NETRC")
32         if !ok {
33                 homeDir, err := os.UserHomeDir()
34                 if err != nil {
35                         log.Fatalln(err)
36                 }
37                 netrcPath = filepath.Join(homeDir, ".netrc")
38         }
39         data, err := os.ReadFile(netrcPath)
40         if err != nil {
41                 if errors.Is(err, fs.ErrNotExist) {
42                         return "", ""
43                 }
44                 log.Fatalln(err)
45         }
46         inMacro := false
47         var machine, login, password string
48         for _, line := range strings.Split(string(data), "\n") {
49                 if inMacro {
50                         if line == "" {
51                                 inMacro = false
52                         }
53                         continue
54                 }
55                 fields := strings.Fields(line)
56                 i := 0
57                 for ; i < len(fields)-1; i += 2 {
58                         switch fields[i] {
59                         case "machine":
60                                 machine = fields[i+1]
61                                 login = ""
62                                 password = ""
63                         case "default":
64                                 break
65                         case "login":
66                                 login = fields[i+1]
67                         case "password":
68                                 password = fields[i+1]
69                         case "macdef":
70                                 inMacro = true
71                         }
72                         if machine != "" && login != "" && password != "" {
73                                 if machine == host {
74                                         return login, password
75                                 }
76                                 machine, login, password = "", "", ""
77                         }
78                 }
79                 if i < len(fields) && fields[i] == "default" {
80                         break
81                 }
82         }
83         return "", ""
84 }