2015-08-16 09:47:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-25 22:37:07 +00:00
|
|
|
"context"
|
2015-08-16 09:47:23 +00:00
|
|
|
"fmt"
|
2017-08-22 03:11:11 +00:00
|
|
|
"log"
|
2015-08-16 09:47:23 +00:00
|
|
|
"os"
|
2015-08-27 06:23:54 +00:00
|
|
|
"os/signal"
|
2017-08-22 03:11:11 +00:00
|
|
|
"strings"
|
2015-08-27 06:23:54 +00:00
|
|
|
"syscall"
|
2015-08-16 09:47:23 +00:00
|
|
|
|
2020-02-28 06:42:08 +00:00
|
|
|
"github.com/urfave/cli"
|
2015-08-16 09:47:23 +00:00
|
|
|
|
2017-02-25 22:37:07 +00:00
|
|
|
"github.com/yudai/gotty/backend/localcommand"
|
|
|
|
"github.com/yudai/gotty/pkg/homedir"
|
|
|
|
"github.com/yudai/gotty/server"
|
2017-01-09 14:06:46 +00:00
|
|
|
"github.com/yudai/gotty/utils"
|
2015-08-16 09:47:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-02-25 22:37:07 +00:00
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "gotty"
|
2017-08-17 05:04:17 +00:00
|
|
|
app.Version = Version + "+" + CommitID
|
2017-02-25 22:37:07 +00:00
|
|
|
app.Usage = "Share your terminal as a web application"
|
|
|
|
app.HideHelp = true
|
2017-01-09 16:13:36 +00:00
|
|
|
cli.AppHelpTemplate = helpTemplate
|
2015-08-27 06:23:54 +00:00
|
|
|
|
2017-02-25 22:37:07 +00:00
|
|
|
appOptions := &server.Options{}
|
2017-01-09 16:13:36 +00:00
|
|
|
if err := utils.ApplyDefaultValues(appOptions); err != nil {
|
|
|
|
exit(err, 1)
|
|
|
|
}
|
2017-02-25 22:37:07 +00:00
|
|
|
backendOptions := &localcommand.Options{}
|
2017-01-09 16:13:36 +00:00
|
|
|
if err := utils.ApplyDefaultValues(backendOptions); err != nil {
|
2017-01-09 14:06:46 +00:00
|
|
|
exit(err, 1)
|
2015-08-27 06:23:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 16:13:36 +00:00
|
|
|
cliFlags, flagMappings, err := utils.GenerateFlags(appOptions, backendOptions)
|
2015-08-27 06:23:54 +00:00
|
|
|
if err != nil {
|
|
|
|
exit(err, 3)
|
|
|
|
}
|
|
|
|
|
2017-02-25 22:37:07 +00:00
|
|
|
app.Flags = append(
|
2015-08-27 06:23:54 +00:00
|
|
|
cliFlags,
|
2015-08-23 05:04:31 +00:00
|
|
|
cli.StringFlag{
|
2015-08-27 06:23:54 +00:00
|
|
|
Name: "config",
|
|
|
|
Value: "~/.gotty",
|
|
|
|
Usage: "Config file path",
|
|
|
|
EnvVar: "GOTTY_CONFIG",
|
2015-08-24 10:35:49 +00:00
|
|
|
},
|
2015-08-27 06:23:54 +00:00
|
|
|
)
|
|
|
|
|
2017-02-25 22:37:07 +00:00
|
|
|
app.Action = func(c *cli.Context) {
|
2015-08-16 09:47:23 +00:00
|
|
|
if len(c.Args()) == 0 {
|
2017-01-09 16:13:36 +00:00
|
|
|
msg := "Error: No command given."
|
2015-08-16 09:47:23 +00:00
|
|
|
cli.ShowAppHelp(c)
|
2017-01-09 16:13:36 +00:00
|
|
|
exit(fmt.Errorf(msg), 1)
|
2015-08-27 06:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
configFile := c.String("config")
|
2017-02-25 22:37:07 +00:00
|
|
|
_, err := os.Stat(homedir.Expand(configFile))
|
2015-08-27 06:23:54 +00:00
|
|
|
if configFile != "~/.gotty" || !os.IsNotExist(err) {
|
2017-01-09 16:13:36 +00:00
|
|
|
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
|
|
|
|
2017-01-09 16:13:36 +00:00
|
|
|
utils.ApplyFlags(cliFlags, flagMappings, c, appOptions, backendOptions)
|
2015-08-27 06:23:54 +00:00
|
|
|
|
2017-01-09 16:13:36 +00:00
|
|
|
appOptions.EnableBasicAuth = c.IsSet("credential")
|
|
|
|
appOptions.EnableTLSClientAuth = c.IsSet("tls-ca-crt")
|
2015-10-05 07:08:25 +00:00
|
|
|
|
2017-02-25 22:37:07 +00:00
|
|
|
err = appOptions.Validate()
|
|
|
|
if err != nil {
|
2015-10-05 07:08:25 +00:00
|
|
|
exit(err, 6)
|
|
|
|
}
|
2015-08-27 06:23:54 +00:00
|
|
|
|
2017-02-25 22:37:07 +00:00
|
|
|
args := c.Args()
|
|
|
|
factory, err := localcommand.NewFactory(args[0], args[1:], backendOptions)
|
2017-01-09 16:13:36 +00:00
|
|
|
if err != nil {
|
|
|
|
exit(err, 3)
|
|
|
|
}
|
2017-02-25 22:37:07 +00: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 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
|
|
|
|
2017-02-25 22:37:07 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
gCtx, gCancel := context.WithCancel(context.Background())
|
2015-08-24 10:22:25 +00:00
|
|
|
|
2017-08-22 03:11:11 +00:00
|
|
|
log.Printf("GoTTY is starting with command: %s", strings.Join(args, " "))
|
|
|
|
|
2017-02-25 22:37:07 +00: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 11:40:18 +00:00
|
|
|
}
|
2017-02-25 22:37:07 +00:00
|
|
|
|
2015-08-16 09:47:23 +00:00
|
|
|
}
|
2017-02-25 22:37:07 +00:00
|
|
|
app.Run(os.Args)
|
2015-08-16 09:47:23 +00:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
2017-02-25 22:37:07 +00:00
|
|
|
func waitSignals(errs chan error, cancel context.CancelFunc, gracefullCancel context.CancelFunc) error {
|
2015-08-24 10:22:25 +00:00
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(
|
|
|
|
sigChan,
|
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
)
|
|
|
|
|
2017-02-25 22:37:07 +00: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 04:38:42 +00:00
|
|
|
fmt.Println("Force closing...")
|
2017-02-25 22:37:07 +00:00
|
|
|
cancel()
|
|
|
|
return <-errs
|
2015-08-24 10:22:25 +00:00
|
|
|
}
|
2017-02-25 22:37:07 +00:00
|
|
|
default:
|
|
|
|
cancel()
|
|
|
|
return <-errs
|
2015-08-24 10:22:25 +00:00
|
|
|
}
|
2017-02-25 22:37:07 +00:00
|
|
|
}
|
2015-08-24 10:22:25 +00:00
|
|
|
}
|