add create options
This commit is contained in:
parent
c52651c4d0
commit
1b08036a0b
@ -2,7 +2,6 @@ package runtime
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -50,14 +49,26 @@ func newRuntime() *runtime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newService(s *Service) *service {
|
func newService(s *Service, c CreateOptions) *service {
|
||||||
|
var exec string
|
||||||
|
var args []string
|
||||||
|
|
||||||
|
if len(s.Exec) > 0 {
|
||||||
parts := strings.Split(s.Exec, " ")
|
parts := strings.Split(s.Exec, " ")
|
||||||
exec := parts[0]
|
exec = parts[0]
|
||||||
args := []string{}
|
args = []string{}
|
||||||
|
|
||||||
if len(parts) > 1 {
|
if len(parts) > 1 {
|
||||||
args = parts[1:]
|
args = parts[1:]
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// set command
|
||||||
|
exec = c.Command[0]
|
||||||
|
// set args
|
||||||
|
if len(c.Command) > 1 {
|
||||||
|
args = c.Command[1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &service{
|
return &service{
|
||||||
Service: s,
|
Service: s,
|
||||||
@ -67,7 +78,7 @@ func newService(s *Service) *service {
|
|||||||
Name: s.Name,
|
Name: s.Name,
|
||||||
Path: exec,
|
Path: exec,
|
||||||
},
|
},
|
||||||
Env: os.Environ(),
|
Env: c.Env,
|
||||||
Args: args,
|
Args: args,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -189,7 +200,7 @@ func (r *runtime) run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *runtime) Create(s *Service) error {
|
func (r *runtime) Create(s *Service, opts ...CreateOption) error {
|
||||||
r.Lock()
|
r.Lock()
|
||||||
defer r.Unlock()
|
defer r.Unlock()
|
||||||
|
|
||||||
@ -197,8 +208,17 @@ func (r *runtime) Create(s *Service) error {
|
|||||||
return errors.New("service already registered")
|
return errors.New("service already registered")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var options CreateOptions
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(s.Exec) == 0 && len(options.Command) == 0 {
|
||||||
|
return errors.New("missing exec command")
|
||||||
|
}
|
||||||
|
|
||||||
// save service
|
// save service
|
||||||
r.services[s.Name] = newService(s)
|
r.services[s.Name] = newService(s, options)
|
||||||
|
|
||||||
// push into start queue
|
// push into start queue
|
||||||
r.start <- r.services[s.Name]
|
r.start <- r.services[s.Name]
|
||||||
|
27
runtime/options.go
Normal file
27
runtime/options.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package runtime
|
||||||
|
|
||||||
|
type CreateOption func(o *CreateOptions)
|
||||||
|
|
||||||
|
type CreateOptions struct {
|
||||||
|
// command to execute including args
|
||||||
|
Command []string
|
||||||
|
// Environment to configure
|
||||||
|
Env []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command specifies the command to execute
|
||||||
|
func WithCommand(c string, args ...string) CreateOption {
|
||||||
|
return func(o *CreateOptions) {
|
||||||
|
// set command
|
||||||
|
o.Command = []string{c}
|
||||||
|
// set args
|
||||||
|
o.Command = append(o.Command, args...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Env sets the created service env
|
||||||
|
func WithEnv(env []string) CreateOption {
|
||||||
|
return func(o *CreateOptions) {
|
||||||
|
o.Env = env
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,7 @@ package runtime
|
|||||||
// Runtime is a service runtime manager
|
// Runtime is a service runtime manager
|
||||||
type Runtime interface {
|
type Runtime interface {
|
||||||
// Registers a service
|
// Registers a service
|
||||||
Create(*Service) error
|
Create(*Service, ...CreateOption) error
|
||||||
// Remove a service
|
// Remove a service
|
||||||
Delete(*Service) error
|
Delete(*Service) error
|
||||||
// starts the runtime
|
// starts the runtime
|
||||||
@ -28,8 +28,8 @@ var (
|
|||||||
DefaultRuntime = newRuntime()
|
DefaultRuntime = newRuntime()
|
||||||
)
|
)
|
||||||
|
|
||||||
func Create(s *Service) error {
|
func Create(s *Service, opts ...CreateOption) error {
|
||||||
return DefaultRuntime.Create(s)
|
return DefaultRuntime.Create(s, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Delete(s *Service) error {
|
func Delete(s *Service) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user