1
0
Fork 0
forked from External/grumble

Sorted ssh help.

This commit is contained in:
Mikkel Krautz 2011-11-10 14:11:49 +01:00
parent ccbc282f1e
commit 69abacbb19

71
ssh.go
View file

@ -27,23 +27,25 @@ type SshCmdReply interface {
type SshCmdFunc func(reply SshCmdReply, args []string) error type SshCmdFunc func(reply SshCmdReply, args []string) error
type SshCmd struct { type SshCmd struct {
CmdFunc SshCmdFunc Name string
Args string CmdFunc SshCmdFunc
Description string Args string
Description string
} }
func (c SshCmd) Call(reply SshCmdReply, args []string) error { func (c SshCmd) Call(reply SshCmdReply, args []string) error {
return c.CmdFunc(reply, args) return c.CmdFunc(reply, args)
} }
var cmdMap = map[string]SshCmd{} var commands = []SshCmd{}
func RegisterSSHCmd(name string, cmdFunc SshCmdFunc, args string, desc string) { func RegisterSSHCmd(name string, cmdFunc SshCmdFunc, args string, desc string) {
cmdMap[name] = SshCmd{ commands = append(commands, SshCmd{
CmdFunc: cmdFunc, Name: name,
Args: args, CmdFunc: cmdFunc,
Args: args,
Description: desc, Description: desc,
} })
} }
func RunSSH() { func RunSSH() {
@ -141,11 +143,18 @@ func handleChannel(channel ssh.Channel) {
continue continue
} }
if args[0] == "exit" { if args[0] == "exit" || args[0] == "quit" {
return return
} }
if cmd, ok := cmdMap[args[0]]; ok { var cmd *SshCmd
for i := range commands {
if commands[i].Name == args[0] {
cmd = &commands[i]
break
}
}
if cmd != nil {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err = cmd.Call(buf, args) err = cmd.Call(buf, args)
if err != nil { if err != nil {
@ -156,9 +165,17 @@ func handleChannel(channel ssh.Channel) {
continue continue
} }
_, err = shell.Write(buf.Bytes()) bufBytes := buf.Bytes()
if err != nil { chunkSize := int(64)
return for len(bufBytes) > 0 {
if len(bufBytes) < chunkSize {
chunkSize = len(bufBytes)
}
nwritten, err := shell.Write(bufBytes[0:chunkSize])
if err != nil {
return
}
bufBytes = bufBytes[nwritten:]
} }
} else { } else {
_, err = shell.Write([]byte("error: unknown command\r\n")) _, err = shell.Write([]byte("error: unknown command\r\n"))
@ -172,23 +189,25 @@ func handleChannel(channel ssh.Channel) {
} }
func HelpCmd(reply SshCmdReply, args []string) error { func HelpCmd(reply SshCmdReply, args []string) error {
if len(args) == 1 { onlyShow := ""
for cmdName, cmd := range cmdMap { didShow := false
if len(args) > 1 {
onlyShow = args[1]
}
for _, cmd := range commands {
if cmd.Name == onlyShow || onlyShow == "" {
reply.WriteString("\r\n") reply.WriteString("\r\n")
reply.WriteString(" " + cmdName + " " + cmd.Args + "\r\n") reply.WriteString(" " + cmd.Name + " " + cmd.Args + "\r\n")
reply.WriteString(" " + cmd.Description + "\r\n") reply.WriteString(" " + cmd.Description + "\r\n")
} didShow = true
} else if len(args) > 1 {
cmdName := args[1]
if cmd, ok := cmdMap[cmdName]; ok {
reply.WriteString("\r\n")
reply.WriteString(" " + cmdName + " " + cmd.Args + "\r\n")
reply.WriteString(" " + cmd.Description + "\r\n")
} else {
return errors.New("no such command name")
} }
} }
if onlyShow != "" && !didShow {
return errors.New("no such command")
}
reply.WriteString("\r\n") reply.WriteString("\r\n")
return nil return nil
} }
@ -210,4 +229,4 @@ func SetConfCmd(reply SshCmdReply, args []string) error {
func GetConfCmd(reply SshCmdReply, args []string) error { func GetConfCmd(reply SshCmdReply, args []string) error {
return errors.New("not implemented") return errors.New("not implemented")
} }