style tweaks

This commit is contained in:
Michael Hobbs committed 2017-10-02 16:50:05 -07:00
1 parent b099d2c389
commit 8654783131
22 files changed
+101 -134

No files matched your search

+24 -30
View File
@@ -2,7 +2,6 @@ package common
import ( import (
"bufio" "bufio"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@@ -46,8 +45,7 @@ func (sc *ShellCmd) Execute() bool {
sc.Command.Stdout = os.Stdout sc.Command.Stdout = os.Stdout
sc.Command.Stderr = os.Stderr sc.Command.Stderr = os.Stderr
} }
err := sc.Command.Run() if err := sc.Command.Run(); err != nil {
if err != nil {
return false return false
} }
return true return true
@@ -136,27 +134,25 @@ func DirectoryExists(filePath string) bool {
} }
// DockerInspect runs an inspect command with a given format against a container id // 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() b, err := sh.Command("docker", "inspect", "--format", format, containerID).Output()
if err != nil { if err != nil {
return "", err return "", err
} }
output := strings.TrimSpace(string(b[:])) output = strings.TrimSpace(string(b[:]))
if strings.HasPrefix(output, "'") && strings.HasSuffix(output, "'") { if strings.HasPrefix(output, "'") && strings.HasSuffix(output, "'") {
output = strings.TrimSuffix(strings.TrimPrefix(output, "'"), "'") output = strings.TrimSuffix(strings.TrimPrefix(output, "'"), "'")
} }
return output, err return
} }
// DokkuApps returns a list of all local apps // DokkuApps returns a list of all local apps
func DokkuApps() ([]string, error) { func DokkuApps() (apps []string, err error) {
var apps []string
dokkuRoot := MustGetEnv("DOKKU_ROOT") dokkuRoot := MustGetEnv("DOKKU_ROOT")
files, err := ioutil.ReadDir(dokkuRoot) files, err := ioutil.ReadDir(dokkuRoot)
if err != nil { 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 { for _, f := range files {
@@ -171,18 +167,18 @@ func DokkuApps() ([]string, error) {
} }
if len(apps) == 0 { 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 // FileToSlice reads in all the lines from a file into a string slice
func FileToSlice(filePath string) ([]string, error) { func FileToSlice(filePath string) (lines []string, err error) {
var lines []string
f, err := os.Open(filePath) f, err := os.Open(filePath)
if err != nil { if err != nil {
return lines, err return
} }
defer f.Close() defer f.Close()
@@ -195,7 +191,7 @@ func FileToSlice(filePath string) ([]string, error) {
lines = append(lines, text) lines = append(lines, text)
} }
err = scanner.Err() err = scanner.Err()
return lines, err return
} }
// FileExists returns if a path exists and is a file // FileExists returns if a path exists and is a file
@@ -244,7 +240,6 @@ func IsDeployed(appName string) bool {
return true return true
} }
} }
return false return false
} }
@@ -272,41 +267,40 @@ func IsImageHerokuishBased(image string) bool {
} }
// MustGetEnv returns env variable or fails if it's not set // MustGetEnv returns env variable or fails if it's not set
func MustGetEnv(key string) string { func MustGetEnv(key string) (val string) {
value := os.Getenv(key) val = os.Getenv(key)
if value == "" { if val == "" {
LogFail(fmt.Sprintf("%s not set!", key)) LogFail(fmt.Sprintf("%s not set!", key))
} }
return value return
} }
// ReadFirstLine gets the first line of a file that has contents and returns it // ReadFirstLine gets the first line of a file that has contents and returns it
// if there are no contents, an empty string is returned // if there are no contents, an empty string is returned
// will also return an empty string if the file does not exist // 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) { if !FileExists(filename) {
return "" return
} }
f, err := os.Open(filename) f, err := os.Open(filename)
if err != nil { if err != nil {
return "" return
} }
defer f.Close() defer f.Close()
scanner := bufio.NewScanner(f) scanner := bufio.NewScanner(f)
for scanner.Scan() { for scanner.Scan() {
text := strings.TrimSpace(scanner.Text()) if text = strings.TrimSpace(scanner.Text()); text == "" {
if text == "" {
continue continue
} }
return text return
} }
return "" return
} }
// StripInlineComments removes bash-style comment from input line // StripInlineComments removes bash-style comment from input line
func StripInlineComments(text string) string { func StripInlineComments(text string) string {
var bytes = []byte(text) bytes := []byte(text)
re := regexp.MustCompile("(?s)#.*") re := regexp.MustCompile("(?s)#.*")
bytes = re.ReplaceAll(bytes, nil) bytes = re.ReplaceAll(bytes, nil)
return strings.TrimSpace(string(bytes)) return strings.TrimSpace(string(bytes))
+25 -42
View File
@@ -11,18 +11,16 @@ import (
) )
// CommandPropertySet is a generic function that will set a property for a given plugin/app combination // 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) { func CommandPropertySet(pluginName, appName, property, value string, properties map[string]string) {
err := VerifyAppName(appName) if err := VerifyAppName(appName); err != nil {
if err != nil {
LogFail(err.Error()) LogFail(err.Error())
} }
if property == "" { if property == "" {
LogFail("No property specified") LogFail("No property specified")
} }
if !isValidProperty(validProperties, property) { if _, ok := properties[property]; !ok {
properties := reflect.ValueOf(validProperties).MapKeys() properties := reflect.ValueOf(properties).MapKeys()
validPropertyList := make([]string, len(properties)) validPropertyList := make([]string, len(properties))
for i := 0; i < len(properties); i++ { for i := 0; i < len(properties); i++ {
validPropertyList[i] = properties[i].String() 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) { func PropertyDelete(pluginName string, appName string, property string) {
pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName) pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName)
propertyPath := strings.Join([]string{pluginAppConfigRoot, property}, "/") propertyPath := strings.Join([]string{pluginAppConfigRoot, property}, "/")
err := os.Remove(propertyPath) if err := os.Remove(propertyPath); err != nil {
if 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))
} }
} }
@@ -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 // 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) { if !PropertyExists(pluginName, appName, property) {
return "" return
} }
pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName) pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName)
@@ -86,16 +83,15 @@ func PropertyGetDefault(pluginName string, appName string, property string, defa
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))
return "" return
} }
val = string(b)
return string(b) return
} }
// 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) {
err := makePropertyPath(pluginName, appName) if err := makePropertyPath(pluginName, appName); err != nil {
if err != nil {
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()))
} }
@@ -103,7 +99,7 @@ func PropertyWrite(pluginName string, appName string, property string, value str
propertyPath := strings.Join([]string{pluginAppConfigRoot, 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()))
} }
defer file.Close() defer file.Close()
@@ -113,20 +109,14 @@ func PropertyWrite(pluginName string, appName string, property string, value str
} }
// PropertySetup creates the plugin config root // PropertySetup creates the plugin config root
func PropertySetup(pluginName string) error { func PropertySetup(pluginName string) (err error) {
pluginConfigRoot := getPluginConfigPath(pluginName) pluginConfigRoot := getPluginConfigPath(pluginName)
err := os.MkdirAll(pluginConfigRoot, 0755) if err = os.MkdirAll(pluginConfigRoot, 0755); err != nil {
if err != nil { return
return err
} }
return setPermissions(pluginConfigRoot, 0755) 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 // 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}, "/")
@@ -138,44 +128,37 @@ func getPluginConfigPath(pluginName string) string {
} }
// makePropertyPath ensures that a property path exists // 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) pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName)
err := os.MkdirAll(pluginAppConfigRoot, 0755) if err = os.MkdirAll(pluginAppConfigRoot, 0755); err != nil {
if err != nil { return
return err
} }
return setPermissions(pluginAppConfigRoot, 0755) return setPermissions(pluginAppConfigRoot, 0755)
} }
// setPermissions sets the proper owner and filemode for a given file // setPermissions sets the proper owner and filemode for a given file
func setPermissions(path string, fileMode os.FileMode) error { func setPermissions(path string, fileMode os.FileMode) (err error) {
err := os.Chmod(path, fileMode) if err = os.Chmod(path, fileMode); err != nil {
if err != nil {
return err return err
} }
group, err := user.LookupGroup("dokku") group, err := user.LookupGroup("dokku")
if err != nil { if err != nil {
return err return
} }
user, err := user.Lookup("dokku") user, err := user.Lookup("dokku")
if err != nil { if err != nil {
return err return
} }
uid, err := strconv.Atoi(user.Uid) uid, err := strconv.Atoi(user.Uid)
if err != nil { if err != nil {
return err return
} }
gid, err := strconv.Atoi(group.Gid) gid, err := strconv.Atoi(group.Gid)
if err != nil { if err != nil {
return err return
} }
return os.Chown(path, uid, gid)
err = os.Chown(path, uid, gid)
if err != nil {
return err
}
return nil
} }
+6 -7
View File
@@ -4,18 +4,18 @@ import (
"fmt" "fmt"
"strings" "strings"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
// GetWithDefault returns the value set for a given key, returning defaultValue if none found // GetWithDefault returns the value set for a given key, returning defaultValue if none found
func GetWithDefault(appName string, key string, defaultValue string) string { func GetWithDefault(appName string, key string, defaultValue string) (value string) {
value = defaultValue
envFile := strings.Join([]string{common.MustGetEnv("DOKKU_ROOT"), appName, "ENV"}, "/") envFile := strings.Join([]string{common.MustGetEnv("DOKKU_ROOT"), appName, "ENV"}, "/")
lines, err := common.FileToSlice(envFile) lines, err := common.FileToSlice(envFile)
if err != nil { if err != nil {
return defaultValue return
} }
value := defaultValue
prefix := fmt.Sprintf("export %v=", key) prefix := fmt.Sprintf("export %v=", key)
for _, line := range lines { for _, line := range lines {
if !strings.HasPrefix(line, prefix) { if !strings.HasPrefix(line, prefix) {
@@ -26,6 +26,5 @@ func GetWithDefault(appName string, key string, defaultValue string) string {
value = strings.TrimPrefix(strings.TrimSuffix(value, "'"), "'") value = strings.TrimPrefix(strings.TrimSuffix(value, "'"), "'")
} }
} }
return
return value
} }
+20 -28
View File
@@ -5,33 +5,27 @@ import (
"strconv" "strconv"
"strings" "strings"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
config "github.com/dokku/dokku/plugins/config" "github.com/dokku/dokku/plugins/config"
sh "github.com/codeskyblue/go-sh" sh "github.com/codeskyblue/go-sh"
) )
// ValidProperties is a map of all valid network properties var (
var ValidProperties = map[string]bool{ // DefaultProperties is a map of all valid network properties with corresponding default property values
"bind-all-interfaces": true, DefaultProperties = map[string]string{
} "bind-all-interfaces": "false",
}
// DefaultPropertyValues is a map of all valid network properties with corresponding default property values )
var DefaultPropertyValues = map[string]string{
"bind-all-interfaces": "false",
}
// BuildConfig builds network config files // BuildConfig builds network config files
func BuildConfig(appName string) { func BuildConfig(appName string) {
err := common.VerifyAppName(appName) if err := common.VerifyAppName(appName); err != nil {
if err != nil {
common.LogFail(err.Error()) common.LogFail(err.Error())
} }
if !common.IsDeployed(appName) { if !common.IsDeployed(appName) {
return return
} }
appRoot := strings.Join([]string{common.MustGetEnv("DOKKU_ROOT"), appName}, "/") appRoot := strings.Join([]string{common.MustGetEnv("DOKKU_ROOT"), appName}, "/")
scaleFile := strings.Join([]string{appRoot, "DOKKU_SCALE"}, "/") scaleFile := strings.Join([]string{appRoot, "DOKKU_SCALE"}, "/")
if !common.FileExists(scaleFile) { if !common.FileExists(scaleFile) {
@@ -40,7 +34,6 @@ func BuildConfig(appName string) {
image := common.GetAppImageName(appName, "", "") image := common.GetAppImageName(appName, "", "")
isHerokuishContainer := common.IsImageHerokuishBased(image) isHerokuishContainer := common.IsImageHerokuishBased(image)
common.LogInfo1(fmt.Sprintf("Ensuring network configuration is in sync for %s", appName)) common.LogInfo1(fmt.Sprintf("Ensuring network configuration is in sync for %s", appName))
lines, err := common.FileToSlice(scaleFile) lines, err := common.FileToSlice(scaleFile)
if err != nil { if err != nil {
@@ -93,9 +86,9 @@ func BuildConfig(appName string) {
} }
// GetContainerIpaddress returns the ipaddr for a given app container // GetContainerIpaddress returns the ipaddr for a given app container
func GetContainerIpaddress(appName string, procType string, containerID string) string { func GetContainerIpaddress(appName, procType, containerID string) (ipAddr string) {
if procType != "web" { if procType != "web" {
return "" return
} }
b, err := common.DockerInspect(containerID, "'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'") b, err := common.DockerInspect(containerID, "'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'")
@@ -108,18 +101,16 @@ func GetContainerIpaddress(appName string, procType string, containerID string)
return string(b[:]) return string(b[:])
} }
return "" return
} }
// GetContainerPort returns the port for a given app container // GetContainerPort returns the port for a given app container
func GetContainerPort(appName string, procType string, isHerokuishContainer bool, containerID string) string { func GetContainerPort(appName, procType string, isHerokuishContainer bool, containerID string) (port string) {
if procType != "web" { if procType != "web" {
return "" return
} }
dockerfilePorts := make([]string, 0) dockerfilePorts := make([]string, 0)
port := ""
if !isHerokuishContainer { if !isHerokuishContainer {
configValue := config.GetWithDefault(appName, "DOKKU_DOCKERFILE_PORTS", "") configValue := config.GetWithDefault(appName, "DOKKU_DOCKERFILE_PORTS", "")
if configValue != "" { if configValue != "" {
@@ -145,15 +136,16 @@ func GetContainerPort(appName string, procType string, isHerokuishContainer bool
port = "5000" port = "5000"
} }
return port return
} }
// GetDefaultValue returns the default value for a given property // GetDefaultValue returns the default value for a given property
func GetDefaultValue(property string) string { func GetDefaultValue(property string) (value string) {
if ValidProperties[property] { value, ok := DefaultProperties[property]
return DefaultPropertyValues[property] if ok {
return
} }
return "" return
} }
// HasNetworkConfig returns whether the network configuration for a given app exists // HasNetworkConfig returns whether the network configuration for a given app exists
@@ -3,7 +3,7 @@ package main
import ( import (
"flag" "flag"
network "github.com/dokku/dokku/plugins/network" "github.com/dokku/dokku/plugins/network"
) )
// rebuilds network settings for an app // rebuilds network settings for an app
@@ -1,8 +1,8 @@
package main package main
import ( import (
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
network "github.com/dokku/dokku/plugins/network" "github.com/dokku/dokku/plugins/network"
) )
// rebuilds network settings for all apps // rebuilds network settings for all apps
+3 -3
View File
@@ -3,8 +3,8 @@ package main
import ( import (
"flag" "flag"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
network "github.com/dokku/dokku/plugins/network" "github.com/dokku/dokku/plugins/network"
) )
// set or clear a network property for an app // set or clear a network property for an app
@@ -18,5 +18,5 @@ func main() {
value = "false" value = "false"
} }
common.CommandPropertySet("network", appName, property, value, network.ValidProperties) common.CommandPropertySet("network", appName, property, value, network.DefaultProperties)
} }
@@ -3,14 +3,13 @@ package main
import ( import (
"fmt" "fmt"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
proxy "github.com/dokku/dokku/plugins/proxy" "github.com/dokku/dokku/plugins/proxy"
) )
// runs the install step for the network plugin // runs the install step for the network plugin
func main() { func main() {
err := common.PropertySetup("network") if err := common.PropertySetup("network"); err != nil {
if err != nil {
common.LogFail(fmt.Sprintf("Unable to install the network plugin: %s", err.Error())) common.LogFail(fmt.Sprintf("Unable to install the network plugin: %s", err.Error()))
} }
@@ -3,7 +3,7 @@ package main
import ( import (
"flag" "flag"
network "github.com/dokku/dokku/plugins/network" "github.com/dokku/dokku/plugins/network"
) )
// rebuilds network settings for an app // rebuilds network settings for an app
@@ -6,8 +6,8 @@ import (
"os" "os"
"strings" "strings"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
config "github.com/dokku/dokku/plugins/config" "github.com/dokku/dokku/plugins/config"
) )
// computes the ports for a given app container // computes the ports for a given app container
@@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
network "github.com/dokku/dokku/plugins/network" "github.com/dokku/dokku/plugins/network"
) )
// write the ipaddress to stdout for a given app container // write the ipaddress to stdout for a given app container
@@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
network "github.com/dokku/dokku/plugins/network" "github.com/dokku/dokku/plugins/network"
) )
// write the ipaddress to stdout for a given app container // write the ipaddress to stdout for a given app container
@@ -1,13 +1,13 @@
package main package main
import ( import (
"path/filepath"
"flag" "flag"
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings" "strings"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
// returns the listeners (host:port combinations) for a given app container // returns the listeners (host:port combinations) for a given app container
@@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
network "github.com/dokku/dokku/plugins/network" network "github.com/dokku/dokku/plugins/network"
) )
@@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
network "github.com/dokku/dokku/plugins/network" network "github.com/dokku/dokku/plugins/network"
) )
@@ -6,7 +6,7 @@ import (
"os" "os"
"strings" "strings"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
// writes the ip to disk // writes the ip to disk
@@ -6,7 +6,7 @@ import (
"os" "os"
"strings" "strings"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
// writes the port to disk // writes the port to disk
@@ -3,7 +3,7 @@ package main
import ( import (
"flag" "flag"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
// set bind-all-interfaces to false by default // set bind-all-interfaces to false by default
@@ -3,7 +3,7 @@ package main
import ( import (
"flag" "flag"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
// write the port to stdout for a given app container // write the port to stdout for a given app container
+2 -2
View File
@@ -1,8 +1,8 @@
package proxy package proxy
import ( import (
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
config "github.com/dokku/dokku/plugins/config" "github.com/dokku/dokku/plugins/config"
) )
// IsAppProxyEnabled returns true if proxy is enabled; otherwise return false // IsAppProxyEnabled returns true if proxy is enabled; otherwise return false
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"flag" "flag"
"strings" "strings"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
// runs 'git gc --aggressive' against the application's repo // runs 'git gc --aggressive' against the application's repo
@@ -5,7 +5,7 @@ import (
"os" "os"
"strings" "strings"
common "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
// deletes the contents of the build cache stored in the repository // deletes the contents of the build cache stored in the repository