feat: make sync use wizard mode

Some bugs squashed while testing this extensively.
This commit is contained in:
2021-11-06 23:40:22 +01:00
parent a539033b55
commit 2b9395be1a
6 changed files with 312 additions and 170 deletions

View File

@ -29,15 +29,6 @@ var PushFlag = &cli.BoolFlag{
Destination: &Push,
}
var Dry bool
var DryFlag = &cli.BoolFlag{
Name: "dry-run",
Usage: "No changes are made, only reports changes that would be made",
Value: false,
Aliases: []string{"d"},
Destination: &Dry,
}
var CommitMessage string
var CommitMessageFlag = &cli.StringFlag{
Name: "commit-message",
@ -95,10 +86,10 @@ You may invoke this command in "wizard" mode and be prompted for input:
`,
Flags: []cli.Flag{
DryFlag,
MajorFlag,
MinorFlag,
PatchFlag,
internal.DryFlag,
internal.MajorFlag,
internal.MinorFlag,
internal.PatchFlag,
PushFlag,
CommitFlag,
CommitMessageFlag,
@ -108,7 +99,7 @@ You may invoke this command in "wizard" mode and be prompted for input:
recipe := internal.ValidateRecipeWithPrompt(c)
directory := path.Join(config.APPS_DIR, recipe.Name)
tagString := c.Args().Get(1)
mainApp := getMainApp(recipe)
mainApp := internal.GetMainApp(recipe)
imagesTmp, err := getImageVersions(recipe)
if err != nil {
@ -130,16 +121,16 @@ You may invoke this command in "wizard" mode and be prompted for input:
}
}
if (!Major && !Minor && !Patch) && tagString != "" {
if (!internal.Major && !internal.Minor && !internal.Patch) && tagString != "" {
logrus.Fatal("please specify <version> or bump type (--major/--minor/--patch)")
}
if (Major || Minor || Patch) && tagString != "" {
if (internal.Major || internal.Minor || internal.Patch) && tagString != "" {
logrus.Fatal("cannot specify tag and bump type at the same time")
}
// bumpType is used to decide what part of the tag should be incremented
bumpType := btoi(Major)*4 + btoi(Minor)*2 + btoi(Patch)
bumpType := btoi(internal.Major)*4 + btoi(internal.Minor)*2 + btoi(internal.Patch)
if bumpType != 0 {
// a bitwise check if the number is a power of 2
if (bumpType & (bumpType - 1)) != 0 {
@ -147,29 +138,14 @@ You may invoke this command in "wizard" mode and be prompted for input:
}
}
if (!Major && !Minor && !Patch) && tagString == "" {
fmt.Printf(`
semver cheat sheet (more via semver.org):
major: new features/bug fixes, backwards incompatible
minor: new features/bug fixes, backwards compatible
patch: bug fixes, backwards compatible
`)
var chosenBumpType string
prompt := &survey.Select{
Message: fmt.Sprintf("select recipe version increment type"),
Options: []string{"major", "minor", "patch"},
}
if err := survey.AskOne(prompt, &chosenBumpType); err != nil {
logrus.Fatal(err)
}
setBumpType(chosenBumpType)
if err := internal.PromptBumpType(tagString); err != nil {
logrus.Fatal(err)
}
if TagMessage == "" {
prompt := &survey.Input{
Message: "tag message",
Default: fmt.Sprintf("chore: publish new %s version", getBumpType()),
Default: fmt.Sprintf("chore: publish new %s version", internal.GetBumpType()),
}
if err := survey.AskOne(prompt, &TagMessage); err != nil {
logrus.Fatal(err)
@ -210,7 +186,7 @@ semver cheat sheet (more via semver.org):
if CommitMessage == "" {
prompt := &survey.Input{
Message: "commit message",
Default: fmt.Sprintf("chore: publish new %s version", getBumpType()),
Default: fmt.Sprintf("chore: publish new %s version", internal.GetBumpType()),
}
if err := survey.AskOne(prompt, &CommitMessage); err != nil {
logrus.Fatal(err)
@ -223,7 +199,7 @@ semver cheat sheet (more via semver.org):
}
logrus.Debug("staged compose.**yml for commit")
if !Dry {
if !internal.Dry {
_, err = commitWorktree.Commit(CommitMessage, &git.CommitOptions{})
if err != nil {
logrus.Fatal(err)
@ -257,7 +233,7 @@ semver cheat sheet (more via semver.org):
tag.MissingPatch = false
}
tagString = fmt.Sprintf("%s+%s", tag.String(), mainAppVersion)
if Dry {
if internal.Dry {
hash := abraFormatter.SmallSHA(head.Hash().String())
logrus.Info(fmt.Sprintf("dry run only: NOT creating tag %s at %s", tagString, hash))
return nil
@ -266,7 +242,7 @@ semver cheat sheet (more via semver.org):
repo.CreateTag(tagString, head.Hash(), &createTagOptions)
hash := abraFormatter.SmallSHA(head.Hash().String())
logrus.Info(fmt.Sprintf("created tag %s at %s", tagString, hash))
if Push && !Dry {
if Push && !internal.Dry {
if err := repo.Push(&git.PushOptions{}); err != nil {
logrus.Fatal(err)
}
@ -306,20 +282,20 @@ semver cheat sheet (more via semver.org):
newTag := lastGitTag
var newtagString string
if bumpType > 0 {
if Patch {
if internal.Patch {
now, err := strconv.Atoi(newTag.Patch)
if err != nil {
logrus.Fatal(err)
}
newTag.Patch = strconv.Itoa(now + 1)
} else if Minor {
} else if internal.Minor {
now, err := strconv.Atoi(newTag.Minor)
if err != nil {
logrus.Fatal(err)
}
newTag.Patch = "0"
newTag.Minor = strconv.Itoa(now + 1)
} else if Major {
} else if internal.Major {
now, err := strconv.Atoi(newTag.Major)
if err != nil {
logrus.Fatal(err)
@ -332,7 +308,7 @@ semver cheat sheet (more via semver.org):
newTag.Metadata = mainAppVersion
newtagString = newTag.String()
if Dry {
if internal.Dry {
hash := abraFormatter.SmallSHA(head.Hash().String())
logrus.Info(fmt.Sprintf("dry run only: NOT creating tag %s at %s", newtagString, hash))
return nil
@ -341,13 +317,13 @@ semver cheat sheet (more via semver.org):
repo.CreateTag(newtagString, head.Hash(), &createTagOptions)
hash := abraFormatter.SmallSHA(head.Hash().String())
logrus.Info(fmt.Sprintf("created tag %s at %s", newtagString, hash))
if Push && !Dry {
if Push && !internal.Dry {
if err := repo.Push(&git.PushOptions{}); err != nil {
logrus.Fatal(err)
}
logrus.Info(fmt.Sprintf("pushed tag %s to remote", newtagString))
} else {
logrus.Info("dry run only: NOT pushing changes")
logrus.Info("gry run only: NOT pushing changes")
}
return nil
@ -387,18 +363,6 @@ func getImageVersions(recipe recipe.Recipe) (map[string]string, error) {
return services, nil
}
// getMainApp retrieves the main 'app' image name
func getMainApp(recipe recipe.Recipe) string {
for _, service := range recipe.Config.Services {
name := service.Name
if name == "app" {
return strings.Split(service.Image, ":")[0]
}
}
return ""
}
// btoi converts a boolean value into an integer
func btoi(b bool) int {
if b {
@ -407,33 +371,3 @@ func btoi(b bool) int {
return 0
}
// getBumpType figures out which bump type is specified
func getBumpType() string {
var bumpType string
if Major {
bumpType = "major"
} else if Minor {
bumpType = "minor"
} else if Patch {
bumpType = "patch"
} else {
logrus.Fatal("no version bump type specififed?")
}
return bumpType
}
// setBumpType figures out which bump type is specified
func setBumpType(bumpType string) {
if bumpType == "major" {
Major = true
} else if bumpType == "minor" {
Minor = true
} else if bumpType == "patch" {
Patch = true
} else {
logrus.Fatal("no version bump type specififed?")
}
}