style tweaks
This commit is contained in:
@@ -2,7 +2,6 @@ package common
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@@ -46,8 +45,7 @@ func (sc *ShellCmd) Execute() bool {
|
||||
sc.Command.Stdout = os.Stdout
|
||||
sc.Command.Stderr = os.Stderr
|
||||
}
|
||||
err := sc.Command.Run()
|
||||
if err != nil {
|
||||
if err := sc.Command.Run(); err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@@ -136,27 +134,25 @@ func DirectoryExists(filePath string) bool {
|
||||
}
|
||||
|
||||
// DockerInspect runs an inspect command with a given format against a container id
|
||||
func DockerInspect(containerID, format string) (string, error) {
|
||||
func DockerInspect(containerID, format string) (output string, err error) {
|
||||
b, err := sh.Command("docker", "inspect", "--format", format, containerID).Output()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
output := strings.TrimSpace(string(b[:]))
|
||||
output = strings.TrimSpace(string(b[:]))
|
||||
if strings.HasPrefix(output, "'") && strings.HasSuffix(output, "'") {
|
||||
output = strings.TrimSuffix(strings.TrimPrefix(output, "'"), "'")
|
||||
}
|
||||
return output, err
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DokkuApps returns a list of all local apps
|
||||
func DokkuApps() ([]string, error) {
|
||||
var apps []string
|
||||
|
||||
func DokkuApps() (apps []string, err error) {
|
||||
dokkuRoot := MustGetEnv("DOKKU_ROOT")
|
||||
files, err := ioutil.ReadDir(dokkuRoot)
|
||||
if err != nil {
|
||||
return apps, errors.New("You haven't deployed any applications yet")
|
||||
err = fmt.Errorf("You haven't deployed any applications yet")
|
||||
return
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
@@ -171,18 +167,18 @@ func DokkuApps() ([]string, error) {
|
||||
}
|
||||
|
||||
if len(apps) == 0 {
|
||||
return apps, errors.New("You haven't deployed any applications yet")
|
||||
err = fmt.Errorf("You haven't deployed any applications yet")
|
||||
return
|
||||
}
|
||||
|
||||
return apps, nil
|
||||
return
|
||||
}
|
||||
|
||||
// FileToSlice reads in all the lines from a file into a string slice
|
||||
func FileToSlice(filePath string) ([]string, error) {
|
||||
var lines []string
|
||||
func FileToSlice(filePath string) (lines []string, err error) {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return lines, err
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
@@ -195,7 +191,7 @@ func FileToSlice(filePath string) ([]string, error) {
|
||||
lines = append(lines, text)
|
||||
}
|
||||
err = scanner.Err()
|
||||
return lines, err
|
||||
return
|
||||
}
|
||||
|
||||
// FileExists returns if a path exists and is a file
|
||||
@@ -244,7 +240,6 @@ func IsDeployed(appName string) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -272,41 +267,40 @@ func IsImageHerokuishBased(image string) bool {
|
||||
}
|
||||
|
||||
// MustGetEnv returns env variable or fails if it's not set
|
||||
func MustGetEnv(key string) string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
func MustGetEnv(key string) (val string) {
|
||||
val = os.Getenv(key)
|
||||
if val == "" {
|
||||
LogFail(fmt.Sprintf("%s not set!", key))
|
||||
}
|
||||
return value
|
||||
return
|
||||
}
|
||||
|
||||
// ReadFirstLine gets the first line of a file that has contents and returns it
|
||||
// if there are no contents, an empty string is returned
|
||||
// will also return an empty string if the file does not exist
|
||||
func ReadFirstLine(filename string) string {
|
||||
func ReadFirstLine(filename string) (text string) {
|
||||
if !FileExists(filename) {
|
||||
return ""
|
||||
return
|
||||
}
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return ""
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
text := strings.TrimSpace(scanner.Text())
|
||||
if text == "" {
|
||||
if text = strings.TrimSpace(scanner.Text()); text == "" {
|
||||
continue
|
||||
}
|
||||
return text
|
||||
return
|
||||
}
|
||||
return ""
|
||||
return
|
||||
}
|
||||
|
||||
// StripInlineComments removes bash-style comment from input line
|
||||
func StripInlineComments(text string) string {
|
||||
var bytes = []byte(text)
|
||||
bytes := []byte(text)
|
||||
re := regexp.MustCompile("(?s)#.*")
|
||||
bytes = re.ReplaceAll(bytes, nil)
|
||||
return strings.TrimSpace(string(bytes))
|
||||
|
||||
@@ -11,18 +11,16 @@ import (
|
||||
)
|
||||
|
||||
// CommandPropertySet is a generic function that will set a property for a given plugin/app combination
|
||||
func CommandPropertySet(pluginName string, appName string, property string, value string, validProperties map[string]bool) {
|
||||
err := VerifyAppName(appName)
|
||||
if err != nil {
|
||||
func CommandPropertySet(pluginName, appName, property, value string, properties map[string]string) {
|
||||
if err := VerifyAppName(appName); err != nil {
|
||||
LogFail(err.Error())
|
||||
}
|
||||
|
||||
if property == "" {
|
||||
LogFail("No property specified")
|
||||
}
|
||||
|
||||
if !isValidProperty(validProperties, property) {
|
||||
properties := reflect.ValueOf(validProperties).MapKeys()
|
||||
if _, ok := properties[property]; !ok {
|
||||
properties := reflect.ValueOf(properties).MapKeys()
|
||||
validPropertyList := make([]string, len(properties))
|
||||
for i := 0; i < len(properties); i++ {
|
||||
validPropertyList[i] = properties[i].String()
|
||||
@@ -44,8 +42,7 @@ func CommandPropertySet(pluginName string, appName string, property string, valu
|
||||
func PropertyDelete(pluginName string, appName string, property string) {
|
||||
pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName)
|
||||
propertyPath := strings.Join([]string{pluginAppConfigRoot, property}, "/")
|
||||
err := os.Remove(propertyPath)
|
||||
if err != nil {
|
||||
if err := os.Remove(propertyPath); err != nil {
|
||||
LogFail(fmt.Sprintf("Unable to remove %s property %s.%s", pluginName, appName, property))
|
||||
}
|
||||
}
|
||||
@@ -75,9 +72,9 @@ func PropertyGet(pluginName string, appName string, property string) string {
|
||||
}
|
||||
|
||||
// PropertyGetDefault returns the value for a given property with a specified default value
|
||||
func PropertyGetDefault(pluginName string, appName string, property string, defaultValue string) string {
|
||||
func PropertyGetDefault(pluginName, appName, property, defaultValue string) (val string) {
|
||||
if !PropertyExists(pluginName, appName, property) {
|
||||
return ""
|
||||
return
|
||||
}
|
||||
|
||||
pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName)
|
||||
@@ -86,16 +83,15 @@ func PropertyGetDefault(pluginName string, appName string, property string, defa
|
||||
b, err := ioutil.ReadFile(propertyPath)
|
||||
if err != nil {
|
||||
LogWarn(fmt.Sprintf("Unable to read %s property %s.%s", pluginName, appName, property))
|
||||
return ""
|
||||
return
|
||||
}
|
||||
|
||||
return string(b)
|
||||
val = string(b)
|
||||
return
|
||||
}
|
||||
|
||||
// PropertyWrite writes a value for a given application property
|
||||
func PropertyWrite(pluginName string, appName string, property string, value string) {
|
||||
err := makePropertyPath(pluginName, appName)
|
||||
if err != nil {
|
||||
if err := makePropertyPath(pluginName, appName); err != nil {
|
||||
LogFail(fmt.Sprintf("Unable to create %s config directory for %s: %s", pluginName, appName, err.Error()))
|
||||
}
|
||||
|
||||
@@ -103,7 +99,7 @@ func PropertyWrite(pluginName string, appName string, property string, value str
|
||||
propertyPath := strings.Join([]string{pluginAppConfigRoot, property}, "/")
|
||||
file, err := os.Create(propertyPath)
|
||||
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()))
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
@@ -113,20 +109,14 @@ func PropertyWrite(pluginName string, appName string, property string, value str
|
||||
}
|
||||
|
||||
// PropertySetup creates the plugin config root
|
||||
func PropertySetup(pluginName string) error {
|
||||
func PropertySetup(pluginName string) (err error) {
|
||||
pluginConfigRoot := getPluginConfigPath(pluginName)
|
||||
err := os.MkdirAll(pluginConfigRoot, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
if err = os.MkdirAll(pluginConfigRoot, 0755); err != nil {
|
||||
return
|
||||
}
|
||||
return setPermissions(pluginConfigRoot, 0755)
|
||||
}
|
||||
|
||||
// isValidProperty returns whether a property is a valid property or not
|
||||
func isValidProperty(validProperties map[string]bool, property string) bool {
|
||||
return validProperties[property]
|
||||
}
|
||||
|
||||
// getPluginAppPropertyPath returns the plugin property path for a given plugin/app combination
|
||||
func getPluginAppPropertyPath(pluginName string, appName string) string {
|
||||
return strings.Join([]string{getPluginConfigPath(pluginName), appName}, "/")
|
||||
@@ -138,44 +128,37 @@ func getPluginConfigPath(pluginName string) string {
|
||||
}
|
||||
|
||||
// makePropertyPath ensures that a property path exists
|
||||
func makePropertyPath(pluginName string, appName string) error {
|
||||
func makePropertyPath(pluginName string, appName string) (err error) {
|
||||
pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName)
|
||||
err := os.MkdirAll(pluginAppConfigRoot, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
if err = os.MkdirAll(pluginAppConfigRoot, 0755); err != nil {
|
||||
return
|
||||
}
|
||||
return setPermissions(pluginAppConfigRoot, 0755)
|
||||
}
|
||||
|
||||
// setPermissions sets the proper owner and filemode for a given file
|
||||
func setPermissions(path string, fileMode os.FileMode) error {
|
||||
err := os.Chmod(path, fileMode)
|
||||
if err != nil {
|
||||
func setPermissions(path string, fileMode os.FileMode) (err error) {
|
||||
if err = os.Chmod(path, fileMode); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
group, err := user.LookupGroup("dokku")
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
user, err := user.Lookup("dokku")
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
uid, err := strconv.Atoi(user.Uid)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
gid, err := strconv.Atoi(group.Gid)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
err = os.Chown(path, uid, gid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return os.Chown(path, uid, gid)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user