Skip to content

Commit

Permalink
Tantique Mutationes
Browse files Browse the repository at this point in the history
  - Split into multiple files for better logical separation
    (makes things a lot easier to digest)

  - Add "Equal" methods to both *Netrc and *Machine

  - Add "Visit" method to *Netrc allowing caller to execute a function
    against each *Machine.

  - Add tests (and supporting testdata) as appropriate
  • Loading branch information
tep committed May 27, 2019
1 parent 5baba36 commit 72925ea
Show file tree
Hide file tree
Showing 9 changed files with 577 additions and 388 deletions.
20 changes: 20 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package netrc

import "fmt"

// Error represents a netrc file parse error.
type Error struct {
LineNum int // Line number
Msg string // Error message
}

// Error returns a string representation of error e.
func (e *Error) Error() string {
return fmt.Sprintf("line %d: %s", e.LineNum, e.Msg)
}

func (e *Error) BadDefaultOrder() bool {
return e.Msg == errBadDefaultOrder
}

const errBadDefaultOrder = "default token must appear after all machine tokens"
109 changes: 109 additions & 0 deletions machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package netrc

import (
"strings"
)

// Machine contains information about a remote machine.
type Machine struct {
Name string
Login string
Password string
Account string

nametoken *token
logintoken *token
passtoken *token
accounttoken *token
}

func (n *Netrc) NewMachine(name, login, password, account string) *Machine {
n.updateLock.Lock()
defer n.updateLock.Unlock()

prefix := "\n"
if len(n.tokens) == 0 {
prefix = ""
}
m := &Machine{
Name: name,
Login: login,
Password: password,
Account: account,

nametoken: &token{
kind: tkMachine,
rawkind: []byte(prefix + "machine"),
value: name,
rawvalue: []byte(" " + name),
},
logintoken: &token{
kind: tkLogin,
rawkind: []byte("\n\tlogin"),
value: login,
rawvalue: []byte(" " + login),
},
passtoken: &token{
kind: tkPassword,
rawkind: []byte("\n\tpassword"),
value: password,
rawvalue: []byte(" " + password),
},
accounttoken: &token{
kind: tkAccount,
rawkind: []byte("\n\taccount"),
value: account,
rawvalue: []byte(" " + account),
},
}
n.insertMachineTokensBeforeDefault(m)
for i := range n.machines {
if n.machines[i].IsDefault() {
n.machines = append(append(n.machines[:i], m), n.machines[i:]...)
return m
}
}
n.machines = append(n.machines, m)
return m
}

// IsDefault returns true if the machine is a "default" token, denoted by an
// empty name.
func (m *Machine) IsDefault() bool {
return m.Name == ""
}

// UpdatePassword sets the password for the Machine m.
func (m *Machine) UpdatePassword(newpass string) {
m.Password = newpass
updateTokenValue(m.passtoken, newpass)
}

// UpdateLogin sets the login for the Machine m.
func (m *Machine) UpdateLogin(newlogin string) {
m.Login = newlogin
updateTokenValue(m.logintoken, newlogin)
}

// UpdateAccount sets the login for the Machine m.
func (m *Machine) UpdateAccount(newaccount string) {
m.Account = newaccount
updateTokenValue(m.accounttoken, newaccount)
}

func (m *Machine) Equal(o *Machine) bool {
switch {
case m == nil && o == nil:
return true
case m == nil || o == nil:
return false
default:
return m.Name == o.Name && m.Login == o.Login && m.Password == o.Password && m.Account == o.Account
}
}

const keysep = "\000"

func (m *Machine) key() string {
return strings.Join([]string{m.Login, m.Account, m.Name}, keysep)
}
28 changes: 28 additions & 0 deletions macros.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package netrc

// Macros contains all the macro definitions in a netrc file.
type Macros map[string]string

func (m Macros) equal(o Macros) bool {
if m == nil && o == nil {
return true
}

if m == nil || o == nil {
return false
}

for k, mv := range m {
if ov, ok := o[k]; !ok || mv != ov {
return false
}
}

for k := range o {
if _, ok := m[k]; !ok {
return false
}
}

return true
}
Loading

0 comments on commit 72925ea

Please sign in to comment.