gotty/backend/localcommand/factory.go

49 lines
1.3 KiB
Go
Raw Permalink Normal View History

2017-02-26 06:37:07 +08:00
package localcommand
import (
"syscall"
2017-08-20 12:39:06 +08:00
"time"
2017-02-26 06:37:07 +08:00
"github.com/sorenisanerd/gotty/server"
2017-02-26 06:37:07 +08:00
)
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
2017-08-20 12:39:06 +08:00
opts []Option
2017-02-26 06:37:07 +08:00
}
func NewFactory(command string, argv []string, options *Options) (*Factory, error) {
2017-08-20 12:39:06 +08:00
opts := []Option{WithCloseSignal(syscall.Signal(options.CloseSignal))}
if options.CloseTimeout >= 0 {
opts = append(opts, WithCloseTimeout(time.Duration(options.CloseTimeout)*time.Second))
}
2017-02-26 06:37:07 +08:00
return &Factory{
command: command,
argv: argv,
options: options,
2017-08-20 12:39:06 +08:00
opts: opts,
2017-02-26 06:37:07 +08:00
}, nil
}
func (factory *Factory) Name() string {
return "local command"
}
func (factory *Factory) New(params map[string][]string, headers map[string][]string) (server.Slave, error) {
2017-02-26 06:37:07 +08:00
argv := make([]string, len(factory.argv))
copy(argv, factory.argv)
if params["arg"] != nil && len(params["arg"]) > 0 {
argv = append(argv, params["arg"]...)
}
2017-08-20 12:39:06 +08:00
return New(factory.command, argv, headers, factory.opts...)
2017-02-26 06:37:07 +08:00
}