service: fix darwin bug. Move KV code to central location.
This commit is contained in:
+47
-1
@@ -29,7 +29,53 @@ type Config struct {
|
|||||||
UserService bool // Install as a current user service.
|
UserService bool // Install as a current user service.
|
||||||
|
|
||||||
// System specific parameters.
|
// System specific parameters.
|
||||||
KV map[string]interface{}
|
KV KeyValue
|
||||||
|
}
|
||||||
|
|
||||||
|
type KeyValue map[string]interface{}
|
||||||
|
|
||||||
|
// Bool returns the value of the given name, assuming the value is a boolean.
|
||||||
|
// If the value isn't found or is not of the type, the defaultValue is returned.
|
||||||
|
func (kv KeyValue) bool(name string, defaultValue bool) bool {
|
||||||
|
if v, found := kv[name]; found {
|
||||||
|
if castValue, is := v.(bool); is {
|
||||||
|
return castValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int returns the value of the given name, assuming the value is an int.
|
||||||
|
// If the value isn't found or is not of the type, the defaultValue is returned.
|
||||||
|
func (kv KeyValue) int(name string, defaultValue int) int {
|
||||||
|
if v, found := kv[name]; found {
|
||||||
|
if castValue, is := v.(int); is {
|
||||||
|
return castValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int returns the value of the given name, assuming the value is a string.
|
||||||
|
// If the value isn't found or is not of the type, the defaultValue is returned.
|
||||||
|
func (kv KeyValue) string(name string, defaultValue string) string {
|
||||||
|
if v, found := kv[name]; found {
|
||||||
|
if castValue, is := v.(string); is {
|
||||||
|
return castValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int returns the value of the given name, assuming the value is a float64.
|
||||||
|
// If the value isn't found or is not of the type, the defaultValue is returned.
|
||||||
|
func (kv KeyValue) float64(name string, defaultValue float64) float64 {
|
||||||
|
if v, found := kv[name]; found {
|
||||||
|
if castValue, is := v.(float64); is {
|
||||||
|
return castValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaultValue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alpha API. Do not yet use.
|
// Alpha API. Do not yet use.
|
||||||
|
|||||||
+2
-11
@@ -43,15 +43,6 @@ func (s *darwinLaunchdService) getServiceFilePath() (string, error) {
|
|||||||
return "/Library/LaunchDaemons/" + s.Name + ".plist", nil
|
return "/Library/LaunchDaemons/" + s.Name + ".plist", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *darwinLaunchdService) kvBool(name string, def bool) bool {
|
|
||||||
if v, found := s.KV["KeepAlive"]; found {
|
|
||||||
if castValue, is := v.(bool); is {
|
|
||||||
return castValue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return def
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *darwinLaunchdService) Install() error {
|
func (s *darwinLaunchdService) Install() error {
|
||||||
confPath, err := s.getServiceFilePath()
|
confPath, err := s.getServiceFilePath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -81,8 +72,8 @@ func (s *darwinLaunchdService) Install() error {
|
|||||||
}{
|
}{
|
||||||
Config: s.Config,
|
Config: s.Config,
|
||||||
Path: path,
|
Path: path,
|
||||||
KeepAlive: s.kvBool("KeepAlive", true),
|
KeepAlive: s.KV.bool("KeepAlive", true),
|
||||||
RunAtLoad: s.kvBool("RunAtLoad", false),
|
RunAtLoad: s.KV.bool("RunAtLoad", false),
|
||||||
}
|
}
|
||||||
|
|
||||||
functions := template.FuncMap{
|
functions := template.FuncMap{
|
||||||
|
|||||||
Reference in New Issue
Block a user