]> Sergey Matveev's repositories - mmc.git/blob - netrc.go
Ability to invoke cmd/download outside state directory
[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         "io/ioutil"
25         "log"
26         "os"
27         "path/filepath"
28         "strings"
29 )
30
31 func FindInNetrc(host string) (string, string) {
32         netrcPath, ok := os.LookupEnv("NETRC")
33         if !ok {
34                 homeDir, err := os.UserHomeDir()
35                 if err != nil {
36                         log.Fatalln(err)
37                 }
38                 netrcPath = filepath.Join(homeDir, ".netrc")
39         }
40         data, err := ioutil.ReadFile(netrcPath)
41         if err != nil {
42                 if errors.Is(err, fs.ErrNotExist) {
43                         return "", ""
44                 }
45                 log.Fatalln(err)
46         }
47         inMacro := false
48         var machine, login, password string
49         for _, line := range strings.Split(string(data), "\n") {
50                 if inMacro {
51                         if line == "" {
52                                 inMacro = false
53                         }
54                         continue
55                 }
56                 fields := strings.Fields(line)
57                 i := 0
58                 for ; i < len(fields)-1; i += 2 {
59                         switch fields[i] {
60                         case "machine":
61                                 machine = fields[i+1]
62                                 login = ""
63                                 password = ""
64                         case "default":
65                                 break
66                         case "login":
67                                 login = fields[i+1]
68                         case "password":
69                                 password = fields[i+1]
70                         case "macdef":
71                                 inMacro = true
72                         }
73                         if machine != "" && login != "" && password != "" {
74                                 if machine == host {
75                                         return login, password
76                                 }
77                                 machine, login, password = "", "", ""
78                         }
79                 }
80                 if i < len(fields) && fields[i] == "default" {
81                         break
82                 }
83         }
84         return "", ""
85 }