[#521] Add netmap load command to policy playground #522
1 changed files with 39 additions and 1 deletions
|
@ -3,6 +3,7 @@ package container
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -39,7 +40,7 @@ func (repl *policyPlaygroundREPL) handleLs(args []string) error {
|
||||||
for id, node := range repl.nodes {
|
for id, node := range repl.nodes {
|
||||||
var attrs []string
|
var attrs []string
|
||||||
node.IterateAttributes(func(k, v string) {
|
node.IterateAttributes(func(k, v string) {
|
||||||
attrs = append(attrs, fmt.Sprintf("%s:%s", k, v))
|
attrs = append(attrs, fmt.Sprintf("%s:%q", k, v))
|
||||||
})
|
})
|
||||||
fmt.Printf("\t%2d: id=%s attrs={%v}\n", i, id, strings.Join(attrs, " "))
|
fmt.Printf("\t%2d: id=%s attrs={%v}\n", i, id, strings.Join(attrs, " "))
|
||||||
i++
|
i++
|
||||||
|
@ -69,6 +70,40 @@ func (repl *policyPlaygroundREPL) handleAdd(args []string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (repl *policyPlaygroundREPL) handleLoad(args []string) error {
|
||||||
|
if len(args) != 1 {
|
||||||
|
return fmt.Errorf("too few arguments for command 'add': got %d, want 1", len(args))
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonNetmap := map[string]map[string]string{}
|
||||||
|
|
||||||
|
b, err := os.ReadFile(args[0])
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("reading netmap file %q: %v", args[0], err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(b, &jsonNetmap); err != nil {
|
||||||
|
return fmt.Errorf("decoding json netmap: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
repl.nodes = make(map[string]netmap.NodeInfo)
|
||||||
|
for id, attrs := range jsonNetmap {
|
||||||
|
key, err := hex.DecodeString(id)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("node id must be a hex string: got %q: %v", id, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
node := repl.nodes[id]
|
||||||
|
node.SetPublicKey(key)
|
||||||
|
for k, v := range attrs {
|
||||||
|
node.SetAttribute(k, v)
|
||||||
|
}
|
||||||
|
repl.nodes[id] = node
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (repl *policyPlaygroundREPL) handleRemove(args []string) error {
|
func (repl *policyPlaygroundREPL) handleRemove(args []string) error {
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return fmt.Errorf("too few arguments for command 'remove': got %d, want >0", len(args))
|
return fmt.Errorf("too few arguments for command 'remove': got %d, want >0", len(args))
|
||||||
|
@ -131,9 +166,12 @@ func (repl *policyPlaygroundREPL) run() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdHandlers := map[string]func([]string) error{
|
cmdHandlers := map[string]func([]string) error{
|
||||||
|
"list": repl.handleLs,
|
||||||
"ls": repl.handleLs,
|
"ls": repl.handleLs,
|
||||||
"add": repl.handleAdd,
|
"add": repl.handleAdd,
|
||||||
|
"load": repl.handleLoad,
|
||||||
"remove": repl.handleRemove,
|
"remove": repl.handleRemove,
|
||||||
|
"rm": repl.handleRemove,
|
||||||
"eval": repl.handleEval,
|
"eval": repl.handleEval,
|
||||||
}
|
}
|
||||||
for reader := bufio.NewReader(os.Stdin); ; {
|
for reader := bufio.NewReader(os.Stdin); ; {
|
||||||
|
|
Loading…
Reference in a new issue