update README and add more examples

This commit is contained in:
Kevin Burke
2017-05-25 22:05:04 -07:00
parent 873a8ad95f
commit 6dcd4cf595
3 changed files with 49 additions and 14 deletions
+26 -6
View File
@@ -17,11 +17,27 @@ want to retrieve.
port := ssh_config.Get("myhost", "Port")
```
Some SSH arguments have default values - for example, the default value for
`KeyboardAuthentication` is `"yes"`. If we can't find a value for the given
Host/keyword pair, and a default exists for the keyword, we return it.
You can also load a config file and read values from it.
Here's how you can manipulate an SSH config file, and then write it back.
```go
var config = `
Host test.test
Compression yes
`
cfg, err := ssh_config.Decode(strings.NewReader(config))
fmt.Println(cfg.Get("Port"))
```
Some SSH arguments have default values - for example, the default value for
`KeyboardAuthentication` is `"yes"`. If you call Get(), and no value for the
given Host/keyword pair exists in the config, we'll return a default for the
keyword if one exists.
### Manipulating SSH config files
Here's how you can manipulate an SSH config file, and then write it back to
disk.
```go
f, _ := os.Open(filepath.Join(os.Getenv("HOME"), ".ssh", "config"))
@@ -29,11 +45,13 @@ cfg, _ := ssh_config.Decode(f)
for _, host := range cfg.Hosts {
fmt.Println("patterns:", host.Patterns)
for _, node := range host.Nodes {
// Manipulate the nodes as you see fit, or use a type switch to
// distinguish between Empty, KV, and Include nodes.
fmt.Println(node.String())
}
}
// Write the cfg back to disk:
// Print the config to stdout:
fmt.Println(cfg.String())
```
@@ -41,7 +59,9 @@ fmt.Println(cfg.String())
Wherever possible we try to implement the specification as documented in
the `ssh_config` manpage. Unimplemented features should be present in the
[issues][issues] list. Notably, the `Match` directive is currently unsupported.
[issues][issues] list.
Notably, the `Match` directive is currently unsupported.
[issues]: https://github.com/kevinburke/ssh_config/issues
-2
View File
@@ -242,8 +242,6 @@ type Config struct {
// Config contains an invalid conditional Include value.
//
// The match for key is case insensitive.
//
// Get is a wrapper around DefaultUserSettings.Get.
func (c *Config) Get(alias, key string) (string, error) {
lowerKey := strings.ToLower(key)
for _, host := range c.Hosts {
+23 -6
View File
@@ -1,10 +1,15 @@
package ssh_config
package ssh_config_test
import "fmt"
import (
"fmt"
"strings"
"github.com/kevinburke/ssh_config"
)
func ExampleHost_Matches() {
pat, _ := NewPattern("test.*.example.com")
host := &Host{Patterns: []*Pattern{pat}}
pat, _ := ssh_config.NewPattern("test.*.example.com")
host := &ssh_config.Host{Patterns: []*ssh_config.Pattern{pat}}
fmt.Println(host.Matches("test.stage.example.com"))
fmt.Println(host.Matches("othersubdomain.example.com"))
// Output:
@@ -13,11 +18,23 @@ func ExampleHost_Matches() {
}
func ExamplePattern() {
pat, _ := NewPattern("*")
host := &Host{Patterns: []*Pattern{pat}}
pat, _ := ssh_config.NewPattern("*")
host := &ssh_config.Host{Patterns: []*ssh_config.Pattern{pat}}
fmt.Println(host.Matches("test.stage.example.com"))
fmt.Println(host.Matches("othersubdomain.any.any"))
// Output:
// true
// true
}
func ExampleDecode() {
var config = `
Host *.example.com
Compression yes
`
cfg, _ := ssh_config.Decode(strings.NewReader(config))
val, _ := cfg.Get("test.example.com", "Compression")
fmt.Println(val)
// Output: yes
}