refactor: ensure all property-handling functions return an error
This will allow upstream functions to handle errors in the most appropriate manner
This commit is contained in:
1 parent
4db7ebd743
commit
6f1e964c9f
4 files changed
+33
-16
No files matched your search
@@ -1,6 +1,7 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
@@ -34,27 +35,32 @@ func CommandPropertySet(pluginName, appName, property, value string, properties
|
|||||||
PropertyWrite(pluginName, appName, property, value)
|
PropertyWrite(pluginName, appName, property, value)
|
||||||
} else {
|
} else {
|
||||||
LogInfo2Quiet(fmt.Sprintf("Unsetting %s", property))
|
LogInfo2Quiet(fmt.Sprintf("Unsetting %s", property))
|
||||||
PropertyDelete(pluginName, appName, property)
|
err := PropertyDelete(pluginName, appName, property)
|
||||||
|
if err != nil {
|
||||||
|
LogFail(err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) error {
|
||||||
propertyPath := getPropertyPath(pluginName, appName, property)
|
propertyPath := getPropertyPath(pluginName, appName, 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))
|
return errors.New(fmt.Sprintf("Unable to remove %s property %s.%s", pluginName, appName, property))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PropertyDestroy destroys the plugin properties for an app
|
// PropertyDestroy destroys the plugin properties for an app
|
||||||
func PropertyDestroy(pluginName string, appName string) {
|
func PropertyDestroy(pluginName string, appName string) error {
|
||||||
if appName == "_all_" {
|
if appName == "_all_" {
|
||||||
pluginConfigPath := getPluginConfigPath(pluginName)
|
pluginConfigPath := getPluginConfigPath(pluginName)
|
||||||
os.RemoveAll(pluginConfigPath)
|
return os.RemoveAll(pluginConfigPath)
|
||||||
} else {
|
|
||||||
pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName)
|
|
||||||
os.RemoveAll(pluginAppConfigRoot)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName)
|
||||||
|
return os.RemoveAll(pluginAppConfigRoot)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PropertyExists returns whether a property exists or not
|
// PropertyExists returns whether a property exists or not
|
||||||
@@ -106,21 +112,22 @@ func PropertyTouch(pluginName string, appName string, property string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PropertyWrite writes a value for a given application property
|
// PropertyWrite writes a value for a given application property
|
||||||
func PropertyWrite(pluginName string, appName string, property string, value string) {
|
func PropertyWrite(pluginName string, appName string, property string, value string) error {
|
||||||
if err := makePropertyPath(pluginName, appName); err != nil {
|
if err := PropertyTouch(pluginName, appName, property); err != nil {
|
||||||
LogFail(fmt.Sprintf("Unable to create %s config directory for %s: %s", pluginName, appName, err.Error()))
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
propertyPath := getPropertyPath(pluginName, appName, property)
|
propertyPath := getPropertyPath(pluginName, appName, 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()))
|
return errors.New(fmt.Sprintf("Unable to write %s config value %s.%s: %s", pluginName, appName, property, err.Error()))
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
fmt.Fprintf(file, value)
|
fmt.Fprintf(file, value)
|
||||||
file.Chmod(0600)
|
file.Chmod(0600)
|
||||||
setPermissions(propertyPath, 0600)
|
setPermissions(propertyPath, 0600)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PropertySetup creates the plugin config root
|
// PropertySetup creates the plugin config root
|
||||||
|
|||||||
@@ -23,10 +23,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
if proxy.IsAppProxyEnabled(appName) {
|
if proxy.IsAppProxyEnabled(appName) {
|
||||||
common.LogVerboseQuiet("Setting %s network property 'bind-all-interfaces' to false")
|
common.LogVerboseQuiet("Setting %s network property 'bind-all-interfaces' to false")
|
||||||
common.PropertyWrite("network", appName, "bind-all-interfaces", "false")
|
if err := common.PropertyWrite("network", appName, "bind-all-interfaces", "false"); err != nil {
|
||||||
|
common.LogWarn(err.Error())
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
common.LogVerboseQuiet("Setting %s network property 'bind-all-interfaces' to true")
|
common.LogVerboseQuiet("Setting %s network property 'bind-all-interfaces' to true")
|
||||||
common.PropertyWrite("network", appName, "bind-all-interfaces", "true")
|
if err := common.PropertyWrite("network", appName, "bind-all-interfaces", "true"); err != nil {
|
||||||
|
common.LogWarn(err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,5 +11,8 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
appName := flag.Arg(0)
|
appName := flag.Arg(0)
|
||||||
|
|
||||||
common.PropertyWrite("network", appName, "bind-all-interfaces", "false")
|
err := common.PropertyWrite("network", appName, "bind-all-interfaces", "false")
|
||||||
|
if err != nil {
|
||||||
|
common.LogWarn(err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -11,5 +11,8 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
appName := flag.Arg(0)
|
appName := flag.Arg(0)
|
||||||
|
|
||||||
common.PropertyDestroy("network", appName)
|
err := common.PropertyDestroy("network", appName)
|
||||||
|
if err != nil {
|
||||||
|
common.LogFail(err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user