gotty/main.go

120 lines
2.3 KiB
Go
Raw Normal View History

2015-08-16 09:47:23 +00:00
package main
import (
"fmt"
"os"
2015-08-27 06:23:54 +00:00
"os/signal"
"syscall"
2015-08-16 09:47:23 +00:00
"github.com/codegangsta/cli"
"github.com/yudai/gotty/app"
"github.com/yudai/gotty/backends/ptycommand"
"github.com/yudai/gotty/utils"
2015-08-16 09:47:23 +00:00
)
func main() {
cmd := cli.NewApp()
cmd.Name = "gotty"
cmd.Version = app.Version
2015-08-16 09:47:23 +00:00
cmd.Usage = "Share your terminal as a web application"
2015-08-21 09:42:04 +00:00
cmd.HideHelp = true
cli.AppHelpTemplate = helpTemplate
2015-08-27 06:23:54 +00:00
appOptions := &app.Options{}
if err := utils.ApplyDefaultValues(appOptions); err != nil {
exit(err, 1)
}
backendOptions := &ptycommand.Options{}
if err := utils.ApplyDefaultValues(backendOptions); err != nil {
exit(err, 1)
2015-08-27 06:23:54 +00:00
}
cliFlags, flagMappings, err := utils.GenerateFlags(appOptions, backendOptions)
2015-08-27 06:23:54 +00:00
if err != nil {
exit(err, 3)
}
cmd.Flags = append(
cliFlags,
cli.StringFlag{
2015-08-27 06:23:54 +00:00
Name: "config",
Value: "~/.gotty",
Usage: "Config file path",
EnvVar: "GOTTY_CONFIG",
},
2015-08-27 06:23:54 +00:00
)
2015-08-16 09:47:23 +00:00
cmd.Action = func(c *cli.Context) {
if len(c.Args()) == 0 {
msg := "Error: No command given."
2015-08-16 09:47:23 +00:00
cli.ShowAppHelp(c)
exit(fmt.Errorf(msg), 1)
2015-08-27 06:23:54 +00:00
}
configFile := c.String("config")
_, err := os.Stat(utils.ExpandHomeDir(configFile))
2015-08-27 06:23:54 +00:00
if configFile != "~/.gotty" || !os.IsNotExist(err) {
if err := utils.ApplyConfigFile(configFile, appOptions, backendOptions); err != nil {
2015-08-27 06:23:54 +00:00
exit(err, 2)
}
2015-08-16 09:47:23 +00:00
}
2015-08-23 11:40:18 +00:00
utils.ApplyFlags(cliFlags, flagMappings, c, appOptions, backendOptions)
2015-08-27 06:23:54 +00:00
appOptions.EnableBasicAuth = c.IsSet("credential")
appOptions.EnableTLSClientAuth = c.IsSet("tls-ca-crt")
if err := app.CheckConfig(appOptions); err != nil {
exit(err, 6)
}
2015-08-27 06:23:54 +00:00
manager, err := ptycommand.NewCommandClientContextManager(c.Args(), backendOptions)
if err != nil {
exit(err, 3)
}
app, err := app.New(manager, appOptions)
2015-08-16 09:47:23 +00:00
if err != nil {
2015-08-27 06:23:54 +00:00
exit(err, 3)
2015-08-16 09:47:23 +00:00
}
2015-08-23 11:40:18 +00:00
2015-08-24 10:22:25 +00:00
registerSignals(app)
2015-08-23 11:40:18 +00:00
err = app.Run()
if err != nil {
2015-08-27 06:23:54 +00:00
exit(err, 4)
2015-08-23 11:40:18 +00:00
}
2015-08-16 09:47:23 +00:00
}
cmd.Run(os.Args)
}
2015-08-24 10:22:25 +00:00
2015-08-27 06:23:54 +00:00
func exit(err error, code int) {
2015-08-29 04:11:46 +00:00
if err != nil {
fmt.Println(err)
}
2015-08-27 06:23:54 +00:00
os.Exit(code)
}
2015-08-24 10:22:25 +00: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:
2015-09-01 06:07:04 +00:00
if app.Exit() {
fmt.Println("Send ^C to force exit.")
} else {
2015-08-27 06:23:54 +00:00
os.Exit(5)
2015-08-24 10:22:25 +00:00
}
}
}
}()
}