diff options
| author | Loyalsoldier <[email protected]> | 2023-10-29 15:37:22 +0800 |
|---|---|---|
| committer | Loyalsoldier <[email protected]> | 2023-10-29 15:37:22 +0800 |
| commit | 82bc6a9f3ccf028ff4c01849b4aee33a0c0bd94d (patch) | |
| tree | 9fe3f104f4f232bb4fb81c92dabeb9e4dc087430 /lib | |
| parent | cbbee50da7d45aad95c2700bc7148e0636170c62 (diff) | |
Feat: skip comment within a line in plaintext formats automatically
content starts with `#` `//` `/*` within a line will be treated as comment and be ignored
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/error.go | 1 | ||||
| -rw-r--r-- | lib/lib.go | 12 |
2 files changed, 11 insertions, 2 deletions
diff --git a/lib/error.go b/lib/error.go index c990d367..1b8e4518 100644 --- a/lib/error.go +++ b/lib/error.go @@ -11,4 +11,5 @@ var ( ErrInvalidIPLength = errors.New("invalid IP address length") ErrInvalidIPNet = errors.New("invalid IPNet address") ErrInvalidPrefixType = errors.New("invalid prefix type") + ErrCommentLine = errors.New("comment line") ) @@ -164,6 +164,14 @@ func (e *Entry) processPrefix(src any) (*netip.Prefix, IPType, error) { } case string: + src, _, _ = strings.Cut(src, "#") + src, _, _ = strings.Cut(src, "//") + src, _, _ = strings.Cut(src, "/*") + src = strings.TrimSpace(src) + if src == "" { + return nil, "", ErrCommentLine + } + _, network, err := net.ParseCIDR(src) switch err { case nil: @@ -256,7 +264,7 @@ func (e *Entry) remove(prefix *netip.Prefix, ipType IPType) error { func (e *Entry) AddPrefix(cidr any) error { prefix, ipType, err := e.processPrefix(cidr) - if err != nil { + if err != nil && err != ErrCommentLine { return err } if err := e.add(prefix, ipType); err != nil { @@ -267,7 +275,7 @@ func (e *Entry) AddPrefix(cidr any) error { func (e *Entry) RemovePrefix(cidr string) error { prefix, ipType, err := e.processPrefix(cidr) - if err != nil { + if err != nil && err != ErrCommentLine { return err } if err := e.remove(prefix, ipType); err != nil { |
