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:
Jose Diaz-Gonzalez
2020-02-09 20:37:03 -05:00
parent b3a802a965
commit 37c83e1c52
4 changed files with 67 additions and 115 deletions

View File

@@ -1,9 +1,6 @@
package buildpacks
import (
"fmt"
"os"
"reflect"
"strings"
"github.com/dokku/dokku/plugins/common"
@@ -24,38 +21,7 @@ func ReportSingleApp(appName, infoFlag string) {
"--buildpacks-list": strings.Join(buildpacks, ","),
}
if len(infoFlag) == 0 {
common.LogInfo2Quiet(fmt.Sprintf("%s buildpacks information", appName))
for k, v := range infoFlags {
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))
trimPrefix := false
uppercaseFirstCharacter := true
common.ReportSingleApp("buildpacks", appName, infoFlag, infoFlags, trimPrefix, uppercaseFirstCharacter)
}