From 53c3155b6684426cc7309ba2e4eb99aeedf6040d Mon Sep 17 00:00:00 2001 From: Daniel Theophanes Date: Fri, 30 May 2014 13:18:12 -0700 Subject: [PATCH] service: fix darwin bug. Move KV code to central location. --- service.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++- service_darwin.go | 13 ++----------- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/service.go b/service.go index b90b5ff..e051a49 100644 --- a/service.go +++ b/service.go @@ -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. diff --git a/service_darwin.go b/service_darwin.go index d48919c..379be5a 100644 --- a/service_darwin.go +++ b/service_darwin.go @@ -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{