Documentation
¶
Overview ¶
Package netrc provides functionality for reading and parsing .netrc files.
Security Note: .netrc files store credentials in plaintext and should have restrictive permissions (0600) to prevent unauthorized access. This package validates file permissions and will return an error if the file is readable by others.
Example usage:
// Get credentials for a specific host
creds, found, err := netrc.GetHostCredentials("example.com")
if err != nil {
log.Fatal(err)
}
if found {
fmt.Printf("User: %s\n", creds.Login)
}
// Use a specific .netrc file
f, err := netrc.NewFile("/path/to/.netrc")
if err != nil {
log.Fatal(err)
}
creds, found, err := f.GetCredentials("example.com")
Index ¶
Constants ¶
const DefaultNetrcFilename = ".netrc"
DefaultNetrcFilename is the standard filename for netrc files.
Variables ¶
This section is empty.
Functions ¶
func DefaultPath ¶
DefaultPath returns the path to the default .netrc file. It checks NETRC, USERPROFILE, HOME environment variables, then os.UserHomeDir().
func ParseNetrcFile ¶
func ParseNetrcFile(content string, targetHost string) (login, password string, found bool, err error)
ParseNetrcFile parses .netrc file content and returns credentials for the given host. It aims to follow common .netrc conventions, including handling of 'machine', 'default', 'login', 'password' keywords, quoted strings with escapes, and skipping comments/macdef. The first complete 'machine' entry matching targetHost wins. If no specific machine matches, the first complete 'default' entry wins.
Types ¶
type Credentials ¶
Credentials represents a username and password pair from a .netrc file. It holds the Login (username) and Password.
func GetHostCredentials ¶
func GetHostCredentials(host string) (Credentials, bool, error)
GetHostCredentials is a convenience function that gets credentials for the specified host using the default .netrc path.
func (Credentials) IsEmpty ¶
func (c Credentials) IsEmpty() bool
IsEmpty returns true if both Login and Password are empty.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File represents a .netrc file and provides methods to retrieve credentials. Its main purpose is to encapsulate the file path.
func NewFile ¶
NewFile creates a new File instance for the specified .netrc file path. If the provided path is empty, it uses the default .netrc file location.
func (*File) GetCredentials ¶
func (f *File) GetCredentials(host string) (Credentials, bool, error)
GetCredentials reads the .netrc file and parses it to find credentials for the specified host. If the .netrc file does not exist, it returns false for found and no error.