gotty/main.go

163 lines
3.2 KiB
Go
Raw Normal View History

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"
2023-10-21 15:49:19 +00:00
"io"
2022-01-12 23:34:32 +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
2021-04-13 03:58:39 +00:00
cli "github.com/urfave/cli/v2"
2015-08-16 09:47:23 +00: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 09:47:23 +00:00
)
func main() {
2017-02-25 22:37:07 +00:00
app := cli.NewApp()
app.Name = "gotty"
2021-04-22 15:20:08 +00:00
app.Version = Version
2017-02-25 22:37:07 +00:00
app.Usage = "Share your terminal as a web application"
2021-04-18 14:01:31 +00:00
app.HideHelpCommand = true
2017-02-25 22:37:07 +00:00
appOptions := &server.Options{}
2021-04-18 14:01:31 +00:00
if err := utils.ApplyDefaultValues(appOptions); err != nil {
exit(err, 1)
}
2017-02-25 22:37:07 +00:00
backendOptions := &localcommand.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)
}
2017-02-25 22:37:07 +00:00
app.Flags = append(
2015-08-27 06:23:54 +00:00
cliFlags,
2021-04-13 03:58:39 +00:00
&cli.StringFlag{
Name: "config",
Value: "~/.gotty",
Usage: "Config file path",
EnvVars: []string{"GOTTY_CONFIG"},
},
2015-08-27 06:23:54 +00:00
)
2021-04-13 03:58:39 +00:00
app.Action = func(c *cli.Context) error {
if c.NArg() == 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")
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) {
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)
2021-11-09 16:23:04 +00:00
if appOptions.Quiet {
log.SetFlags(0)
2023-10-21 15:49:19 +00:00
log.SetOutput(io.Discard)
2021-11-09 16:23:04 +00:00
}
if c.IsSet("credential") {
appOptions.EnableBasicAuth = true
}
if c.IsSet("tls-ca-crt") {
appOptions.EnableTLSClientAuth = true
}
2017-02-25 22:37:07 +00:00
err = appOptions.Validate()
if err != nil {
exit(err, 6)
}
2015-08-27 06:23:54 +00:00
2017-02-25 22:37:07 +00:00
args := c.Args()
2021-04-13 03:58:39 +00:00
factory, err := localcommand.NewFactory(args.First(), args.Tail(), backendOptions)
if err != nil {
exit(err, 3)
}
2017-02-25 22:37:07 +00:00
hostname, _ := os.Hostname()
appOptions.TitleVariables = map[string]interface{}{
2021-04-13 03:58:39 +00:00
"command": args.First(),
"argv": args.Tail(),
2017-02-25 22:37:07 +00:00
"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
2021-04-13 03:58:39 +00:00
log.Printf("GoTTY is starting with command: %s", strings.Join(args.Slice(), " "))
2017-08-22 03:11:11 +00:00
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
2021-04-13 03:58:39 +00:00
return nil
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
}