From 4db7ebd74373afee23d510f253a34fe7a78d56bb Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 21 Jan 2019 17:06:02 -0500 Subject: [PATCH] feat: add helper method for ensuring a property file exists --- plugins/common/properties.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/plugins/common/properties.go b/plugins/common/properties.go index 21552f66a..82a4045a8 100644 --- a/plugins/common/properties.go +++ b/plugins/common/properties.go @@ -85,6 +85,26 @@ func PropertyGetDefault(pluginName, appName, property, defaultValue string) (val return } +// PropertyTouch ensures a given application property file exists +func PropertyTouch(pluginName string, appName string, property string) error { + if err := makePluginAppPropertyPath(pluginName, appName); err != nil { + return errors.New(fmt.Sprintf("Unable to create %s config directory for %s: %s", pluginName, appName, err.Error())) + } + + propertyPath := getPropertyPath(pluginName, appName, property) + if PropertyExists(pluginName, appName, property) { + return nil + } + + file, err := os.Create(propertyPath) + if err != nil { + return errors.New(fmt.Sprintf("Unable to write %s config value %s.%s: %s", pluginName, appName, property, err.Error())) + } + defer file.Close() + + return nil +} + // PropertyWrite writes a value for a given application property func PropertyWrite(pluginName string, appName string, property string, value string) { if err := makePropertyPath(pluginName, appName); err != nil {