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