From f2bb50fe14334fece2bd989b98e22bc864e618ad Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sat, 12 Aug 2023 15:05:02 +0300 Subject: [PATCH 1/1] External netrc module --- cmd/ch-leave/main.go | 3 +- cmd/mmc/main.go | 3 +- cmd/sb/main.go | 3 +- go.mod | 3 +- go.sum | 6 ++-- netrc.go | 83 -------------------------------------------- 6 files changed, 12 insertions(+), 89 deletions(-) delete mode 100644 netrc.go diff --git a/cmd/ch-leave/main.go b/cmd/ch-leave/main.go index 0d09e2d..18c05b7 100644 --- a/cmd/ch-leave/main.go +++ b/cmd/ch-leave/main.go @@ -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) } diff --git a/cmd/mmc/main.go b/cmd/mmc/main.go index b38738d..76f92c5 100644 --- a/cmd/mmc/main.go +++ b/cmd/mmc/main.go @@ -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) } diff --git a/cmd/sb/main.go b/cmd/sb/main.go index ddb29b6..e7a4cbd 100644 --- a/cmd/sb/main.go +++ b/cmd/sb/main.go @@ -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 58f5f3f..37d1fb5 100644 --- 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 f8809dc..69d7505 100644 --- 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 index 18fa7b1..0000000 --- a/netrc.go +++ /dev/null @@ -1,83 +0,0 @@ -// 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" - "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 "", "" -} -- 2.44.0