refactor: DRY up reports in golang
This unifies all the report-handling code so that plugins only need to worry about the data they wish to represent, and not the logic of actually returning it.
This commit is contained in:
@@ -3,10 +3,6 @@ package resource
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
)
|
||||
@@ -32,39 +28,15 @@ func ReportSingleApp(appName, infoFlag string) {
|
||||
return
|
||||
}
|
||||
|
||||
flags := []string{}
|
||||
infoFlags := map[string]string{}
|
||||
for key, value := range resources {
|
||||
flag := fmt.Sprintf("--resource-%v", key)
|
||||
flags = append(flags, flag)
|
||||
infoFlags[flag] = value
|
||||
}
|
||||
sort.Strings(flags)
|
||||
|
||||
if len(infoFlag) == 0 {
|
||||
common.LogInfo2Quiet(fmt.Sprintf("%s resource information", appName))
|
||||
for _, k := range flags {
|
||||
v := infoFlags[k]
|
||||
key := strings.Replace(strings.Replace(strings.TrimPrefix(k, "--resource-"), "-", " ", -1), ".", " ", -1)
|
||||
common.LogVerbose(fmt.Sprintf("%s%s", Right(fmt.Sprintf("%s:", key), 31, " "), v))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
for _, k := range flags {
|
||||
v := infoFlags[k]
|
||||
if infoFlag == k {
|
||||
fmt.Fprintln(os.Stdout, v)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
keys := reflect.ValueOf(infoFlags).MapKeys()
|
||||
strkeys := make([]string, len(keys))
|
||||
for i := 0; i < len(keys); i++ {
|
||||
strkeys[i] = keys[i].String()
|
||||
}
|
||||
common.LogFail(fmt.Sprintf("Invalid flag passed, valid flags: %s", strings.Join(strkeys, ", ")))
|
||||
trimPrefix := true
|
||||
uppercaseFirstCharacter := false
|
||||
common.ReportSingleApp("resource", appName, infoFlag, infoFlags, trimPrefix, uppercaseFirstCharacter)
|
||||
}
|
||||
|
||||
// GetResourceValue fetches a single value for a given app/process/request/key combination
|
||||
@@ -87,18 +59,6 @@ func GetResourceValue(appName string, processType string, resourceType string, k
|
||||
return defaultValue, nil
|
||||
}
|
||||
|
||||
func times(str string, n int) (out string) {
|
||||
for i := 0; i < n; i++ {
|
||||
out += str
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Right right-pads the string with pad up to len runes
|
||||
func Right(str string, length int, pad string) string {
|
||||
return str + times(pad, length-len(str))
|
||||
}
|
||||
|
||||
func propertyKey(processType string, resourceType string, key string) string {
|
||||
return fmt.Sprintf("%v.%v.%v", processType, resourceType, key)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user