seaweedfs/weed/shell/shell_liner.go

207 lines
4.4 KiB
Go
Raw Normal View History

package shell
import (
2021-08-30 13:19:46 +08:00
"context"
"fmt"
2021-11-08 16:09:11 +08:00
"github.com/chrislusf/seaweedfs/weed/cluster"
"github.com/chrislusf/seaweedfs/weed/pb"
2021-08-30 13:19:46 +08:00
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
"github.com/chrislusf/seaweedfs/weed/util/grace"
"golang.org/x/exp/slices"
2019-03-18 08:28:29 +08:00
"io"
"math/rand"
"os"
2019-03-24 03:57:35 +08:00
"path"
"regexp"
2020-03-24 17:40:51 +08:00
"strings"
"github.com/peterh/liner"
)
var (
line *liner.State
2019-03-24 03:57:35 +08:00
historyPath = path.Join(os.TempDir(), "weed-shell")
)
func RunShell(options ShellOptions) {
slices.SortFunc(Commands, func(a, b command) bool {
return strings.Compare(a.Name(), b.Name()) < 0
2021-03-20 21:33:45 +08:00
})
line = liner.NewLiner()
defer line.Close()
grace.OnInterrupt(func() {
line.Close()
})
line.SetCtrlCAborts(true)
line.SetTabCompletionStyle(liner.TabPrints)
setCompletionHandler()
2019-03-24 03:57:35 +08:00
loadHistory()
2019-03-24 03:57:35 +08:00
defer saveHistory()
reg, _ := regexp.Compile(`'.*?'|".*?"|\S+`)
2022-01-12 17:24:24 +08:00
commandEnv := NewCommandEnv(&options)
go commandEnv.MasterClient.KeepConnectedToMaster()
commandEnv.MasterClient.WaitUntilConnected()
if commandEnv.option.FilerAddress == "" {
var filers []pb.ServerAddress
commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
resp, err := client.ListClusterNodes(context.Background(), &master_pb.ListClusterNodesRequest{
2021-11-08 16:09:11 +08:00
ClientType: cluster.FilerType,
2022-05-02 12:59:16 +08:00
FilerGroup: *options.FilerGroup,
})
if err != nil {
return err
}
for _, clusterNode := range resp.ClusterNodes {
filers = append(filers, pb.ServerAddress(clusterNode.Address))
}
return nil
})
2021-11-03 14:45:47 +08:00
fmt.Printf("master: %s ", *options.Masters)
if len(filers) > 0 {
2021-11-03 14:45:47 +08:00
fmt.Printf("filers: %v", filers)
commandEnv.option.FilerAddress = filers[rand.Intn(len(filers))]
}
2021-11-03 14:45:47 +08:00
fmt.Println()
}
2021-08-30 13:19:46 +08:00
if commandEnv.option.FilerAddress != "" {
commandEnv.WithFilerClient(false, func(filerClient filer_pb.SeaweedFilerClient) error {
2021-08-30 13:19:46 +08:00
resp, err := filerClient.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
if err != nil {
return err
}
if resp.ClusterId != "" {
fmt.Printf(`
---
Free Monitoring Data URL:
https://cloud.seaweedfs.com/ui/%s
---
`, resp.ClusterId)
}
return nil
})
}
for {
cmd, err := line.Prompt("> ")
if err != nil {
2019-03-18 08:28:29 +08:00
if err != io.EOF {
fmt.Printf("%v\n", err)
}
return
}
for _, c := range strings.Split(cmd, ";") {
if processEachCmd(reg, c, commandEnv) {
return
}
}
}
}
func processEachCmd(reg *regexp.Regexp, cmd string, commandEnv *CommandEnv) bool {
cmds := reg.FindAllString(cmd, -1)
2021-09-09 06:55:19 +08:00
line.AppendHistory(cmd)
if len(cmds) == 0 {
return false
} else {
args := make([]string, len(cmds[1:]))
for i := range args {
args[i] = strings.Trim(string(cmds[1+i]), "\"'")
}
2020-09-14 12:25:51 +08:00
cmd := cmds[0]
if cmd == "help" || cmd == "?" {
printHelp(cmds)
} else if cmd == "exit" || cmd == "quit" {
return true
} else {
foundCommand := false
for _, c := range Commands {
if c.Name() == cmd || c.Name() == "fs."+cmd {
if err := c.Do(args, commandEnv, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
}
foundCommand = true
2019-04-24 13:29:36 +08:00
}
}
if !foundCommand {
fmt.Fprintf(os.Stderr, "unknown command: %v\n", cmd)
}
}
}
return false
}
func printGenericHelp() {
msg :=
`Type: "help <command>" for help on <command>. Most commands support "<command> -h" also for options.
`
fmt.Print(msg)
for _, c := range Commands {
2019-03-24 02:54:26 +08:00
helpTexts := strings.SplitN(c.Help(), "\n", 2)
fmt.Printf(" %-30s\t# %s \n", c.Name(), helpTexts[0])
}
}
func printHelp(cmds []string) {
args := cmds[1:]
if len(args) == 0 {
printGenericHelp()
} else if len(args) > 1 {
fmt.Println()
} else {
cmd := strings.ToLower(args[0])
for _, c := range Commands {
if c.Name() == cmd {
2019-03-24 02:54:26 +08:00
fmt.Printf(" %s\t# %s\n", c.Name(), c.Help())
}
}
}
}
func setCompletionHandler() {
line.SetCompleter(func(line string) (c []string) {
for _, i := range Commands {
if strings.HasPrefix(i.Name(), strings.ToLower(line)) {
c = append(c, i.Name())
}
}
return
})
}
2019-03-24 03:57:35 +08:00
func loadHistory() {
if f, err := os.Open(historyPath); err == nil {
line.ReadHistory(f)
f.Close()
}
}
2019-03-24 03:57:35 +08:00
func saveHistory() {
if f, err := os.Create(historyPath); err != nil {
2021-06-08 17:01:23 +08:00
fmt.Printf("Error creating history file: %v\n", err)
} else {
2021-06-08 17:01:23 +08:00
if _, err = line.WriteHistory(f); err != nil {
fmt.Printf("Error writing history file: %v\n", err)
}
f.Close()
}
}