mirror of
https://github.com/yudai/gotty.git
synced 2025-12-24 19:30:13 +01:00
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package localcommand
|
|
|
|
import (
|
|
"context"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/ghthor/gotty/v2/server"
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
type Options struct {
|
|
CloseSignal int `hcl:"close_signal" flagName:"close-signal" flagSName:"" flagDescribe:"Signal sent to the command process when gotty close it (default: SIGHUP)" default:"1"`
|
|
CloseTimeout int `hcl:"close_timeout" flagName:"close-timeout" flagSName:"" flagDescribe:"Time in seconds to force kill process after client is disconnected (default: -1)" default:"-1"`
|
|
}
|
|
|
|
type Factory struct {
|
|
command string
|
|
argv []string
|
|
options *Options
|
|
opts []Option
|
|
}
|
|
|
|
func NewFactory(command string, argv []string, options *Options) (*Factory, error) {
|
|
opts := []Option{WithCloseSignal(syscall.Signal(options.CloseSignal))}
|
|
if options.CloseTimeout >= 0 {
|
|
opts = append(opts, WithCloseTimeout(time.Duration(options.CloseTimeout)*time.Second))
|
|
}
|
|
|
|
return &Factory{
|
|
command: command,
|
|
argv: argv,
|
|
options: options,
|
|
opts: opts,
|
|
}, nil
|
|
}
|
|
|
|
func (factory *Factory) Name() string {
|
|
return "local command"
|
|
}
|
|
|
|
func (factory *Factory) New(ctx context.Context, params map[string][]string, conn *websocket.Conn) (server.Slave, error) {
|
|
argv := make([]string, len(factory.argv))
|
|
copy(argv, factory.argv)
|
|
if len(params["arg"]) > 0 {
|
|
argv = append(argv, params["arg"]...)
|
|
}
|
|
|
|
return New(ctx, factory.command, argv, factory.opts...)
|
|
}
|