Files
dokku/plugins/resource/resource.go
Jose Diaz-Gonzalez 3bf6c72451 fix: align Go-plugin :report JSON keys with bash-strip convention
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.
2026-05-27 07:17:00 -04:00

80 lines
2.1 KiB
Go

package resource
import (
"fmt"
"github.com/dokku/dokku/plugins/common"
)
// Resource is a collection of resource constraints for apps
type Resource struct {
CPU string `json:"cpu"`
Memory string `json:"memory"`
MemorySwap string `json:"memory-swap"`
Network string `json:"network"`
NetworkIngress string `json:"network-ingress"`
NetworkEgress string `json:"network-egress"`
NvidiaGPU string `json:"nvidia-gpu"`
}
// ReportSingleApp is an internal function that displays the resource report for one or more apps
func ReportSingleApp(appName, format, infoFlag string) error {
if appName != "--global" {
if err := common.VerifyAppName(appName); err != nil {
return err
}
}
resources, err := common.PropertyGetAll("resource", appName)
if err != nil {
return nil
}
flags := map[string]string{}
for key, value := range resources {
flag := fmt.Sprintf("--resource-%v", key)
flags[flag] = value
}
flagKeys := []string{}
for flagKey := range flags {
flagKeys = append(flagKeys, flagKey)
}
return common.ReportSingleApp(common.ReportSingleAppInput{
ReportType: "resource",
AppName: appName,
InfoFlag: infoFlag,
InfoFlags: flags,
InfoFlagKeys: flagKeys,
Format: format,
TrimPrefix: true,
UppercaseFirstCharacter: false,
EmitLegacyPrefix: true,
})
}
// GetResourceValue fetches a single value for a given app/process/request/key combination
func GetResourceValue(appName string, processType string, resourceType string, key string) (string, error) {
resources, err := common.PropertyGetAll("resource", appName)
if err != nil {
return "", err
}
defaultValue := ""
for k, value := range resources {
if k == propertyKey("_default_", resourceType, key) {
defaultValue = value
}
if k == propertyKey(processType, resourceType, key) {
return value, nil
}
}
return defaultValue, nil
}
func propertyKey(processType string, resourceType string, key string) string {
return fmt.Sprintf("%v.%v.%v", processType, resourceType, key)
}