From 2a82936caa6f48f31bbfa4dbf6466e0466a666a8 Mon Sep 17 00:00:00 2001 From: Cassowary Rusnov Date: Wed, 12 Apr 2023 14:58:21 -0700 Subject: [PATCH 1/5] recipe/upgrade: Add non-interactive mode. Add support for -n which just outputs the list of compatible tags for each image. --- cli/recipe/upgrade.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/cli/recipe/upgrade.go b/cli/recipe/upgrade.go index dfbc849f..a3b39212 100644 --- a/cli/recipe/upgrade.go +++ b/cli/recipe/upgrade.go @@ -240,15 +240,21 @@ You may invoke this command in "wizard" mode and be prompted for input: compatibleStrings = append(compatibleStrings, regVersion) } } - - prompt := &survey.Select{ - Message: msg, - Help: "enter / return to confirm, choose 'skip' to not upgrade this tag, vim mode is enabled", - VimMode: true, - Options: compatibleStrings, - } - if err := survey.AskOne(prompt, &upgradeTag); err != nil { - logrus.Fatal(err) + if internal.NoInput { + logrus.Infof("potential upgrades for service: %s, image: %s, tag: %s ::\t", service.Name, image, tag) + for _, upgradableVersion := range compatibleStrings[1:] { + logrus.Infof("\t%s\n", upgradableVersion) + } + } else { + prompt := &survey.Select{ + Message: msg, + Help: "enter / return to confirm, choose 'skip' to not upgrade this tag, vim mode is enabled", + VimMode: true, + Options: compatibleStrings, + } + if err := survey.AskOne(prompt, &upgradeTag); err != nil { + logrus.Fatal(err) + } } } } -- 2.49.0 From fd7efa7daf177bf9843a1b0404ffbb334b3f1b28 Mon Sep 17 00:00:00 2001 From: Cassowary Rusnov Date: Wed, 12 Apr 2023 15:25:48 -0700 Subject: [PATCH 2/5] recipe/upgrade: Refactor upgradability list to make output easier For future, we can print the struct as JSON. --- cli/recipe/upgrade.go | 44 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/cli/recipe/upgrade.go b/cli/recipe/upgrade.go index a3b39212..e1c105d0 100644 --- a/cli/recipe/upgrade.go +++ b/cli/recipe/upgrade.go @@ -27,6 +27,13 @@ type imgPin struct { version tagcmp.Tag } +type upgradeList struct { + service string + image string + tag string + upgradeTags []string +} + var recipeUpgradeCommand = cli.Command{ Name: "upgrade", Aliases: []string{"u"}, @@ -75,6 +82,8 @@ You may invoke this command in "wizard" mode and be prompted for input: } } + var upgrades []upgradeList + // check for versions file and load pinned versions versionsPresent := false recipeDir := path.Join(config.RECIPES_DIR, recipe.Name) @@ -240,11 +249,25 @@ You may invoke this command in "wizard" mode and be prompted for input: compatibleStrings = append(compatibleStrings, regVersion) } } - if internal.NoInput { - logrus.Infof("potential upgrades for service: %s, image: %s, tag: %s ::\t", service.Name, image, tag) - for _, upgradableVersion := range compatibleStrings[1:] { - logrus.Infof("\t%s\n", upgradableVersion) + + upgrade := upgradeList{ + 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() + } + + upgrades = append(upgrades, upgrade) + + if internal.NoInput { + upgradeTag = "skip" } else { prompt := &survey.Select{ Message: msg, @@ -267,10 +290,21 @@ You may invoke this command in "wizard" mode and be prompted for input: logrus.Infof("tag upgraded from %s to %s for %s", tag.String(), upgradeTag, image) } } else { - logrus.Warnf("not upgrading %s, skipping as requested", image) + if !internal.NoInput { + logrus.Warnf("not upgrading %s, skipping as requested", image) + } } } + 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) + } + } + } return nil }, } -- 2.49.0 From 2fb490c8b3503d309f0bf048d74aa24c3a0eba39 Mon Sep 17 00:00:00 2001 From: Cassowary Rusnov Date: Thu, 13 Apr 2023 09:28:34 -0700 Subject: [PATCH 3/5] 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) + } } } } -- 2.49.0 From 196905526ab3be0bd6c3aad062980fc26e5018f4 Mon Sep 17 00:00:00 2001 From: Cassowary Rusnov Date: Thu, 13 Apr 2023 09:32:09 -0700 Subject: [PATCH 4/5] Make -m imply -n in recipe/upgrade --- cli/recipe/upgrade.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cli/recipe/upgrade.go b/cli/recipe/upgrade.go index 9c1dd50b..c70ef4f3 100644 --- a/cli/recipe/upgrade.go +++ b/cli/recipe/upgrade.go @@ -84,6 +84,11 @@ You may invoke this command in "wizard" mode and be prompted for input: } } + if internal.MachineReadable { + // -m implies -n in this case + internal.NoInput = true + } + upgradeList := make(map[string]anUpgrade) // check for versions file and load pinned versions -- 2.49.0 From 7c55325c13ca0ed677ec0cae0ecf9cf3eda70902 Mon Sep 17 00:00:00 2001 From: Cassowary Rusnov Date: Thu, 27 Apr 2023 09:43:47 -0700 Subject: [PATCH 5/5] Add some minor tweaks to machine readable pathway in recipe upgrade --- cli/recipe/upgrade.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/cli/recipe/upgrade.go b/cli/recipe/upgrade.go index c70ef4f3..a243858e 100644 --- a/cli/recipe/upgrade.go +++ b/cli/recipe/upgrade.go @@ -28,6 +28,8 @@ type imgPin struct { version tagcmp.Tag } +// anUpgrade represents a single service upgrade (as within a recipe), and the list of tags that it can be upgraded to, +// for serialization purposes. type anUpgrade struct { Service string `json:"service"` Image string `json:"image"` @@ -257,14 +259,17 @@ You may invoke this command in "wizard" mode and be prompted for input: } } + // there is always at least the item "skip" in compatibleStrings (a list of + // possible upgradable tags) and at least one other tag. + upgradableTags := compatibleStrings[1:] upgrade := anUpgrade{ Service: service.Name, Image: image, Tag: tag.String(), - UpgradeTags: make([]string, len(compatibleStrings[1:])), + UpgradeTags: make([]string, len(upgradableTags)), } - for n, s := range compatibleStrings[1:] { + for n, s := range upgradableTags { var sb strings.Builder if _, err := sb.WriteString(s); err != nil { } @@ -308,16 +313,16 @@ You may invoke this command in "wizard" mode and be prompted for input: 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) - } + fmt.Println(string(jsonstring)) + return nil + } + + 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) } } } -- 2.49.0