Fix netrc writing bug

This commit is contained in:
Abel Luck 2026-02-27 09:53:24 +01:00
parent 29fe7f76c1
commit b0959fcf38
3 changed files with 13 additions and 7 deletions

View file

@ -25,7 +25,7 @@ func Upsert(path, machine, password string) error {
}
}
if !found {
entries = append(entries, entry{machine: machine, password: password})
entries = append(entries, entry{machine: machine, login: "dummy", password: password})
}
return write(path, entries)
@ -71,6 +71,7 @@ func GetPassword(path, machine string) (string, error) {
type entry struct {
machine string
login string
password string
}
@ -102,6 +103,10 @@ func parse(path string) ([]entry, error) {
entries = append(entries, *current)
}
current = &entry{machine: fields[1]}
case "login":
if current != nil {
current.login = fields[1]
}
case "password":
if current != nil {
current.password = fields[1]
@ -129,7 +134,7 @@ func write(path string, entries []entry) error {
if i > 0 {
b.WriteString("\n")
}
fmt.Fprintf(&b, "machine %s\npassword %s\n", e.machine, e.password)
fmt.Fprintf(&b, "machine %s\nlogin %s\npassword %s\n", e.machine, e.login, e.password)
}
if err := os.WriteFile(path, []byte(b.String()), 0600); err != nil {