netrc

package module
v0.0.0-...-bfa2d42 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2021 License: MIT Imports: 8 Imported by: 0

README

netrc

This is a simple implementation for reading a .netrc file for a user's credentials for services

Build Status Coverage Status

Installation

$ go get github.com/Keithbsmiley/netrc

The add it to your import

import (
  "github.com/Keithbsmiley/netrc"
)
Development

At the moment this library still has some major feature omissions. Such as setting new credentials in the user's netrc. I plan on adding these soon and I will write a better usage guide then. For now most of the methods are commented and you can view netrc_test to see some of the method calls.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileExists

func FileExists(path string) bool

Check to see if the file exists at the given path

func FilePath

func FilePath(path string) string

Creates a file path based on the passed string

if the empty string is passed $HOME/.netrc is used
if a filename is passed ex: .mynetrc it is assumed to be in your
  $HOME directory, ex: $HOME/.mynetrc
if a path starting with a / is passed it is used without modification

this uses path/filepath's Join(...) function so you can also pass
  relative paths ex: "../.netrc" would return /Users/.netrc on OS X

func ValidPermissions

func ValidPermissions(path string) bool

Check the file permissions at the given path

600 is valid, all others are invalid
returns false if there is an issue with the path

Types

type Cred

type Cred struct {
	User string
	Pass string
}

A simple credential struct for a username and password

func CreateCred

func CreateCred(u string, p string) (*Cred, error)

Creates a new Cred struct with a user and pass

u: a username
p: a password
if either u or p are the empty string an error is returned
otherwise a pointer to the struct is returned

type Netrc

type Netrc struct {
	Path  string
	Creds map[string]*Cred
}

A struct for the Netrc

Path is the netrc filepath
Creds is a map of string keys that have pointers to Cred structs
The keys relate to the service name from the netrc or 'default'
ex: 'machine github.com' Then the key is github.com

func Open

func Open(path string) *Netrc

Opens the given path if it exists If the path is invalid a fatal error is thrown A new Netrc is created and the file is read into the Creds map A pointer to the Netrc will always be returned

func OpenDefault

func OpenDefault() *Netrc

A convenience method for opening the default .netrc

Probably in $HOME/.netrc

func (*Netrc) GetValue

func (n *Netrc) GetValue(key string, def bool) (string, string, error)

Get a username and password for a service

This must be called from a valid netrc instace that has been Read

Params

key: the string following machine in the netrc that identifies the service
def: pass true if you want the default credentials (if they exist)
	 if there aren't any credentials for the given key

Return

string: the username
string: the password
error:	an error if one occurs
		 either because of a no credentials or an invalid key

func (*Netrc) Read

func (n *Netrc) Read()

Read the netrc from the file

This must be called from a valid netrc instance that has a Path

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL