refactor: add helper method for getting the path of a property

This commit is contained in:
Jose Diaz-Gonzalez committed 2019-03-05 21:34:26 -05:00
1 parent 34f5eaeaa2
commit 83f5e380d4
1 file changed
+9 -9
+9 -9
View File
@@ -40,8 +40,7 @@ func CommandPropertySet(pluginName, appName, property, value string, properties
// PropertyDelete deletes a property from the plugin properties for an app // PropertyDelete deletes a property from the plugin properties for an app
func PropertyDelete(pluginName string, appName string, property string) { func PropertyDelete(pluginName string, appName string, property string) {
pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName) propertyPath := getPropertyPath(pluginName, appName, property)
propertyPath := strings.Join([]string{pluginAppConfigRoot, property}, "/")
if err := os.Remove(propertyPath); err != nil { if err := os.Remove(propertyPath); err != nil {
LogFail(fmt.Sprintf("Unable to remove %s property %s.%s", pluginName, appName, property)) LogFail(fmt.Sprintf("Unable to remove %s property %s.%s", pluginName, appName, property))
} }
@@ -60,8 +59,7 @@ func PropertyDestroy(pluginName string, appName string) {
// PropertyExists returns whether a property exists or not // PropertyExists returns whether a property exists or not
func PropertyExists(pluginName string, appName string, property string) bool { func PropertyExists(pluginName string, appName string, property string) bool {
pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName) propertyPath := getPropertyPath(pluginName, appName, property)
propertyPath := strings.Join([]string{pluginAppConfigRoot, property}, "/")
_, err := os.Stat(propertyPath) _, err := os.Stat(propertyPath)
return !os.IsNotExist(err) return !os.IsNotExist(err)
} }
@@ -77,9 +75,7 @@ func PropertyGetDefault(pluginName, appName, property, defaultValue string) (val
return return
} }
pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName) propertyPath := getPropertyPath(pluginName, appName, property)
propertyPath := strings.Join([]string{pluginAppConfigRoot, property}, "/")
b, err := ioutil.ReadFile(propertyPath) b, err := ioutil.ReadFile(propertyPath)
if err != nil { if err != nil {
LogWarn(fmt.Sprintf("Unable to read %s property %s.%s", pluginName, appName, property)) LogWarn(fmt.Sprintf("Unable to read %s property %s.%s", pluginName, appName, property))
@@ -95,8 +91,7 @@ func PropertyWrite(pluginName string, appName string, property string, value str
LogFail(fmt.Sprintf("Unable to create %s config directory for %s: %s", pluginName, appName, err.Error())) LogFail(fmt.Sprintf("Unable to create %s config directory for %s: %s", pluginName, appName, err.Error()))
} }
pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName) propertyPath := getPropertyPath(pluginName, appName, property)
propertyPath := strings.Join([]string{pluginAppConfigRoot, property}, "/")
file, err := os.Create(propertyPath) file, err := os.Create(propertyPath)
if err != nil { if err != nil {
LogFail(fmt.Sprintf("Unable to write %s config value %s.%s: %s", pluginName, appName, property, err.Error())) LogFail(fmt.Sprintf("Unable to write %s config value %s.%s: %s", pluginName, appName, property, err.Error()))
@@ -117,6 +112,11 @@ func PropertySetup(pluginName string) (err error) {
return setPermissions(pluginConfigRoot, 0755) return setPermissions(pluginConfigRoot, 0755)
} }
func getPropertyPath(pluginName string, appName string, property string) string {
pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName)
return strings.Join([]string{pluginAppConfigRoot, property}, "/")
}
// getPluginAppPropertyPath returns the plugin property path for a given plugin/app combination // getPluginAppPropertyPath returns the plugin property path for a given plugin/app combination
func getPluginAppPropertyPath(pluginName string, appName string) string { func getPluginAppPropertyPath(pluginName string, appName string) string {
return strings.Join([]string{getPluginConfigPath(pluginName), appName}, "/") return strings.Join([]string{getPluginConfigPath(pluginName), appName}, "/")