use Default() and validate()

This commit is contained in:
Kevin Burke
2017-05-24 21:30:13 -07:00
parent c20644453d
commit 1c4ddb97d7
3 changed files with 66 additions and 6 deletions
+19 -6
View File
@@ -82,7 +82,14 @@ func findVal(c *Config, alias, key string) (string, error) {
if c == nil {
return "", nil
}
return c.Get(alias, key)
val, err := c.Get(alias, key)
if err != nil || val == "" {
return "", err
}
if err := validate(key, val); err != nil {
return "", err
}
return val, nil
}
// Get finds the first value for key within a declaration that matches the
@@ -98,8 +105,9 @@ func Get(alias, key string) string {
}
// GetStrict finds the first value for key within a declaration that matches the
// alias. For more information on how patterns are matched, see the manpage for
// ssh_config.
// alias. If key has a default value and no matching configuration is found, the
// default will be returned. For more information on default values and the way
// patterns are matched, see the manpage for ssh_config.
//
// error will be non-nil if and only if a user's configuration file or the
// system configuration file could not be parsed, and u.IgnoreErrors is false.
@@ -124,8 +132,9 @@ func (u *UserSettings) Get(alias, key string) string {
}
// GetStrict finds the first value for key within a declaration that matches the
// alias. For more information on how patterns are matched, see the manpage for
// ssh_config.
// alias. If key has a default value and no matching configuration is found, the
// default will be returned. For more information on default values and the way
// patterns are matched, see the manpage for ssh_config.
//
// error will be non-nil if and only if a user's configuration file or the
// system configuration file could not be parsed, and u.IgnoreErrors is false.
@@ -162,7 +171,11 @@ func (u *UserSettings) GetStrict(alias, key string) (string, error) {
if err != nil || val != "" {
return val, err
}
return findVal(u.systemConfig, alias, key)
val2, err2 := findVal(u.systemConfig, alias, key)
if err2 != nil || val2 != "" {
return val2, err2
}
return Default(key), nil
}
func parseFile(filename string) (*Config, error) {
+45
View File
@@ -52,6 +52,51 @@ func TestGet(t *testing.T) {
}
}
func TestGetWithDefault(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/config1"),
}
val, err := us.GetStrict("wap", "PasswordAuthentication")
if err != nil {
t.Fatalf("expected nil err, got %v", err)
}
if val != "yes" {
t.Errorf("expected to get PasswordAuthentication yes, got %q", val)
}
}
func TestGetInvalidPort(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/invalid-port"),
}
val, err := us.GetStrict("test.test", "Port")
if err == nil {
t.Fatalf("expected non-nil err, got nil")
}
if val != "" {
t.Errorf("expected to get '' for val, got %q", val)
}
if err.Error() != `ssh_config: strconv.ParseUint: parsing "notanumber": invalid syntax` {
t.Errorf("wrong error: got %v", err)
}
}
func TestGetNotFoundNoDefault(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/config1"),
}
val, err := us.GetStrict("wap", "CanonicalDomains")
if err != nil {
t.Fatalf("expected nil err, got %v", err)
}
if val != "" {
t.Errorf("expected to get CanonicalDomains '', got %q", val)
}
}
func TestGetWildcard(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/config3"),
+2
View File
@@ -0,0 +1,2 @@
Host test.test
Port notanumber