diff options
| author | Shulhan <ms@kilabit.info> | 2023-12-18 23:48:00 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-12-18 23:48:00 +0700 |
| commit | f57c33ecfec8a3b67f6fe3950002d068dc5bdd68 (patch) | |
| tree | e2f4a73eec32fd2524b7d6d21247dbd00c66bfce /lib/ssh/config/pattern.go | |
| parent | a9a2b05381892ea1397ff1e489fe6f6b35bd86a4 (diff) | |
| download | pakakeh.go-f57c33ecfec8a3b67f6fe3950002d068dc5bdd68.tar.xz | |
ssh/config: add method MarshalText and WriteTo
The MarshalText method encode the Section back to ssh_config format
with two spaces as indentation in key.
The WriteTo method marshal the Section into text and write it to
[io.Writer] w.
Diffstat (limited to 'lib/ssh/config/pattern.go')
| -rw-r--r-- | lib/ssh/config/pattern.go | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/lib/ssh/config/pattern.go b/lib/ssh/config/pattern.go index d413fc9a..39199fd8 100644 --- a/lib/ssh/config/pattern.go +++ b/lib/ssh/config/pattern.go @@ -4,7 +4,11 @@ package config -import "path/filepath" +import ( + "bytes" + "io" + "path/filepath" +) type pattern struct { value string @@ -22,6 +26,28 @@ func newPattern(s string) (pat *pattern) { return pat } +// MarshalText encode the pattern back to ssh_config format. +func (pat *pattern) MarshalText() (text []byte, err error) { + var buf bytes.Buffer + + if pat.isNegate { + buf.WriteByte('!') + } + buf.WriteString(pat.value) + + return buf.Bytes(), nil +} + +// WriteTo marshal the pattern into text and write it to w. +func (pat *pattern) WriteTo(w io.Writer) (n int64, err error) { + var text []byte + text, _ = pat.MarshalText() + + var c int + c, err = w.Write(text) + return int64(c), err +} + // isMatch will return true if input string match with regex and isNegate is // false; otherwise it will return false. func (pat *pattern) isMatch(s string) bool { |
