]> Sergey Matveev's repositories - mmc.git/commitdiff
External netrc module
authorSergey Matveev <stargrave@stargrave.org>
Sat, 12 Aug 2023 12:05:02 +0000 (15:05 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sat, 12 Aug 2023 12:05:02 +0000 (15:05 +0300)
cmd/ch-leave/main.go
cmd/mmc/main.go
cmd/sb/main.go
go.mod
go.sum
netrc.go [deleted file]

index 0d09e2d4d8b958f884f2a441c995e02e16b27486..18c05b7374fae40a5a9c4860479df2851055cd96 100644 (file)
@@ -21,6 +21,7 @@ import (
        "log"
 
        "github.com/mattermost/mattermost-server/v6/model"
+       "go.cypherpunks.ru/netrc"
        "go.stargrave.org/mmc"
 )
 
@@ -30,7 +31,7 @@ func main() {
        log.SetFlags(log.Lshortfile)
 
        chId := flag.Arg(0)
-       login, password := mmc.FindInNetrc(*entrypoint)
+       login, password := netrc.Find(*entrypoint)
        if login == "" || password == "" {
                log.Fatalln("no credentials found for:", *entrypoint)
        }
index b38738db555e5799dda0720e5c95f2f7bd354fea..76f92c5d5b8ae5351715bc46d240f9dc2bb23f9c 100644 (file)
@@ -38,6 +38,7 @@ import (
 
        "github.com/davecgh/go-spew/spew"
        "github.com/mattermost/mattermost-server/v6/model"
+       "go.cypherpunks.ru/netrc"
        "go.stargrave.org/mmc"
 )
 
@@ -85,7 +86,7 @@ func main() {
        }
        defer DebugFd.Close()
 
-       login, password := mmc.FindInNetrc(*entrypoint)
+       login, password := netrc.Find(*entrypoint)
        if login == "" || password == "" {
                log.Fatalln("no credentials found for:", *entrypoint)
        }
index ddb29b68f44dc4c559de58d690bbee3dc2a93224..e7a4cbdd46806652048ae630ccb3cac87121d780 100644 (file)
@@ -24,6 +24,7 @@ import (
 
        "github.com/mattermost/mattermost-server/v6/model"
        "go.cypherpunks.ru/recfile"
+       "go.cypherpunks.ru/netrc"
        "go.stargrave.org/mmc"
 )
 
@@ -37,7 +38,7 @@ func main() {
 
        chId := flag.Arg(0)
        postId := flag.Arg(1)
-       login, password := mmc.FindInNetrc(*entrypoint)
+       login, password := netrc.Find(*entrypoint)
        if login == "" || password == "" {
                log.Fatalln("no credentials found for:", *entrypoint)
        }
diff --git a/go.mod b/go.mod
index 58f5f3f89e2bd5a9b599b8b7b31d551934c867a8..37d1fb5d6cf0a8789176e96c8077e9e613b8d4c1 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -6,8 +6,9 @@ require (
        github.com/davecgh/go-spew v1.1.1
        github.com/fsnotify/fsnotify v1.6.0
        github.com/mattermost/mattermost-server/v6 v6.7.2
+       go.cypherpunks.ru/netrc v0.1.0
        go.cypherpunks.ru/recfile v0.7.0
-       golang.org/x/sys v0.6.0
+       golang.org/x/sys v0.11.0
 )
 
 require (
diff --git a/go.sum b/go.sum
index f8809dc145dcdba1d7e728d45a464aba2b06e85b..69d750562de6452a5129b0b988b3dcc7e98cedae 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -1352,6 +1352,8 @@ github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPS
 github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg=
 github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
 gitlab.com/nyarla/go-crypt v0.0.0-20160106005555-d9a5dc2b789b/go.mod h1:T3BPAOm2cqquPa0MKWeNkmOM5RQsRhkrwMWonFMN7fE=
+go.cypherpunks.ru/netrc v0.1.0 h1:dzaSh4lgpPXvLzUME/hTSegg+Uw1aPEmC79fxbPLFGk=
+go.cypherpunks.ru/netrc v0.1.0/go.mod h1:ZmQaL9ENEII++WQooelccr33XZcBhKd8VgtcICf4SIE=
 go.cypherpunks.ru/recfile v0.7.0 h1:0R1UnDGKccp7JnC66msslJMlSY02jHx/XkW+ISl0GuY=
 go.cypherpunks.ru/recfile v0.7.0/go.mod h1:sR+KajB+vzofL3SFVFwKt3Fke0FaCcN1g3YPNAhU3qI=
 go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
@@ -1700,8 +1702,8 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc
 golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20220403205710-6acee93ad0eb/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
-golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
+golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
diff --git a/netrc.go b/netrc.go
deleted file mode 100644 (file)
index 18fa7b1..0000000
--- a/netrc.go
+++ /dev/null
@@ -1,83 +0,0 @@
-// mmc -- Mattermost client
-// Copyright (C) 2023 Sergey Matveev <stargrave@stargrave.org>
-//
-// 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 <http://www.gnu.org/licenses/>.
-
-// Nearly all code is taken from src/cmd/go/internal/auth/netrc.go
-
-package mmc
-
-import (
-       "errors"
-       "io/fs"
-       "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 := os.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":
-                       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 "", ""
-}