From 152c5d4563e2d27da296fa05f6e1fae027a7d9e6 Mon Sep 17 00:00:00 2001 From: Cassowary Rusnov Date: Thu, 13 Apr 2023 09:28:34 -0700 Subject: [PATCH] Add machine output for recipe/upgrade - Normal faff related to calling external libraries with structs thnx go - Ouputs json now --- cli/recipe/upgrade.go | 47 ++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/cli/recipe/upgrade.go b/cli/recipe/upgrade.go index e1c105d0..9c1dd50b 100644 --- a/cli/recipe/upgrade.go +++ b/cli/recipe/upgrade.go @@ -2,6 +2,7 @@ package recipe import ( "bufio" + "encoding/json" "fmt" "os" "path" @@ -27,11 +28,11 @@ type imgPin struct { version tagcmp.Tag } -type upgradeList struct { - service string - image string - tag string - upgradeTags []string +type anUpgrade struct { + Service string `json:"service"` + Image string `json:"image"` + Tag string `json:"tag"` + UpgradeTags []string `json:"upgrades"` } var recipeUpgradeCommand = cli.Command{ @@ -63,6 +64,7 @@ You may invoke this command in "wizard" mode and be prompted for input: internal.PatchFlag, internal.MinorFlag, internal.MajorFlag, + internal.MachineReadableFlag, internal.AllTagsFlag, }, Before: internal.SubCommandBefore, @@ -82,7 +84,7 @@ You may invoke this command in "wizard" mode and be prompted for input: } } - var upgrades []upgradeList + upgradeList := make(map[string]anUpgrade) // check for versions file and load pinned versions versionsPresent := false @@ -250,21 +252,21 @@ You may invoke this command in "wizard" mode and be prompted for input: } } - upgrade := upgradeList{ - service: service.Name, - image: image, - tag: tag.String(), - upgradeTags: make([]string, len(compatibleStrings[1:])), + upgrade := anUpgrade{ + Service: service.Name, + Image: image, + Tag: tag.String(), + UpgradeTags: make([]string, len(compatibleStrings[1:])), } for n, s := range compatibleStrings[1:] { var sb strings.Builder if _, err := sb.WriteString(s); err != nil { } - upgrade.upgradeTags[n] = sb.String() + upgrade.UpgradeTags[n] = sb.String() } - upgrades = append(upgrades, upgrade) + upgradeList[upgrade.Service] = upgrade if internal.NoInput { upgradeTag = "skip" @@ -297,11 +299,20 @@ You may invoke this command in "wizard" mode and be prompted for input: } if internal.NoInput { - // FIXME handle machine-readable here - for _, upgrade := range upgrades { - logrus.Infof("can upgrade service: %s, image: %s, tag: %s ::\n", upgrade.service, upgrade.image, upgrade.tag) - for _, utag := range upgrade.upgradeTags { - logrus.Infof(" %s\n", utag) + if internal.MachineReadable { + jsonstring, err := json.Marshal(upgradeList) + if err != nil { + logrus.Fatal(err) + } else { + fmt.Println(string(jsonstring)) + } + + } else { + for _, upgrade := range upgradeList { + logrus.Infof("can upgrade service: %s, image: %s, tag: %s ::\n", upgrade.Service, upgrade.Image, upgrade.Tag) + for _, utag := range upgrade.UpgradeTags { + logrus.Infof(" %s\n", utag) + } } } }