Compare commits

...

4 Commits

6 changed files with 42 additions and 26 deletions

@ -11,7 +11,7 @@ import (
)
func main() {
eth0 := &iface.NetworkInterface{}
ifaces := iface.NewMultiParser()
switch {
case len(os.Args) < 2:
buf := &bytes.Buffer{}
@ -31,7 +31,7 @@ func main() {
break
}
}
n, err := eth0.Write(buf.Bytes())
n, err := ifaces.Write(buf.Bytes())
if err != nil {
panic(err)
}
@ -43,7 +43,7 @@ func main() {
if err != nil {
panic(err)
}
n, err := eth0.Write(dat)
n, err := ifaces.Write(dat)
if err != nil {
panic(err)
}
@ -51,12 +51,22 @@ func main() {
panic("short write")
}
}
dat, err := json.MarshalIndent(eth0, "", "\t")
imap, err := ifaces.Parse()
if err != nil {
panic(err)
}
if err = eth0.Validate(); err != nil {
println(err.Error())
return
}
for _, netif := range imap {
if netif == nil {
continue
}
if err = netif.Validate(); err != nil {
println(netif.Name + " skip due to error: " + err.Error())
delete(imap, netif.Name)
continue
}
}
dat, err := json.MarshalIndent(imap, "", "\t")
_, _ = os.Stdout.Write(dat)
}

Binary file not shown.

3
go.mod

@ -5,7 +5,4 @@ go 1.21.4
require (
git.tcp.direct/kayos/common v0.9.6
github.com/davecgh/go-spew v1.1.1
github.com/hashicorp/go-multierror v1.1.1
)
require github.com/hashicorp/errwrap v1.0.0 // indirect

4
go.sum

@ -2,7 +2,3 @@ git.tcp.direct/kayos/common v0.9.6 h1:EITtktxZF/zkzqAhZZxvm6cZpFYoZ0P/gLB9RPatKU
git.tcp.direct/kayos/common v0.9.6/go.mod h1:8y9b+PN1+ZVaQ/VugD9dkKe+uqhE8jH7a64RyF7h2rM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=

@ -2,13 +2,15 @@ package iface
import (
"bufio"
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
"sync"
"git.tcp.direct/kayos/common/pool"
)
type Interfaces map[string]*NetworkInterface
type poolGroup struct {
Buffers pool.BufferFactory
Strs pool.StringFactory
@ -20,22 +22,28 @@ type MultiParser struct {
Interfaces map[string]*NetworkInterface
Errs []error
buf []byte
mu *sync.Mutex
}
func NewMultiParser() *MultiParser {
return &MultiParser{
Interfaces: make(map[string]*NetworkInterface),
Interfaces: make(Interfaces),
Errs: make([]error, 0),
buf: make([]byte, 0),
mu: &sync.Mutex{},
}
}
func (p *MultiParser) Write(data []byte) (int, error) {
p.mu.Lock()
p.buf = append(p.buf, data...)
p.mu.Unlock()
return len(data), nil
}
func (p *MultiParser) Parse() error {
func (p *MultiParser) Parse() (Interfaces, error) {
p.mu.Lock()
defer p.mu.Unlock()
scanner := bufio.NewScanner(strings.NewReader(string(p.buf)))
index := 0
@ -92,11 +100,16 @@ func (p *MultiParser) Parse() error {
if len(buf.Bytes()) > 0 {
flush("unknown")
}
me := &multierror.Error{}
var multiErr error
for _, err := range p.Errs {
if err != nil {
me = multierror.Append(me, err)
switch {
case err == nil:
continue
case multiErr == nil:
multiErr = err
default:
multiErr = fmt.Errorf("%w, %w", multiErr, err)
}
}
return me.ErrorOrNil()
return p.Interfaces, multiErr
}

@ -14,13 +14,13 @@ import (
type Hooks struct {
// PreUp of the interface
PreUp []string
PreUp []string `json:"pre_up,omitempty"`
// PostUp of the interface
PostUp []string
PostUp []string `json:"post_up,omitempty"`
// PreDown of the interface
PreDown []string
PreDown []string `json:"pre_down,omitempty"`
// PostDown of the interface
PostDown []string
PostDown []string `json:"post_down,omitempty"`
}
type AddressConfig uint8