Compare commits
2 commits
8a57c78f5f
...
80f8a8fd3a
Author | SHA1 | Date | |
---|---|---|---|
80f8a8fd3a | |||
2f3bc6eb84 |
1 changed files with 36 additions and 10 deletions
|
@ -1,11 +1,10 @@
|
||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -14,6 +13,7 @@ import (
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
||||||
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
||||||
|
"github.com/chzyer/readline"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
@ -163,6 +163,16 @@ func (repl *policyPlaygroundREPL) netMap() netmap.NetMap {
|
||||||
return nm
|
return nm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var policyPlaygroundCompleter = readline.NewPrefixCompleter(
|
||||||
|
readline.PcItem("list"),
|
||||||
|
readline.PcItem("ls"),
|
||||||
|
readline.PcItem("add"),
|
||||||
|
readline.PcItem("load"),
|
||||||
|
readline.PcItem("remove"),
|
||||||
|
readline.PcItem("rm"),
|
||||||
|
readline.PcItem("eval"),
|
||||||
|
)
|
||||||
|
|
||||||
func (repl *policyPlaygroundREPL) run() error {
|
func (repl *policyPlaygroundREPL) run() error {
|
||||||
if len(viper.GetString(commonflags.RPC)) > 0 {
|
if len(viper.GetString(commonflags.RPC)) > 0 {
|
||||||
key := key.GetOrGenerate(repl.cmd)
|
key := key.GetOrGenerate(repl.cmd)
|
||||||
|
@ -189,22 +199,38 @@ func (repl *policyPlaygroundREPL) run() error {
|
||||||
"rm": repl.handleRemove,
|
"rm": repl.handleRemove,
|
||||||
"eval": repl.handleEval,
|
"eval": repl.handleEval,
|
||||||
}
|
}
|
||||||
for reader := bufio.NewReader(os.Stdin); ; {
|
|
||||||
fmt.Print("> ")
|
rl, err := readline.NewEx(&readline.Config{
|
||||||
line, err := reader.ReadString('\n')
|
Prompt: "> ",
|
||||||
|
InterruptPrompt: "^C",
|
||||||
|
AutoComplete: policyPlaygroundCompleter,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error initializing readline: %w", err)
|
||||||
|
}
|
||||||
|
defer rl.Close()
|
||||||
|
|
||||||
|
var exit bool
|
||||||
|
for {
|
||||||
|
line, err := rl.Readline()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == io.EOF {
|
if errors.Is(err, readline.ErrInterrupt) {
|
||||||
return nil
|
if exit {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
exit = true
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
return fmt.Errorf("reading line: %v", err)
|
return fmt.Errorf("reading line: %w", err)
|
||||||
}
|
}
|
||||||
|
exit = false
|
||||||
|
|
||||||
parts := strings.Fields(line)
|
parts := strings.Fields(line)
|
||||||
if len(parts) == 0 {
|
if len(parts) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
cmd := parts[0]
|
cmd := parts[0]
|
||||||
handler, exists := cmdHandlers[cmd]
|
if handler, exists := cmdHandlers[cmd]; exists {
|
||||||
if exists {
|
|
||||||
if err := handler(parts[1:]); err != nil {
|
if err := handler(parts[1:]); err != nil {
|
||||||
fmt.Printf("error: %v\n", err)
|
fmt.Printf("error: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue