Source File
config.go
Belonging Package
github.com/kevinburke/ssh_config
package ssh_config
import (
osuser
)
const version = "1.0"
var _ = version
type configFinder func() string
type UserSettings struct {
IgnoreErrors bool
systemConfig *Config
systemConfigFinder configFinder
userConfig *Config
userConfigFinder configFinder
loadConfigs sync.Once
onceErr error
}
func () string {
, := osuser.Current()
if == nil {
return .HomeDir
} else {
return os.Getenv("HOME")
}
}
func () string {
return filepath.Join(homedir(), ".ssh", "config")
}
var DefaultUserSettings = &UserSettings{
IgnoreErrors: false,
systemConfigFinder: systemConfigFinder,
userConfigFinder: userConfigFinder,
}
func () string {
return filepath.Join("/", "etc", "ssh", "ssh_config")
}
func ( *Config, , string) (string, error) {
if == nil {
return "", nil
}
, := .Get(, )
if != nil || == "" {
return "",
}
if := validate(, ); != nil {
return "",
}
return , nil
}
func (, string) string {
return DefaultUserSettings.Get(, )
}
func (, string) (string, error) {
return DefaultUserSettings.GetStrict(, )
}
func ( *UserSettings) (, string) string {
, := .GetStrict(, )
if != nil {
return ""
}
return
}
func ( *UserSettings) (, string) (string, error) {
var string
if .userConfigFinder == nil {
= userConfigFinder()
} else {
= .userConfigFinder()
}
var error
if != nil && os.IsNotExist() == false {
.onceErr =
return
}
if .systemConfigFinder == nil {
= systemConfigFinder()
} else {
= .systemConfigFinder()
}
if != nil && os.IsNotExist() == false {
.onceErr =
return
}
if .onceErr != nil && .IgnoreErrors == false {
return "", .onceErr
}
, := findVal(.userConfig, , )
if != nil || != "" {
return ,
}
, := findVal(.systemConfig, , )
if != nil || != "" {
return ,
}
return Default(), nil
}
func ( string) (*Config, error) {
return parseWithDepth(, 0)
}
func ( string, uint8) (*Config, error) {
, := ioutil.ReadFile()
if != nil {
return nil,
}
return decodeBytes(, isSystem(), )
}
func ( io.Reader) (*Config, error) {
, := ioutil.ReadAll()
if != nil {
return nil,
}
return decodeBytes(, false, 0)
}
func ( []byte, bool, uint8) ( *Config, error) {
defer func() {
if := recover(); != nil {
if , := .(runtime.Error); {
panic()
}
if , := .(error); && == ErrDepthExceeded {
=
return
}
= errors.New(.(string))
}
}()
= parseSSH(lexSSH(), , )
return ,
}
var specialBytes = []byte(`\.+()|[]{}^$`)
func ( byte) bool {
return bytes.IndexByte(specialBytes, ) >= 0
}
switch := []; {
case '*':
.WriteString(".*")
case '?':
.WriteString(".?")
if .implicit == false {
.WriteString(strings.Repeat(" ", int(.leadingSpace)))
.WriteString("Host")
if .hasEquals {
.WriteString(" = ")
} else {
.WriteString(" ")
}
for , := range .Patterns {
.WriteString(.String())
if < len(.Patterns)-1 {
.WriteString(" ")
}
}
if .EOLComment != "" {
.WriteString(" #")
.WriteString(.EOLComment)
}
.WriteByte('\n')
}
for := range .Nodes {
.WriteString(.Nodes[].String())
.WriteByte('\n')
}
return .String()
}
var ErrDepthExceeded = errors.New("ssh_config: max recurse depth exceeded")
:= make([]string, 0)
for := range {
var string
if filepath.IsAbs([]) {
= []
} else if {
= filepath.Join("/etc/ssh", [])
} else {
= filepath.Join(homedir(), ".ssh", [])
}
, := filepath.Glob()
if != nil {
return nil,
}
= append(, ...)
}
= removeDups()
.matches =
for := range {
, := parseWithDepth([], )
if != nil {
return nil,
}
.files[[]] =
}
return , nil
}
func ( *Include) () string {
:= " "
if .hasEquals {
= " = "
}
:= fmt.Sprintf("%sInclude%s%s", strings.Repeat(" ", int(.leadingSpace)), , strings.Join(.directives, " "))
if .Comment != "" {
+= " #" + .Comment
}
return
}
var matchAll *Pattern
func () {
var error
matchAll, = NewPattern("*")
if != nil {
panic()
}
}
func () *Config {
return &Config{
Hosts: []*Host{
&Host{
implicit: true,
Patterns: []*Pattern{matchAll},
Nodes: make([]Node, 0),
},
},
depth: 0,
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |