2015-08-16 17:47:23 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-08-27 14:23:54 +08:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2015-08-16 17:47:23 +08:00
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
|
|
|
|
"github.com/yudai/gotty/app"
|
2017-01-09 22:06:46 +08:00
|
|
|
"github.com/yudai/gotty/utils"
|
2015-08-16 17:47:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cmd := cli.NewApp()
|
2015-10-02 16:01:37 +08:00
|
|
|
cmd.Version = app.Version
|
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-27 14:23:54 +08:00
|
|
|
|
2017-01-09 22:06:46 +08:00
|
|
|
options := &app.Options{}
|
|
|
|
if err := utils.ApplyDefaultValues(options); err != nil {
|
|
|
|
exit(err, 1)
|
2015-08-27 14:23:54 +08:00
|
|
|
}
|
|
|
|
|
2017-01-09 22:06:46 +08:00
|
|
|
cliFlags, flagMappings, err := utils.GenerateFlags(options)
|
2015-08-27 14:23:54 +08:00
|
|
|
if err != nil {
|
|
|
|
exit(err, 3)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags = append(
|
|
|
|
cliFlags,
|
2015-08-23 13:04:31 +08:00
|
|
|
cli.StringFlag{
|
2015-08-27 14:23:54 +08:00
|
|
|
Name: "config",
|
|
|
|
Value: "~/.gotty",
|
|
|
|
Usage: "Config file path",
|
|
|
|
EnvVar: "GOTTY_CONFIG",
|
2015-08-24 18:35:49 +08:00
|
|
|
},
|
2015-08-27 14:23:54 +08:00
|
|
|
)
|
|
|
|
|
2015-08-16 17:47:23 +08:00
|
|
|
cmd.Action = func(c *cli.Context) {
|
|
|
|
if len(c.Args()) == 0 {
|
|
|
|
cli.ShowAppHelp(c)
|
2017-01-09 22:06:46 +08:00
|
|
|
exit(fmt.Errorf("Error: No command given."), 1)
|
2015-08-27 14:23:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
configFile := c.String("config")
|
2017-01-09 22:06:46 +08:00
|
|
|
_, err := os.Stat(utils.ExpandHomeDir(configFile))
|
2015-08-27 14:23:54 +08:00
|
|
|
if configFile != "~/.gotty" || !os.IsNotExist(err) {
|
2017-01-09 22:06:46 +08:00
|
|
|
if err := utils.ApplyConfigFile(configFile, options); err != nil {
|
2015-08-27 14:23:54 +08:00
|
|
|
exit(err, 2)
|
|
|
|
}
|
2015-08-16 17:47:23 +08:00
|
|
|
}
|
2015-08-23 19:40:18 +08:00
|
|
|
|
2017-01-09 22:06:46 +08:00
|
|
|
utils.ApplyFlags(cliFlags, flagMappings, c, options)
|
2015-08-27 14:23:54 +08:00
|
|
|
|
2017-01-09 22:06:46 +08:00
|
|
|
options.EnableBasicAuth = c.IsSet("credential")
|
|
|
|
options.EnableTLSClientAuth = c.IsSet("tls-ca-crt")
|
2015-10-05 15:08:25 +08:00
|
|
|
|
2017-01-09 22:06:46 +08:00
|
|
|
if err := app.CheckConfig(options); err != nil {
|
2015-10-05 15:08:25 +08:00
|
|
|
exit(err, 6)
|
|
|
|
}
|
2015-08-27 14:23:54 +08:00
|
|
|
|
2017-01-09 22:06:46 +08:00
|
|
|
app, err := app.New(c.Args(), options)
|
2015-08-16 17:47:23 +08:00
|
|
|
if err != nil {
|
2015-08-27 14:23:54 +08:00
|
|
|
exit(err, 3)
|
2015-08-16 17:47:23 +08:00
|
|
|
}
|
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 {
|
2015-08-27 14:23:54 +08:00
|
|
|
exit(err, 4)
|
2015-08-23 19:40:18 +08:00
|
|
|
}
|
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
|
|
|
|
2015-08-27 14:23:54 +08:00
|
|
|
func exit(err error, code int) {
|
2015-08-29 12:11:46 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2015-08-27 14:23:54 +08:00
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
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:
|
2015-09-01 14:07:04 +08:00
|
|
|
if app.Exit() {
|
|
|
|
fmt.Println("Send ^C to force exit.")
|
|
|
|
} else {
|
2015-08-27 14:23:54 +08:00
|
|
|
os.Exit(5)
|
2015-08-24 18:22:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|