seaweedfs-operator/controllers/swadmin/seaweed_admin.go

68 lines
1.4 KiB
Go
Raw Normal View History

package swadmin
import (
"fmt"
"io"
"regexp"
"strings"
"github.com/chrislusf/seaweedfs/weed/shell"
"google.golang.org/grpc"
)
type SeaweedAdmin struct {
commandReg *regexp.Regexp
commandEnv *shell.CommandEnv
Output io.Writer
}
func NewSeaweedAdmin(masters string, output io.Writer) *SeaweedAdmin {
var shellOptions shell.ShellOptions
shellOptions.GrpcDialOption = grpc.WithInsecure()
shellOptions.Masters = &masters
commandEnv := shell.NewCommandEnv(shellOptions)
reg, _ := regexp.Compile(`'.*?'|".*?"|\S+`)
go commandEnv.MasterClient.KeepConnectedToMaster()
return &SeaweedAdmin{
commandEnv: commandEnv,
commandReg: reg,
Output: output,
}
}
// ProcessCommands cmds can be semi-colon separated commands
func (sa *SeaweedAdmin) ProcessCommands(cmds string) error {
for _, c := range strings.Split(cmds, ";") {
if err := sa.ProcessCommand(c); err != nil {
return err
}
}
return nil
}
func (sa *SeaweedAdmin) ProcessCommand(cmd string) error {
2020-11-09 00:23:17 +00:00
sa.commandEnv.MasterClient.WaitUntilConnected()
cmds := sa.commandReg.FindAllString(cmd, -1)
if len(cmds) == 0 {
return nil
}
args := make([]string, len(cmds[1:]))
for i := range args {
args[i] = strings.Trim(string(cmds[1+i]), "\"'")
}
for _, c := range shell.Commands {
2020-11-09 07:59:02 +00:00
if c.Name() == cmds[0] || c.Name() == "fs."+cmds[0] {
return c.Do(args, sa.commandEnv, sa.Output)
}
}
return fmt.Errorf("unknown command: %v", cmd)
}