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:
1 parent
b3a802a965
commit
37c83e1c52
4 files changed
+67
-115
No files matched your search
@@ -1,9 +1,6 @@
|
|||||||
package buildpacks
|
package buildpacks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/dokku/dokku/plugins/common"
|
"github.com/dokku/dokku/plugins/common"
|
||||||
@@ -24,38 +21,7 @@ func ReportSingleApp(appName, infoFlag string) {
|
|||||||
"--buildpacks-list": strings.Join(buildpacks, ","),
|
"--buildpacks-list": strings.Join(buildpacks, ","),
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(infoFlag) == 0 {
|
trimPrefix := false
|
||||||
common.LogInfo2Quiet(fmt.Sprintf("%s buildpacks information", appName))
|
uppercaseFirstCharacter := true
|
||||||
for k, v := range infoFlags {
|
common.ReportSingleApp("buildpacks", appName, infoFlag, infoFlags, trimPrefix, uppercaseFirstCharacter)
|
||||||
key := common.UcFirst(strings.Replace(strings.TrimPrefix(k, "--"), "-", " ", -1))
|
|
||||||
common.LogVerbose(fmt.Sprintf("%s%s", Right(fmt.Sprintf("%s:", key), 31, " "), v))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range infoFlags {
|
|
||||||
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, ", ")))
|
|
||||||
}
|
|
||||||
|
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
@@ -350,6 +352,55 @@ func ReadFirstLine(filename string) (text string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReportSingleApp is an internal function that displays a report for an app
|
||||||
|
func ReportSingleApp(reportType string, appName string, infoFlag string, infoFlags map[string]string, trimPrefix bool, uppercaseFirstCharacter bool) {
|
||||||
|
flags := []string{}
|
||||||
|
for key, _ := range infoFlags {
|
||||||
|
flags = append(flags, key)
|
||||||
|
}
|
||||||
|
sort.Strings(flags)
|
||||||
|
|
||||||
|
if len(infoFlag) == 0 {
|
||||||
|
LogInfo2Quiet(fmt.Sprintf("%s %v information", appName, reportType))
|
||||||
|
for _, k := range flags {
|
||||||
|
v := infoFlags[k]
|
||||||
|
prefix := "--"
|
||||||
|
if trimPrefix {
|
||||||
|
prefix = fmt.Sprintf("--%v-", reportType)
|
||||||
|
}
|
||||||
|
|
||||||
|
key := strings.Replace(strings.Replace(strings.TrimPrefix(k, prefix), "-", " ", -1), ".", " ", -1)
|
||||||
|
|
||||||
|
if uppercaseFirstCharacter {
|
||||||
|
key = UcFirst(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
LogVerbose(fmt.Sprintf("%s%s", RightPad(fmt.Sprintf("%s:", key), 31, " "), v))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, k := range flags {
|
||||||
|
if infoFlag == k {
|
||||||
|
v := infoFlags[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()
|
||||||
|
}
|
||||||
|
LogFail(fmt.Sprintf("Invalid flag passed, valid flags: %s", strings.Join(strkeys, ", ")))
|
||||||
|
}
|
||||||
|
|
||||||
|
// RightPad right-pads the string with pad up to len runes
|
||||||
|
func RightPad(str string, length int, pad string) string {
|
||||||
|
return str + times(pad, length-len(str))
|
||||||
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
bytes := []byte(text)
|
bytes := []byte(text)
|
||||||
@@ -415,3 +466,10 @@ func PlugnTrigger(triggerName string, args ...string) error {
|
|||||||
}
|
}
|
||||||
return sh.Command("plugn", shellArgs...).Run()
|
return sh.Command("plugn", shellArgs...).Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func times(str string, n int) (out string) {
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
out += str
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -225,38 +224,7 @@ func ReportSingleApp(appName, infoFlag string) {
|
|||||||
"--network-listeners": strings.Join(GetListeners(appName), " "),
|
"--network-listeners": strings.Join(GetListeners(appName), " "),
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(infoFlag) == 0 {
|
trimPrefix := false
|
||||||
common.LogInfo2Quiet(fmt.Sprintf("%s network information", appName))
|
uppercaseFirstCharacter := true
|
||||||
for k, v := range infoFlags {
|
common.ReportSingleApp("network", appName, infoFlag, infoFlags, trimPrefix, uppercaseFirstCharacter)
|
||||||
key := common.UcFirst(strings.Replace(strings.TrimPrefix(k, "--"), "-", " ", -1))
|
|
||||||
common.LogVerbose(fmt.Sprintf("%s%s", Right(fmt.Sprintf("%s:", key), 31, " "), v))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range infoFlags {
|
|
||||||
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, ", ")))
|
|
||||||
}
|
|
||||||
|
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
@@ -3,10 +3,6 @@ package resource
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"reflect"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/dokku/dokku/plugins/common"
|
"github.com/dokku/dokku/plugins/common"
|
||||||
)
|
)
|
||||||
@@ -32,39 +28,15 @@ func ReportSingleApp(appName, infoFlag string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := []string{}
|
|
||||||
infoFlags := map[string]string{}
|
infoFlags := map[string]string{}
|
||||||
for key, value := range resources {
|
for key, value := range resources {
|
||||||
flag := fmt.Sprintf("--resource-%v", key)
|
flag := fmt.Sprintf("--resource-%v", key)
|
||||||
flags = append(flags, flag)
|
|
||||||
infoFlags[flag] = value
|
infoFlags[flag] = value
|
||||||
}
|
}
|
||||||
sort.Strings(flags)
|
|
||||||
|
|
||||||
if len(infoFlag) == 0 {
|
trimPrefix := true
|
||||||
common.LogInfo2Quiet(fmt.Sprintf("%s resource information", appName))
|
uppercaseFirstCharacter := false
|
||||||
for _, k := range flags {
|
common.ReportSingleApp("resource", appName, infoFlag, infoFlags, trimPrefix, uppercaseFirstCharacter)
|
||||||
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, ", ")))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetResourceValue fetches a single value for a given app/process/request/key combination
|
// 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
|
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 {
|
func propertyKey(processType string, resourceType string, key string) string {
|
||||||
return fmt.Sprintf("%v.%v.%v", processType, resourceType, key)
|
return fmt.Sprintf("%v.%v.%v", processType, resourceType, key)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user