Skip to content
Permalink
Browse files

More go-like interface for netrc

  • Loading branch information...
frioux committed Apr 6, 2019
1 parent a9295b9 commit f950d9562836bd5d986a8085cdbd625a09846eb0
Showing with 44 additions and 38 deletions.
  1. +2 −2 internal/tool/netrcpassword/netrcPassword.go
  2. +6 −12 pkg/netrc/netrc.go
  3. +36 −24 pkg/netrc/netrc_test.go
@@ -28,8 +28,8 @@ func Run(args []string, _ io.Reader) error {
return errors.Wrap(err, "Couldn't parse netrc")
}

login := n.MachineAndLogin(args[1], args[2])
if login.IsZero() {
login, ok := n.MachineAndLogin(args[1], args[2])
if !ok {
return errors.New("Couldn't find login for " + args[2] + "@" + args[1])
}

@@ -23,12 +23,6 @@ type Login struct {
Name, Login, Password, Account, Macdef string
}

// IsZero tells whether you got a real Login or an (effectively) nil Login
func (l Login) IsZero() bool {
return !l.IsDefault && l.Name == "" && l.Login == "" && l.Password == "" &&
l.Account == "" && l.Macdef == ""
}

// Parse the netrc file at the given path
// It returns a Netrc instance
func Parse(path string) (Netrc, error) {
@@ -44,23 +38,23 @@ func Parse(path string) (Netrc, error) {
}

// Machine gets a login by machine name
func (n Netrc) Machine(name string) Login {
func (n Netrc) Machine(name string) (Login, bool) {
for _, m := range n {
if m.Name == name {
return m
return m, true
}
}
return Login{}
return Login{}, false
}

// MachineAndLogin gets a login by machine name and login name
func (n Netrc) MachineAndLogin(name, login string) Login {
func (n Netrc) MachineAndLogin(name, login string) (Login, bool) {
for _, m := range n {
if m.Name == name && m.Login == login {
return m
return m, true
}
}
return Login{}
return Login{}, false
}

func lex(file io.Reader) []string {
@@ -9,73 +9,85 @@ import (
func TestLogin(t *testing.T) {
f, err := Parse("./testdata/login.netrc")
assert.Nil(t, err)
heroku := f.Machine("api.heroku.com")
heroku, _ := f.Machine("api.heroku.com")
assert.Equal(t, "jeff@heroku.com", heroku.Login)
assert.Equal(t, "foo", heroku.Password)

heroku2 := f.MachineAndLogin("api.heroku.com", "jeff2@heroku.com")
heroku2, _ := f.MachineAndLogin("api.heroku.com", "jeff2@heroku.com")
assert.Equal(t, heroku2.Login, "jeff2@heroku.com")
assert.Equal(t, heroku2.Password, "bar")
}

func TestSampleMulti(t *testing.T) {
f, err := Parse("./testdata/sample_multi.netrc")
assert.Nil(t, err)
assert.Equal(t, f.Machine("m").Login, "lm")
assert.Equal(t, f.Machine("m").Password, "pm")
assert.Equal(t, f.Machine("n").Login, "ln")
assert.Equal(t, f.Machine("n").Password, "pn")
m, _ := f.Machine("m")
n, _ := f.Machine("n")
assert.Equal(t, m.Login, "lm")
assert.Equal(t, m.Password, "pm")
assert.Equal(t, n.Login, "ln")
assert.Equal(t, n.Password, "pn")
}

func TestSampleMultiWithDefault(t *testing.T) {
f, err := Parse("./testdata/sample_multi_with_default.netrc")
assert.Nil(t, err)
assert.Equal(t, f.Machine("m").Login, "lm")
assert.Equal(t, f.Machine("m").Password, "pm")
assert.Equal(t, f.Machine("n").Login, "ln")
assert.Equal(t, f.Machine("n").Password, "pn")

m, _ := f.Machine("m")
n, _ := f.Machine("n")
assert.Equal(t, m.Login, "lm")
assert.Equal(t, m.Password, "pm")
assert.Equal(t, n.Login, "ln")
assert.Equal(t, n.Password, "pn")
}

func TestNewlineless(t *testing.T) {
f, err := Parse("./testdata/newlineless.netrc")
assert.Nil(t, err)
assert.Equal(t, f.Machine("m").Login, "l")
assert.Equal(t, f.Machine("m").Password, "p")
m, _ := f.Machine("m")
assert.Equal(t, m.Login, "l")
assert.Equal(t, m.Password, "p")
}

func TestBadDefaultOrder(t *testing.T) {
f, err := Parse("./testdata/bad_default_order.netrc")
assert.Nil(t, err)
assert.Equal(t, f.Machine("mail.google.com").Login, "joe@gmail.com")
assert.Equal(t, f.Machine("mail.google.com").Password, "somethingSecret")
assert.Equal(t, f.Machine("ray").Login, "demo")
assert.Equal(t, f.Machine("ray").Password, "mypassword")
g, _ := f.Machine("mail.google.com")
r, _ := f.Machine("ray")
assert.Equal(t, g.Login, "joe@gmail.com")
assert.Equal(t, g.Password, "somethingSecret")
assert.Equal(t, r.Login, "demo")
assert.Equal(t, r.Password, "mypassword")
}

func TestDefaultOnly(t *testing.T) {
f, err := Parse("./testdata/default_only.netrc")
assert.Nil(t, err)
assert.Equal(t, f.Machine("default").Login, "ld")
assert.Equal(t, f.Machine("default").Password, "pd")
d, _ := f.Machine("default")
assert.Equal(t, d.Login, "ld")
assert.Equal(t, d.Password, "pd")
}

func TestGood(t *testing.T) {
f, err := Parse("./testdata/good.netrc")
assert.Nil(t, err)
assert.Equal(t, f.Machine("mail.google.com").Login, "joe@gmail.com")
assert.Equal(t, f.Machine("mail.google.com").Account, "justagmail")
assert.Equal(t, f.Machine("mail.google.com").Password, "somethingSecret")
g, _ := f.Machine("mail.google.com")
assert.Equal(t, g.Login, "joe@gmail.com")
assert.Equal(t, g.Account, "justagmail")
assert.Equal(t, g.Password, "somethingSecret")
}

func TestPassword(t *testing.T) {
f, err := Parse("./testdata/password.netrc")
assert.Nil(t, err)
assert.Equal(t, f.Machine("m").Password, "p")
m, _ := f.Machine("m")
assert.Equal(t, m.Password, "p")
}

func TestPermissive(t *testing.T) {
f, err := Parse("./testdata/permissive.netrc")
assert.Nil(t, err)
assert.Equal(t, f.Machine("m").Login, "l")
assert.Equal(t, f.Machine("m").Password, "p")
m, _ := f.Machine("m")
assert.Equal(t, m.Login, "l")
assert.Equal(t, m.Password, "p")
}

0 comments on commit f950d95

Please sign in to comment.
You can’t perform that action at this time.