-
Notifications
You must be signed in to change notification settings - Fork 3
/
netrc-password.go
47 lines (37 loc) · 904 Bytes
/
netrc-password.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package netrcpassword
import (
"errors"
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"github.com/frioux/leatherman/pkg/netrc"
)
func Run(args []string, _ io.Reader) error {
if len(args) != 3 {
fmt.Println("Usage:\n\tnetrc-password $machine $login")
os.Exit(1)
}
usr, err := user.Current()
if err != nil {
return fmt.Errorf("Couldn't get current user: %w", err)
}
password, err := run(filepath.Join(usr.HomeDir, ".netrc"), args[1], args[2])
if err != nil {
return fmt.Errorf("Couldn't load password: %w", err)
}
fmt.Println(password)
return nil
}
func run(path, machine, user string) (string, error) {
n, err := netrc.Parse(path)
if err != nil {
return "", fmt.Errorf("Couldn't parse netrc: %w", err)
}
login, ok := n.MachineAndLogin(machine, user)
if !ok {
return "", errors.New("Couldn't find login for " + user + "@" + machine)
}
return login.Password, nil
}