package recipe import ( "errors" "fmt" "coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/pkg/catalogue" "github.com/AlecAivazis/survey/v2" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) var recipeSyncCommand = &cli.Command{ Name: "sync", Usage: "Ensure recipe version labels are up-to-date", Aliases: []string{"s"}, ArgsUsage: " ", Description: ` This command will generate labels for the main recipe service (i.e. by convention, typically the service named "app") which corresponds to the following format: coop-cloud.${STACK_NAME}.version= The is determined by the recipe maintainer and is specified on the command-line. The configuration will be updated on the local file system. `, BashComplete: func(c *cli.Context) { catl, err := catalogue.ReadRecipeCatalogue() if err != nil { logrus.Warn(err) } if c.NArg() > 0 { return } for name := range catl { fmt.Println(name) } }, Action: func(c *cli.Context) error { if c.Args().Len() != 2 { internal.ShowSubcommandHelpAndError(c, errors.New("missing / arguments?")) } recipe := internal.ValidateRecipe(c) // TODO: validate with tagcmp when new commits come in // See https://git.coopcloud.tech/coop-cloud/abra/pulls/109 nextTag := c.Args().Get(1) mainService := "app" var services []string hasAppService := false for _, service := range recipe.Config.Services { services = append(services, service.Name) if service.Name == "app" { hasAppService = true logrus.Debugf("detected app service in '%s'", recipe.Name) } } if !hasAppService { logrus.Warnf("no 'app' service defined in '%s'", recipe.Name) var chosenService string prompt := &survey.Select{ Message: fmt.Sprintf("what is the main service name for '%s'?", recipe.Name), Options: services, } if err := survey.AskOne(prompt, &chosenService); err != nil { logrus.Fatal(err) } mainService = chosenService } logrus.Debugf("selecting '%s' as the service to sync version labels", mainService) label := fmt.Sprintf("coop-cloud.${STACK_NAME}.version=%s", nextTag) if err := recipe.UpdateLabel(mainService, label); err != nil { logrus.Fatal(err) } logrus.Infof("synced label '%s' to service '%s'", label, mainService) return nil }, }