From 7b1a6dd4d7178942aade7ad7875158df0d57d631 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Mon, 9 Aug 2021 16:17:40 +0200 Subject: [PATCH] WIP first run at the upgrade command --- cli/recipe/recipe.go | 69 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/cli/recipe/recipe.go b/cli/recipe/recipe.go index bd854abc..cbafdde6 100644 --- a/cli/recipe/recipe.go +++ b/cli/recipe/recipe.go @@ -12,8 +12,11 @@ import ( "coopcloud.tech/abra/catalogue" "coopcloud.tech/abra/cli/formatter" "coopcloud.tech/abra/cli/internal" + "coopcloud.tech/abra/client" "coopcloud.tech/abra/config" + "coopcloud.tech/abra/tagcmp" + "github.com/AlecAivazis/survey/v2" "github.com/docker/distribution/reference" "github.com/go-git/go-git/v5" "github.com/sirupsen/logrus" @@ -155,7 +158,71 @@ var recipeUpgradeCommand = &cli.Command{ if recipe == "" { internal.ShowSubcommandHelpAndError(c, errors.New("no recipe provided")) } - // TODO: part 1 of https://git.coopcloud.tech/coop-cloud/go-abra/issues/39#issuecomment-8066 + + compose, err := config.GetAppComposeFiles(recipe) + if err != nil { + logrus.Fatal(err) + } + + for _, service := range compose.Services { + var compatible []tagcmp.Tag + + // TODO: use to filter registry versions + // catlVersions, err := catalogue.VersionsOfService(recipe, service.Name) + // if err != nil { + // logrus.Fatal(err) + // } + + img, err := reference.ParseNormalizedNamed(service.Image) + if err != nil { + logrus.Fatal(err) + } + + image := reference.Path(img.(reference.Named)) + regVersions, err := client.GetRegistryTags(image) + if err != nil { + logrus.Fatal(err) + } + + tag, err := tagcmp.Parse(img.(reference.NamedTagged).Tag()) + if err != nil { + logrus.Fatal(err) + } + + for _, regVersion := range regVersions { + other, err := tagcmp.Parse(regVersion.Name) + if err != nil { + continue + } + + if tag.IsCompatible(other) && tag.IsLessThan(other) && !tag.Equals(other) { + compatible = append(compatible, other) + } + } + + sort.Sort(tagcmp.ByTag(compatible)) + + if len(compatible) == 0 { + logrus.Info(fmt.Sprintf("No new versions available for '%s', '%s' is the latest", image, tag)) + break + } + + var compatibleStrings []string + for _, compat := range compatible { + compatibleStrings = append(compatibleStrings, compat.String()) + } + + var upgradeTag string + msg := fmt.Sprintf("Which tag would you like to upgrade to? (service: %s, tag: %s)", service.Name, tag) + prompt := &survey.Select{ + Message: msg, + Options: compatibleStrings, + } + if err := survey.AskOne(prompt, &upgradeTag); err != nil { + logrus.Fatal(err) + } + } + return nil }, }