| 1 | package ssh_config_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/dimonomid/ssh_config" |
| 9 | ) |
| 10 | |
| 11 | func ExampleHost_Matches() { |
| 12 | pat, _ := ssh_config.NewPattern("test.*.example.com") |
| 13 | host := &ssh_config.Host{Patterns: []*ssh_config.Pattern{pat}} |
| 14 | fmt.Println(host.Matches("test.stage.example.com")) |
| 15 | fmt.Println(host.Matches("othersubdomain.example.com")) |
| 16 | // Output: |
| 17 | // true |
| 18 | // false |
| 19 | } |
| 20 | |
| 21 | func ExamplePattern() { |
| 22 | pat, _ := ssh_config.NewPattern("*") |
| 23 | host := &ssh_config.Host{Patterns: []*ssh_config.Pattern{pat}} |
| 24 | fmt.Println(host.Matches("test.stage.example.com")) |
| 25 | fmt.Println(host.Matches("othersubdomain.any.any")) |
| 26 | // Output: |
| 27 | // true |
| 28 | // true |
| 29 | } |
| 30 | |
| 31 | func ExampleDecode() { |
| 32 | var config = ` |
| 33 | Host *.example.com |
| 34 | Compression yes |
| 35 | ` |
| 36 | |
| 37 | cfg, _ := ssh_config.Decode(strings.NewReader(config), false) |
| 38 | val, _ := cfg.Get("test.example.com", "Compression") |
| 39 | fmt.Println(val) |
| 40 | // Output: yes |
| 41 | } |
| 42 | |
| 43 | func ExampleDefault() { |
| 44 | fmt.Println(ssh_config.Default("Port")) |
| 45 | fmt.Println(ssh_config.Default("UnknownVar")) |
| 46 | // Output: |
| 47 | // 22 |
| 48 | // |
| 49 | } |
| 50 | |
| 51 | func ExampleUserSettings_ConfigFinder() { |
| 52 | // This can be used to test SSH config parsing. |
| 53 | u := ssh_config.UserSettings{} |
| 54 | u.ConfigFinder(func() string { |
| 55 | return filepath.Join("testdata", "test_config") |
| 56 | }, |
| 57 | ) |
| 58 | u.Get("example.com", "Host") |
| 59 | } |