From 6dcd4cf59573552963e7c3d51104d26650a0a940 Mon Sep 17 00:00:00 2001 From: Kevin Burke Date: Thu, 25 May 2017 22:05:04 -0700 Subject: [PATCH] update README and add more examples --- README.md | 32 ++++++++++++++++++++++++++------ config.go | 2 -- example_test.go | 29 +++++++++++++++++++++++------ 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 9b6b6c0..f9501b2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config.go b/config.go index 97c319d..8441fd3 100644 --- a/config.go +++ b/config.go @@ -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 { diff --git a/example_test.go b/example_test.go index 5cceaee..8e27067 100644 --- a/example_test.go +++ b/example_test.go @@ -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 +}