service: fix darwin bug. Move KV code to central location.

This commit is contained in:
Daniel Theophanes
2014-05-30 13:18:12 -07:00
parent 2d3b507a40
commit 53c3155b66
2 changed files with 49 additions and 12 deletions
+47 -1
View File
@@ -29,7 +29,53 @@ type Config struct {
UserService bool // Install as a current user service.
// 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.
+2 -11
View File
@@ -43,15 +43,6 @@ func (s *darwinLaunchdService) getServiceFilePath() (string, error) {
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 {
confPath, err := s.getServiceFilePath()
if err != nil {
@@ -81,8 +72,8 @@ func (s *darwinLaunchdService) Install() error {
}{
Config: s.Config,
Path: path,
KeepAlive: s.kvBool("KeepAlive", true),
RunAtLoad: s.kvBool("RunAtLoad", false),
KeepAlive: s.KV.bool("KeepAlive", true),
RunAtLoad: s.KV.bool("RunAtLoad", false),
}
functions := template.FuncMap{