abra/cli/recipe/sync.go

92 lines
2.3 KiB
Go
Raw Normal View History

2021-09-05 20:33:07 +00:00
package recipe
import (
"fmt"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/catalogue"
"github.com/AlecAivazis/survey/v2"
2021-09-05 20:33:07 +00:00
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var recipeSyncCommand = &cli.Command{
Name: "sync",
Usage: "Ensure recipe version labels are up-to-date",
2021-09-05 20:33:07 +00:00
Aliases: []string{"s"},
Description: `
This command will generate labels for the main recipe service (i.e. the service
named "app", by convention) which corresponds to the following format:
2021-09-05 20:33:07 +00:00
coop-cloud.${STACK_NAME}.version=${RECIPE_TAG}
2021-09-05 20:33:07 +00:00
The ${RECIPE_TAG} is determined by the recipe maintainer and is retrieved by
this command by asking for the list of git tags on the local git repository.
The <recipe> configuration will be updated on the local file system.
2021-09-05 20:33:07 +00:00
`,
ArgsUsage: "<recipe>",
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)
}
},
2021-09-05 20:33:07 +00:00
Action: func(c *cli.Context) error {
recipe := internal.ValidateRecipe(c)
2021-09-05 20:33:07 +00:00
mainService := "app"
var services []string
2021-09-05 20:33:07 +00:00
hasAppService := false
for _, service := range recipe.Config.Services {
services = append(services, service.Name)
2021-09-05 20:33:07 +00:00
if service.Name == "app" {
hasAppService = true
logrus.Debugf("detected app service in '%s'", recipe.Name)
2021-09-05 20:33:07 +00:00
}
}
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,
2021-09-05 20:33:07 +00:00
}
if err := survey.AskOne(prompt, &chosenService); err != nil {
2021-09-05 20:33:07 +00:00
logrus.Fatal(err)
}
mainService = chosenService
}
2021-09-05 20:33:07 +00:00
logrus.Debugf("selecting '%s' as the service to sync version labels", mainService)
tags, err := recipe.Tags()
if err != nil {
logrus.Fatal(err)
}
if len(tags) == 0 {
logrus.Fatalf("no tags detected for '%s'", recipe.Name)
2021-09-05 20:33:07 +00:00
}
latestTag := tags[len(tags)-1]
logrus.Infof("choosing '%s' as latest tag for recipe '%s'", latestTag, recipe.Name)
label := fmt.Sprintf("coop-cloud.${STACK_NAME}.version=%s", latestTag)
if err := recipe.UpdateLabel(mainService, label); err != nil {
logrus.Fatal(err)
}
logrus.Infof("added label '%s' to service '%s'", label, mainService)
2021-09-05 20:33:07 +00:00
return nil
},
}