gotty/main.go

146 lines
2.9 KiB
Go
Raw Normal View History

2015-08-16 17:47:23 +08:00
package main
import (
"fmt"
"os"
"github.com/codegangsta/cli"
"github.com/yudai/gotty/app"
2015-08-24 18:22:25 +08:00
"os/signal"
"syscall"
2015-08-16 17:47:23 +08:00
)
func main() {
cmd := cli.NewApp()
cmd.Version = "0.0.3"
2015-08-16 17:47:23 +08:00
cmd.Name = "gotty"
cmd.Usage = "Share your terminal as a web application"
2015-08-21 17:42:04 +08:00
cmd.HideHelp = true
2015-08-16 17:47:23 +08:00
cmd.Flags = []cli.Flag{
cli.StringFlag{
Name: "addr, a",
Value: "",
2015-08-17 10:30:44 +08:00
Usage: "IP address to listen",
2015-08-16 17:47:23 +08:00
EnvVar: "GOTTY_ADDR",
},
cli.StringFlag{
Name: "port, p",
Value: "8080",
2015-08-17 10:30:44 +08:00
Usage: "Port number to listen",
2015-08-16 17:47:23 +08:00
EnvVar: "GOTTY_PORT",
},
cli.BoolFlag{
Name: "permit-write, w",
2015-08-17 10:30:44 +08:00
Usage: "Permit clients to write to the TTY (BE CAREFUL)",
2015-08-16 17:47:23 +08:00
EnvVar: "GOTTY_PERMIT_WRITE",
},
cli.StringFlag{
Name: "credential, c",
Usage: "Credential for Basic Authentication (ex: user:pass)",
EnvVar: "GOTTY_CREDENTIAL",
},
2015-08-19 19:35:04 +08:00
cli.BoolFlag{
Name: "random-url, r",
Usage: "Add a random string to the URL",
EnvVar: "GOTTY_RANDOM_URL",
},
cli.StringFlag{
Name: "profile-file, f",
Value: app.DefaultProfileFilePath,
Usage: "Path to profile file",
EnvVar: "GOTTY_PROFILE_FILE",
},
2015-08-24 15:43:03 +08:00
cli.BoolFlag{
Name: "enable-tls, t",
Usage: "Enable TLS/SSL",
EnvVar: "GOTTY_ENABLE_TLS",
},
cli.StringFlag{
Name: "tls-cert",
Value: app.DefaultTLSCertPath,
Usage: "TLS/SSL cert",
EnvVar: "GOTTY_TLS_CERT",
},
cli.StringFlag{
Name: "tls-key",
Value: app.DefaultTLSKeyPath,
Usage: "TLS/SSL key",
EnvVar: "GOTTY_TLS_KEY",
},
cli.StringFlag{
Name: "title-format",
Value: "GoTTY - {{ .Command }} ({{ .Hostname }})",
Usage: "Title format of browser window",
2015-08-23 20:32:32 +08:00
EnvVar: "GOTTY_TITLE_FORMAT",
},
cli.IntFlag{
Name: "auto-reconnect",
Value: -1,
Usage: "Seconds to automatically reconnect to the server when the connection is closed (default: disabled)",
EnvVar: "GOTTY_AUTO_RECONNECT",
},
2015-08-16 17:47:23 +08:00
}
cmd.Action = func(c *cli.Context) {
if len(c.Args()) == 0 {
2015-08-17 10:30:44 +08:00
fmt.Println("Error: No command given.\n")
2015-08-16 17:47:23 +08:00
cli.ShowAppHelp(c)
os.Exit(1)
}
2015-08-23 19:40:18 +08:00
app, err := app.New(
app.Options{
c.String("addr"),
c.String("port"),
c.Bool("permit-write"),
c.String("credential"),
c.Bool("random-url"),
c.String("profile-file"),
2015-08-24 15:43:03 +08:00
c.Bool("enable-tls"),
c.String("tls-cert"),
c.String("tls-key"),
c.String("title-format"),
c.Int("auto-reconnect"),
c.Args(),
},
)
2015-08-16 17:47:23 +08:00
if err != nil {
fmt.Println(err)
os.Exit(2)
}
2015-08-23 19:40:18 +08:00
2015-08-24 18:22:25 +08:00
registerSignals(app)
2015-08-23 19:40:18 +08:00
err = app.Run()
if err != nil {
fmt.Println(err)
os.Exit(3)
}
2015-08-16 17:47:23 +08:00
}
2015-08-17 10:30:44 +08:00
2015-08-21 17:42:04 +08:00
cli.AppHelpTemplate = helpTemplate
2015-08-17 10:30:44 +08:00
2015-08-16 17:47:23 +08:00
cmd.Run(os.Args)
}
2015-08-24 18:22:25 +08:00
func registerSignals(app *app.App) {
sigChan := make(chan os.Signal, 1)
signal.Notify(
sigChan,
syscall.SIGINT,
syscall.SIGTERM,
)
go func() {
for {
s := <-sigChan
switch s {
case syscall.SIGINT, syscall.SIGTERM:
if !app.Exit() {
os.Exit(4)
}
}
}
}()
}