From c62bff300209897755f2bcc0430909e44bd518af Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 1 Aug 2021 01:21:46 -0400 Subject: [PATCH] refactor: hide dokku scale file parsing inside ps plugin --- plugins/ps/functions.go | 27 ++++++++++++++++++++++----- plugins/ps/ps.go | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/plugins/ps/functions.go b/plugins/ps/functions.go index a03632466..f2d91baae 100644 --- a/plugins/ps/functions.go +++ b/plugins/ps/functions.go @@ -306,6 +306,24 @@ func readScaleFile(appName string) (map[string]int, error) { return scale, nil } +func getFormations(appName string) (FormationSlice, error) { + scale := FormationSlice{} + data, err := readScaleFile(appName) + if err != nil { + return scale, err + } + + for processType, quantity := range data { + f := Formation{ + ProcessType: processType, + Quantity: quantity, + } + scale = append(scale, &f) + } + + return scale, nil +} + func removeProcfile(appName string) error { procfile := getProcfilePath(appName) if !common.FileExists(procfile) { @@ -332,8 +350,7 @@ func restorePrep() error { } func scaleReport(appName string) error { - scalefilePath := getScalefilePath(appName) - lines, err := common.FileToSlice(scalefilePath) + formations, err := getFormations(appName) if err != nil { return err } @@ -350,9 +367,9 @@ func scaleReport(appName string) error { content = append(content, "proctype=qty", "--------=---") } - sort.Strings(lines) - for _, line := range lines { - content = append(content, line) + sort.Sort(formations) + for _, formation := range formations { + content = append(content, fmt.Sprintf("%s=%d", formation.ProcessType, formation.Quantity)) } for _, line := range content { diff --git a/plugins/ps/ps.go b/plugins/ps/ps.go index 10699d9e5..9d524c109 100644 --- a/plugins/ps/ps.go +++ b/plugins/ps/ps.go @@ -24,6 +24,25 @@ var ( } ) +type FormationSlice []*Formation + +type Formation struct { + ProcessType string `json:"process_type"` + Quantity int `json:"quantity"` +} + +func (d FormationSlice) Len() int { + return len(d) +} + +func (d FormationSlice) Swap(i, j int) { + d[i], d[j] = d[j], d[i] +} + +func (d FormationSlice) Less(i, j int) bool { + return d[i].ProcessType < d[j].ProcessType +} + // Rebuild rebuilds app from base image func Rebuild(appName string) error { return common.PlugnTrigger("receive-app", []string{appName}...)