forked from bgentry/go-netrc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
Showing
9 changed files
with
577 additions
and
388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.