Every Go-implemented plugin's `:report --format json` now emits keys without the redundant `<plugin>-` head segment, matching the shape bash plugins have always emitted. The CLI flag names and `:set` semantics are unchanged. For backwards compatibility during the 0.38.x patch series, the legacy `<plugin>-<property>` keys are emitted side-by-side with the new keys, and a future major release will drop the legacy keys. `common.ReportSingleApp` is refactored to accept a `ReportSingleAppInput` struct with a `Validate()` method so the input is checked before any work runs, which also catches the latent `"docker options"` reportType bug at the API boundary.
141 lines
3.3 KiB
Go
141 lines
3.3 KiB
Go
package builds
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/dokku/dokku/plugins/common"
|
|
)
|
|
|
|
// ReportSingleApp emits the report for one app (or --global).
|
|
func ReportSingleApp(appName, format, infoFlag string) error {
|
|
if appName != "--global" {
|
|
if err := common.VerifyAppName(appName); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
var flags map[string]common.ReportFunc
|
|
if appName == "--global" {
|
|
flags = map[string]common.ReportFunc{
|
|
"--builds-global-retention": reportGlobalRetention,
|
|
}
|
|
} else {
|
|
flags = map[string]common.ReportFunc{
|
|
"--build-id": reportBuildID,
|
|
"--build-kind": reportBuildKind,
|
|
"--build-status": reportBuildStatus,
|
|
"--build-pid": reportBuildPID,
|
|
"--build-source": reportBuildSource,
|
|
"--build-started-at": reportBuildStartedAt,
|
|
"--build-finished-at": reportBuildFinishedAt,
|
|
"--build-exit-code": reportBuildExitCode,
|
|
"--builds-retention": reportRetention,
|
|
"--builds-global-retention": reportGlobalRetention,
|
|
"--builds-computed-retention": reportComputedRetention,
|
|
}
|
|
}
|
|
|
|
flagKeys := make([]string, 0, len(flags))
|
|
for k := range flags {
|
|
flagKeys = append(flagKeys, k)
|
|
}
|
|
|
|
infoFlags := common.CollectReport(appName, infoFlag, flags)
|
|
return common.ReportSingleApp(common.ReportSingleAppInput{
|
|
ReportType: "builds",
|
|
AppName: appName,
|
|
InfoFlag: infoFlag,
|
|
InfoFlags: infoFlags,
|
|
InfoFlagKeys: flagKeys,
|
|
Format: format,
|
|
TrimPrefix: true,
|
|
UppercaseFirstCharacter: true,
|
|
EmitLegacyPrefix: true,
|
|
})
|
|
}
|
|
|
|
func mostRecentBuild(appName string) (Build, bool) {
|
|
builds, err := FetchBuilds(appName)
|
|
if err != nil || len(builds) == 0 {
|
|
return Build{}, false
|
|
}
|
|
return builds[0], true
|
|
}
|
|
|
|
func reportBuildID(appName string) string {
|
|
b, ok := mostRecentBuild(appName)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return b.ID
|
|
}
|
|
|
|
func reportBuildKind(appName string) string {
|
|
b, ok := mostRecentBuild(appName)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return string(b.Kind)
|
|
}
|
|
|
|
func reportBuildStatus(appName string) string {
|
|
b, ok := mostRecentBuild(appName)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return string(b.DisplayStatus())
|
|
}
|
|
|
|
func reportBuildPID(appName string) string {
|
|
b, ok := mostRecentBuild(appName)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return strconv.Itoa(b.PID)
|
|
}
|
|
|
|
func reportBuildSource(appName string) string {
|
|
b, ok := mostRecentBuild(appName)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return string(b.Source)
|
|
}
|
|
|
|
func reportBuildStartedAt(appName string) string {
|
|
b, ok := mostRecentBuild(appName)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return b.StartedAt.Format(time.RFC3339)
|
|
}
|
|
|
|
func reportBuildFinishedAt(appName string) string {
|
|
b, ok := mostRecentBuild(appName)
|
|
if !ok || b.FinishedAt == nil {
|
|
return ""
|
|
}
|
|
return b.FinishedAt.Format(time.RFC3339)
|
|
}
|
|
|
|
func reportBuildExitCode(appName string) string {
|
|
b, ok := mostRecentBuild(appName)
|
|
if !ok || b.ExitCode == nil {
|
|
return ""
|
|
}
|
|
return strconv.Itoa(*b.ExitCode)
|
|
}
|
|
|
|
func reportRetention(appName string) string {
|
|
return common.PropertyGet("builds", appName, "retention")
|
|
}
|
|
|
|
func reportGlobalRetention(_ string) string {
|
|
return common.PropertyGet("builds", "--global", "retention")
|
|
}
|
|
|
|
func reportComputedRetention(appName string) string {
|
|
return strconv.Itoa(ResolveRetention(appName))
|
|
}
|