Skip to content
Permalink
Browse files

Replace Get method with fields on struct

  • Loading branch information...
frioux committed Apr 6, 2019
1 parent 10d64a4 commit b6127ba8621aa7f1efda0610a7cf283831b66289
Showing with 28 additions and 44 deletions.
  1. +1 −1 internal/tool/netrcpassword/netrcPassword.go
  2. +1 −17 pkg/netrc/netrc.go
  3. +26 −26 pkg/netrc/netrc_test.go
@@ -33,7 +33,7 @@ func Run(args []string, _ io.Reader) error {
return errors.New("Couldn't find login for " + args[2] + "@" + args[1])
}

fmt.Println(login.Get("password"))
fmt.Println(login.Password)

return nil
}
@@ -54,7 +54,7 @@ func (n Netrc) Machine(name string) *Login {
// MachineAndLogin gets a login by machine name and login name
func (n Netrc) MachineAndLogin(name, login string) *Login {
for _, m := range n.logins {
if m.Name == name && m.Get("login") == login {
if m.Name == name && m.Login == login {
return m
}
}
@@ -138,19 +138,3 @@ func parse(tokens []string) (Netrc, error) {
}
return n, nil
}

// Get a property from a machine
func (m *Login) Get(name string) string {
switch name {
case "login":
return m.Login
case "password":
return m.Password
case "account":
return m.Account
case "macdef":
return m.Macdef
default:
return "???"
}
}
@@ -10,72 +10,72 @@ func TestLogin(t *testing.T) {
f, err := Parse("./testdata/login.netrc")
assert.Nil(t, err)
heroku := f.Machine("api.heroku.com")
assert.Equal(t, "jeff@heroku.com", heroku.Get("login"))
assert.Equal(t, "foo", heroku.Get("password"))
assert.Equal(t, "jeff@heroku.com", heroku.Login)
assert.Equal(t, "foo", heroku.Password)

heroku2 := f.MachineAndLogin("api.heroku.com", "jeff2@heroku.com")
assert.Equal(t, heroku2.Get("login"), "jeff2@heroku.com")
assert.Equal(t, heroku2.Get("password"), "bar")
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").Get("login"), "lm")
assert.Equal(t, f.Machine("m").Get("password"), "pm")
assert.Equal(t, f.Machine("n").Get("login"), "ln")
assert.Equal(t, f.Machine("n").Get("password"), "pn")
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")
}

func TestSampleMultiWithDefault(t *testing.T) {
f, err := Parse("./testdata/sample_multi_with_default.netrc")
assert.Nil(t, err)
assert.Equal(t, f.Machine("m").Get("login"), "lm")
assert.Equal(t, f.Machine("m").Get("password"), "pm")
assert.Equal(t, f.Machine("n").Get("login"), "ln")
assert.Equal(t, f.Machine("n").Get("password"), "pn")
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")
}

func TestNewlineless(t *testing.T) {
f, err := Parse("./testdata/newlineless.netrc")
assert.Nil(t, err)
assert.Equal(t, f.Machine("m").Get("login"), "l")
assert.Equal(t, f.Machine("m").Get("password"), "p")
assert.Equal(t, f.Machine("m").Login, "l")
assert.Equal(t, f.Machine("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").Get("login"), "joe@gmail.com")
assert.Equal(t, f.Machine("mail.google.com").Get("password"), "somethingSecret")
assert.Equal(t, f.Machine("ray").Get("login"), "demo")
assert.Equal(t, f.Machine("ray").Get("password"), "mypassword")
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")
}

func TestDefaultOnly(t *testing.T) {
f, err := Parse("./testdata/default_only.netrc")
assert.Nil(t, err)
assert.Equal(t, f.Machine("default").Get("login"), "ld")
assert.Equal(t, f.Machine("default").Get("password"), "pd")
assert.Equal(t, f.Machine("default").Login, "ld")
assert.Equal(t, f.Machine("default").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").Get("login"), "joe@gmail.com")
assert.Equal(t, f.Machine("mail.google.com").Get("account"), "justagmail")
assert.Equal(t, f.Machine("mail.google.com").Get("password"), "somethingSecret")
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")
}

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

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

0 comments on commit b6127ba

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