recipe/upgrade: Refactor upgradability list to make output easier

For future, we can print the struct as JSON.
This commit is contained in:
Cassowary 2023-04-12 15:25:48 -07:00 committed by Gitea
parent 62f8103fc2
commit 34b274bc52
1 changed files with 39 additions and 5 deletions

View File

@ -27,6 +27,13 @@ type imgPin struct {
version tagcmp.Tag version tagcmp.Tag
} }
type upgradeList struct {
service string
image string
tag string
upgradeTags []string
}
var recipeUpgradeCommand = cli.Command{ var recipeUpgradeCommand = cli.Command{
Name: "upgrade", Name: "upgrade",
Aliases: []string{"u"}, 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 // check for versions file and load pinned versions
versionsPresent := false versionsPresent := false
recipeDir := path.Join(config.RECIPES_DIR, recipe.Name) 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) compatibleStrings = append(compatibleStrings, regVersion)
} }
} }
if internal.NoInput {
logrus.Infof("potential upgrades for service: %s, image: %s, tag: %s ::\t", service.Name, image, tag) upgrade := upgradeList{
for _, upgradableVersion := range compatibleStrings[1:] { service: service.Name,
logrus.Infof("\t%s\n", upgradableVersion) 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 { } else {
prompt := &survey.Select{ prompt := &survey.Select{
Message: msg, 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) logrus.Infof("tag upgraded from %s to %s for %s", tag.String(), upgradeTag, image)
} }
} else { } 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 return nil
}, },
} }