Add shell boilerplate

This commit is contained in:
Manfred Touron 2017-10-31 10:17:06 +01:00
parent fb385f7c49
commit bcaccc9c52
3 changed files with 149 additions and 12 deletions

View file

@ -5,4 +5,4 @@ install:
.PHONY: dev .PHONY: dev
dev: dev:
-go get github.com/githubnemo/CompileDaemon -go get github.com/githubnemo/CompileDaemon
CompileDaemon -build="make install" -command="sshportal --demo --debug" . CompileDaemon -exclude-dir=.git -exclude=".#*" -color=true -command="./sshportal --demo --debug" .

14
main.go
View file

@ -15,15 +15,6 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
) )
var banner = `
__________ _____ __ __
/ __/ __/ // / _ \___ ____/ /____ _/ /
_\ \_\ \/ _ / ___/ _ \/ __/ __/ _ '/ /
/___/___/_//_/_/ \___/_/ \__/\_,_/_/
`
func main() { func main() {
app := cli.NewApp() app := cli.NewApp()
app.Name = path.Base(os.Args[0]) app.Name = path.Base(os.Args[0])
@ -82,8 +73,9 @@ func server(c *cli.Context) error {
switch s.User() { switch s.User() {
case "config": case "config":
io.WriteString(s, banner) if err := shell(s, s.Command()); err != nil {
io.WriteString(s, "Configuration menu not yet implemented.\n\n") io.WriteString(s, fmt.Sprintf("error: %v\n", err))
}
default: default:
host, err := RemoteHostFromSession(s, db) host, err := RemoteHostFromSession(s, db)
if err != nil { if err != nil {

145
shell.go Normal file
View file

@ -0,0 +1,145 @@
package main
import (
"fmt"
"io"
"golang.org/x/crypto/ssh/terminal"
shlex "github.com/anmitsu/go-shlex"
"github.com/gliderlabs/ssh"
"github.com/urfave/cli"
)
var banner = `
__________ _____ __ __
/ __/ __/ // / _ \___ ____/ /____ _/ /
_\ \_\ \/ _ / ___/ _ \/ __/ __/ _ '/ /
/___/___/_//_/_/ \___/_/ \__/\_,_/_/
`
func shell(s ssh.Session, sshCommand []string) error {
if len(sshCommand) == 0 {
io.WriteString(s, banner)
}
cli.AppHelpTemplate = `COMMANDS:
{{range .Commands}}{{if not .HideHelp}} {{join .Names ", "}}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}
`
cli.OsExiter = func(c int) {
// FIXME: forward valid exit code
io.WriteString(s, fmt.Sprintf("exit: %d\n", c))
}
app := cli.NewApp()
app.Writer = s
app.HideVersion = true
app.Commands = []cli.Command{
{
Name: "host",
Usage: "Manage hosts",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "Create a new host",
Action: func(c *cli.Context) error { return nil },
},
{
Name: "inspect",
Usage: "Display detailed information on one or more hosts",
Action: func(c *cli.Context) error { return nil },
},
{
Name: "ls",
Usage: "List hosts",
Action: func(c *cli.Context) error { return nil },
},
{
Name: "rm",
Usage: "Remove one or more hosts",
Action: func(c *cli.Context) error { return nil },
},
},
}, {
Name: "info",
Usage: "Display system-wide information",
Action: func(c *cli.Context) error { return nil },
}, {
Name: "key",
Usage: "Manage keys",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "Create a new key",
Action: func(c *cli.Context) error { return nil },
},
{
Name: "inspect",
Usage: "Display detailed information on one or more keys",
Action: func(c *cli.Context) error { return nil },
},
{
Name: "ls",
Usage: "List keys",
Action: func(c *cli.Context) error { return nil },
},
{
Name: "rm",
Usage: "Remove one or more keys",
Action: func(c *cli.Context) error { return nil },
},
},
}, {
Name: "user",
Usage: "Manage users",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "Create a new user",
Action: func(c *cli.Context) error { return nil },
},
{
Name: "inspect",
Usage: "Display detailed information on one or more users",
Action: func(c *cli.Context) error { return nil },
},
{
Name: "ls",
Usage: "List users",
Action: func(c *cli.Context) error { return nil },
},
{
Name: "rm",
Usage: "Remove one or more users",
Action: func(c *cli.Context) error { return nil },
},
},
},
}
if len(sshCommand) == 0 { // interactive mode
term := terminal.NewTerminal(s, "config> ")
for {
line, err := term.ReadLine()
if err != nil {
return err
}
words, err := shlex.Split(line, true)
if err != nil {
io.WriteString(s, "syntax error.\n")
continue
}
app.Run(append([]string{"config"}, words...))
}
} else { // oneshot mode
app.Run(append([]string{"config"}, sshCommand...))
}
return nil
}