2016-06-03 09:09:14 +08:00
|
|
|
package command
|
2012-08-24 11:56:09 +08:00
|
|
|
|
|
|
|
import (
|
2013-01-17 16:56:56 +08:00
|
|
|
"bufio"
|
2013-09-02 14:58:21 +08:00
|
|
|
"fmt"
|
2013-01-17 16:56:56 +08:00
|
|
|
"os"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2016-06-03 09:09:14 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2012-08-24 11:56:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2013-01-17 16:56:56 +08:00
|
|
|
cmdShell.Run = runShell // break init cycle
|
2012-08-24 11:56:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmdShell = &Command{
|
2013-01-17 16:56:56 +08:00
|
|
|
UsageLine: "shell",
|
|
|
|
Short: "run interactive commands, now just echo",
|
|
|
|
Long: `run interactive commands.
|
2012-08-24 11:56:09 +08:00
|
|
|
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2013-01-17 16:56:56 +08:00
|
|
|
var ()
|
2012-08-24 11:56:09 +08:00
|
|
|
|
|
|
|
func runShell(command *Command, args []string) bool {
|
2013-01-17 16:56:56 +08:00
|
|
|
r := bufio.NewReader(os.Stdin)
|
|
|
|
o := bufio.NewWriter(os.Stdout)
|
|
|
|
e := bufio.NewWriter(os.Stderr)
|
|
|
|
prompt := func() {
|
2013-02-27 14:54:22 +08:00
|
|
|
var err error
|
|
|
|
if _, err = o.WriteString("> "); err != nil {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("error writing to stdout:", err)
|
2013-02-27 14:54:22 +08:00
|
|
|
}
|
|
|
|
if err = o.Flush(); err != nil {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("error flushing stdout:", err)
|
2013-02-27 14:54:22 +08:00
|
|
|
}
|
2013-01-17 16:56:56 +08:00
|
|
|
}
|
|
|
|
readLine := func() string {
|
|
|
|
ret, err := r.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprint(e, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
execCmd := func(cmd string) int {
|
|
|
|
if cmd != "" {
|
2013-02-27 14:54:22 +08:00
|
|
|
if _, err := o.WriteString(cmd); err != nil {
|
2013-08-09 14:57:22 +08:00
|
|
|
glog.V(0).Infoln("error writing to stdout:", err)
|
2013-02-27 14:54:22 +08:00
|
|
|
}
|
2013-01-17 16:56:56 +08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
2012-08-24 11:56:09 +08:00
|
|
|
|
2013-01-17 16:56:56 +08:00
|
|
|
cmd := ""
|
|
|
|
for {
|
|
|
|
prompt()
|
|
|
|
cmd = readLine()
|
|
|
|
execCmd(cmd)
|
|
|
|
}
|
2012-08-24 11:56:09 +08:00
|
|
|
}
|