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

@ -5,6 +5,7 @@
Changes yet to be released are documented here.
- Fix path expansion when running in systemd
- Fix bug in netrc writing
- Add nixos, home-manager, and darwin-nix modules
## v0.1.0

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 {

View file

@ -27,7 +27,7 @@ func TestUpsertUpdateExisting(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "netrc")
initial := "machine other.host\npassword otherpass\n\nmachine cache.example.com\npassword oldtoken\n"
initial := "machine other.host\nlogin dummy\npassword otherpass\n\nmachine cache.example.com\nlogin dummy\npassword oldtoken\n"
if err := os.WriteFile(path, []byte(initial), 0600); err != nil {
t.Fatal(err)
}
@ -59,7 +59,7 @@ func TestUpsertAppend(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "netrc")
initial := "machine existing.host\npassword existingpass\n"
initial := "machine existing.host\nlogin dummy\npassword existingpass\n"
if err := os.WriteFile(path, []byte(initial), 0600); err != nil {
t.Fatal(err)
}
@ -89,7 +89,7 @@ func TestRemove(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "netrc")
initial := "machine keep.host\npassword keeppass\n\nmachine remove.host\npassword removepass\n"
initial := "machine keep.host\nlogin dummy\npassword keeppass\n\nmachine remove.host\nlogin dummy\npassword removepass\n"
if err := os.WriteFile(path, []byte(initial), 0600); err != nil {
t.Fatal(err)
}
@ -141,7 +141,7 @@ func TestGetPasswordNotFound(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "netrc")
content := "machine other.host\npassword otherpass\n"
content := "machine other.host\nlogin dummy\npassword otherpass\n"
if err := os.WriteFile(path, []byte(content), 0600); err != nil {
t.Fatal(err)
}
@ -179,7 +179,7 @@ func TestFilePermissionsCorrected(t *testing.T) {
path := filepath.Join(dir, "netrc")
// Create file with overly permissive mode
if err := os.WriteFile(path, []byte("machine old.host\npassword oldpass\n"), 0644); err != nil {
if err := os.WriteFile(path, []byte("machine old.host\nlogin dummy\npassword oldpass\n"), 0644); err != nil {
t.Fatal(err)
}