From 662b5d0cf7caade0a2b6acd3442642036b910c72 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 23 Apr 2020 14:01:46 -0700 Subject: [PATCH] support semicolon seperated command lines --- weed/server/master_server.go | 41 +++++++++++---------- weed/shell/shell_liner.go | 69 ++++++++++++++++++++---------------- 2 files changed, 62 insertions(+), 48 deletions(-) diff --git a/weed/server/master_server.go b/weed/server/master_server.go index ba17748bd..82b306fc4 100644 --- a/weed/server/master_server.go +++ b/weed/server/master_server.go @@ -236,24 +236,8 @@ func (ms *MasterServer) startAdminScripts() { for range c { if ms.Topo.IsLeader() { for _, line := range scriptLines { - - cmds := reg.FindAllString(line, -1) - if len(cmds) == 0 { - continue - } - args := make([]string, len(cmds[1:])) - for i := range args { - args[i] = strings.Trim(string(cmds[1+i]), "\"'") - } - cmd := strings.ToLower(cmds[0]) - - for _, c := range shell.Commands { - if c.Name() == cmd { - glog.V(0).Infof("executing: %s %v", cmd, args) - if err := c.Do(args, commandEnv, os.Stdout); err != nil { - glog.V(0).Infof("error: %v", err) - } - } + for _, c := range strings.Split(line, ";") { + processEachCmd(reg, c, commandEnv) } } } @@ -261,6 +245,27 @@ func (ms *MasterServer) startAdminScripts() { }() } +func processEachCmd(reg *regexp.Regexp, line string, commandEnv *shell.CommandEnv) { + cmds := reg.FindAllString(line, -1) + if len(cmds) == 0 { + return + } + args := make([]string, len(cmds[1:])) + for i := range args { + args[i] = strings.Trim(string(cmds[1+i]), "\"'") + } + cmd := strings.ToLower(cmds[0]) + + for _, c := range shell.Commands { + if c.Name() == cmd { + glog.V(0).Infof("executing: %s %v", cmd, args) + if err := c.Do(args, commandEnv, os.Stdout); err != nil { + glog.V(0).Infof("error: %v", err) + } + } + } +} + func (ms *MasterServer) createSequencer(option *MasterOption) sequence.Sequencer { var seq sequence.Sequencer v := util.GetViper() diff --git a/weed/shell/shell_liner.go b/weed/shell/shell_liner.go index 0ad46254e..4632a1fb0 100644 --- a/weed/shell/shell_liner.go +++ b/weed/shell/shell_liner.go @@ -45,42 +45,51 @@ func RunShell(options ShellOptions) { return } - cmds := reg.FindAllString(cmd, -1) - if len(cmds) == 0 { - continue - } else { - line.AppendHistory(cmd) - - args := make([]string, len(cmds[1:])) - - for i := range args { - args[i] = strings.Trim(string(cmds[1+i]), "\"'") - } - - cmd := strings.ToLower(cmds[0]) - if cmd == "help" || cmd == "?" { - printHelp(cmds) - } else if cmd == "exit" || cmd == "quit" { + for _, c := range strings.Split(cmd, ";") { + if processEachCmd(reg, c, commandEnv) { return - } 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 - } - } - if !foundCommand { - fmt.Fprintf(os.Stderr, "unknown command: %v\n", cmd) - } } - } } } +func processEachCmd(reg *regexp.Regexp, cmd string, commandEnv *CommandEnv) bool { + cmds := reg.FindAllString(cmd, -1) + if len(cmds) == 0 { + return false + } else { + line.AppendHistory(cmd) + + args := make([]string, len(cmds[1:])) + + for i := range args { + args[i] = strings.Trim(string(cmds[1+i]), "\"'") + } + + cmd := strings.ToLower(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 + } + } + if !foundCommand { + fmt.Fprintf(os.Stderr, "unknown command: %v\n", cmd) + } + } + + } + return false +} + func printGenericHelp() { msg := `Type: "help " for help on