gotty/main.go

149 lines
3.0 KiB
Go
Raw Normal View History

2015-08-16 17:47:23 +08:00
package main
import (
2017-02-26 06:37:07 +08:00
"context"
2015-08-16 17:47:23 +08:00
"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"
2017-02-26 06:37:07 +08:00
"github.com/yudai/gotty/backend/localcommand"
"github.com/yudai/gotty/pkg/homedir"
"github.com/yudai/gotty/server"
"github.com/yudai/gotty/utils"
2015-08-16 17:47:23 +08:00
)
func main() {
2017-02-26 06:37:07 +08:00
app := cli.NewApp()
app.Name = "gotty"
2017-08-17 13:04:17 +08:00
app.Version = Version + "+" + CommitID
2017-02-26 06:37:07 +08:00
app.Usage = "Share your terminal as a web application"
app.HideHelp = true
cli.AppHelpTemplate = helpTemplate
2015-08-27 14:23:54 +08:00
2017-02-26 06:37:07 +08:00
appOptions := &server.Options{}
if err := utils.ApplyDefaultValues(appOptions); err != nil {
exit(err, 1)
}
2017-02-26 06:37:07 +08:00
backendOptions := &localcommand.Options{}
if err := utils.ApplyDefaultValues(backendOptions); err != nil {
exit(err, 1)
2015-08-27 14:23:54 +08:00
}
cliFlags, flagMappings, err := utils.GenerateFlags(appOptions, backendOptions)
2015-08-27 14:23:54 +08:00
if err != nil {
exit(err, 3)
}
2017-02-26 06:37:07 +08:00
app.Flags = append(
2015-08-27 14:23:54 +08:00
cliFlags,
cli.StringFlag{
2015-08-27 14:23:54 +08:00
Name: "config",
Value: "~/.gotty",
Usage: "Config file path",
EnvVar: "GOTTY_CONFIG",
},
2015-08-27 14:23:54 +08:00
)
2017-02-26 06:37:07 +08:00
app.Action = func(c *cli.Context) {
2015-08-16 17:47:23 +08:00
if len(c.Args()) == 0 {
msg := "Error: No command given."
2015-08-16 17:47:23 +08:00
cli.ShowAppHelp(c)
exit(fmt.Errorf(msg), 1)
2015-08-27 14:23:54 +08:00
}
configFile := c.String("config")
2017-02-26 06:37:07 +08:00
_, err := os.Stat(homedir.Expand(configFile))
2015-08-27 14:23:54 +08:00
if configFile != "~/.gotty" || !os.IsNotExist(err) {
if err := utils.ApplyConfigFile(configFile, appOptions, backendOptions); 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
utils.ApplyFlags(cliFlags, flagMappings, c, appOptions, backendOptions)
2015-08-27 14:23:54 +08:00
appOptions.EnableBasicAuth = c.IsSet("credential")
appOptions.EnableTLSClientAuth = c.IsSet("tls-ca-crt")
2017-02-26 06:37:07 +08:00
err = appOptions.Validate()
if err != nil {
exit(err, 6)
}
2015-08-27 14:23:54 +08:00
2017-02-26 06:37:07 +08:00
args := c.Args()
factory, err := localcommand.NewFactory(args[0], args[1:], backendOptions)
if err != nil {
exit(err, 3)
}
2017-02-26 06:37:07 +08:00
hostname, _ := os.Hostname()
appOptions.TitleVariables = map[string]interface{}{
"command": args[0],
"argv": args[1:],
"hostname": hostname,
}
srv, err := server.New(factory, appOptions)
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
2017-02-26 06:37:07 +08:00
ctx, cancel := context.WithCancel(context.Background())
gCtx, gCancel := context.WithCancel(context.Background())
2015-08-24 18:22:25 +08:00
2017-02-26 06:37:07 +08:00
errs := make(chan error, 1)
go func() {
errs <- srv.Run(ctx, server.WithGracefullContext(gCtx))
}()
err = waitSignals(errs, cancel, gCancel)
if err != nil && err != context.Canceled {
fmt.Printf("Error: %s\n", err)
exit(err, 8)
2015-08-23 19:40:18 +08:00
}
2017-02-26 06:37:07 +08:00
2015-08-16 17:47:23 +08:00
}
2017-02-26 06:37:07 +08:00
app.Run(os.Args)
2015-08-16 17:47:23 +08:00
}
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)
}
2017-02-26 06:37:07 +08:00
func waitSignals(errs chan error, cancel context.CancelFunc, gracefullCancel context.CancelFunc) error {
2015-08-24 18:22:25 +08:00
sigChan := make(chan os.Signal, 1)
signal.Notify(
sigChan,
syscall.SIGINT,
syscall.SIGTERM,
)
2017-02-26 06:37:07 +08:00
select {
case err := <-errs:
return err
case s := <-sigChan:
switch s {
case syscall.SIGINT:
gracefullCancel()
fmt.Println("C-C to force close")
select {
case err := <-errs:
return err
case <-sigChan:
2017-08-20 12:38:42 +08:00
fmt.Println("Force closing...")
2017-02-26 06:37:07 +08:00
cancel()
return <-errs
2015-08-24 18:22:25 +08:00
}
2017-02-26 06:37:07 +08:00
default:
cancel()
return <-errs
2015-08-24 18:22:25 +08:00
}
2017-02-26 06:37:07 +08:00
}
2015-08-24 18:22:25 +08:00
}