gotty/main.go

153 lines
3.2 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"
2017-08-22 11:11:11 +08:00
"log"
2015-08-16 17:47:23 +08:00
"os"
2015-08-27 14:23:54 +08:00
"os/signal"
2017-08-22 11:11:11 +08:00
"strings"
2015-08-27 14:23:54 +08:00
"syscall"
2015-08-16 17:47:23 +08:00
2021-04-13 11:58:39 +08:00
cli "github.com/urfave/cli/v2"
2015-08-16 17:47:23 +08:00
"github.com/sorenisanerd/gotty/backend/localcommand"
"github.com/sorenisanerd/gotty/pkg/homedir"
"github.com/sorenisanerd/gotty/server"
"github.com/sorenisanerd/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"
2021-04-18 22:01:31 +08:00
app.HideHelpCommand = true
2017-02-26 06:37:07 +08:00
appOptions := &server.Options{}
2021-04-18 22:01:31 +08:00
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,
2021-04-13 11:58:39 +08:00
&cli.StringFlag{
Name: "config",
Value: "~/.gotty",
Usage: "Config file path",
EnvVars: []string{"GOTTY_CONFIG"},
},
2015-08-27 14:23:54 +08:00
)
2021-04-13 11:58:39 +08:00
app.Action = func(c *cli.Context) error {
if c.NArg() == 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()
2021-04-13 11:58:39 +08:00
factory, err := localcommand.NewFactory(args.First(), args.Tail(), backendOptions)
if err != nil {
exit(err, 3)
}
2017-02-26 06:37:07 +08:00
hostname, _ := os.Hostname()
appOptions.TitleVariables = map[string]interface{}{
2021-04-13 11:58:39 +08:00
"command": args.First(),
"argv": args.Tail(),
2017-02-26 06:37:07 +08:00
"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
2021-04-13 11:58:39 +08:00
log.Printf("GoTTY is starting with command: %s", strings.Join(args.Slice(), " "))
2017-08-22 11:11:11 +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
2021-04-13 11:58:39 +08:00
return nil
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
}