feat(bonfire): add FlagBool and read every bare switch with it (IN-04, quick-261028-rqd)

This commit is contained in:
Jakub Zych
2026-10-28 20:32:38 +01:00
parent e34d113c80
commit 795c4f6b29
15 changed files with 86 additions and 54 deletions

View File

@@ -29,26 +29,13 @@ var (
// forceDescription describes the --force flag of the destructive commands.
const forceDescription = "Skip the confirmation asked in production on a terminal"
// flagOn reports whether the bare flag name is set; a nil Input has none.
func flagOn(in bonfire.Input, name string) bool {
if in == nil {
return false
}
v, ok := in.Flag(name)
if !ok {
return false
}
on, _ := strconv.ParseBool(v)
return on
}
// confirmProduction asks question before a destructive step when the
// environment is production and out is an interactive terminal, unless
// --force is set (D-28). Off a terminal, under -n or -q and in
// bonfire.Call it never asks, so deploys over SSH proceed as before. A
// "no" prints cancelled and returns a Reported error (exit 1).
func confirmProduction(app *backpack.App, in bonfire.Input, out bonfire.Output, question, cancelled string) error {
if flagOn(in, "force") || app == nil || app.Config == nil || app.Config.Environment() != "production" {
if bonfire.FlagBool(in, "force") || app == nil || app.Config == nil || app.Config.Environment() != "production" {
return nil
}
ok, err := bonfire.ConfirmProceed(out, question)
@@ -156,7 +143,7 @@ asks.`,
if err != nil {
return err
}
if flagOn(in, "pretend") {
if bonfire.FlagBool(in, "pretend") {
for _, ref := range pending {
out.Println(ref.Plugin + ": " + ref.ID)
}
@@ -242,7 +229,7 @@ question.`,
if err != nil {
return err
}
if flagOn(in, "pretend") {
if bonfire.FlagBool(in, "pretend") {
if id == "" {
out.Info(fmt.Sprintf("%s has no applied migration to roll back", plugin))
return nil
@@ -283,7 +270,7 @@ sets with its table and whether it is applied, for scripts.`,
if err != nil {
return err
}
if flagOn(in, "json") {
if bonfire.FlagBool(in, "json") {
pending, err := PendingMigrations(gdb, plugins)
if err != nil {
return err
@@ -338,7 +325,7 @@ asks first; --force skips the question.`,
}
before := time.Now().Add(-time.Duration(days) * 24 * time.Hour)
return withDB(ctx, app, func(gdb *gorm.DB) error {
if flagOn(in, "pretend") {
if bonfire.FlagBool(in, "pretend") {
var n int64
if err := gdb.WithContext(ctx).Model(&DeferredBinding{}).Where("created_at < ?", before).Count(&n).Error; err != nil {
return fmt.Errorf("lagoon: count deferred bindings: %w", err)

View File

@@ -43,7 +43,7 @@ first. With -q only the key is printed; --json prints {"key": "..."}.`,
return err
}
switch {
case flagOn(in, "json"):
case bonfire.FlagBool(in, "json"):
return printJSON(out, map[string]string{"key": key})
case bonfire.IsQuiet(out):
out.Println(key)