// Package console holds the sitemap plugin's commands: sitemap:import (the // PHP ImportCommand) and sitemap:import-winter (a copy of a WinterCMS // site's definition and settings). package console import ( "context" "fmt" "strings" "git.golem15.com/golem15/sm-sitemap-plugin/models" "git.golem15.com/golem15/summercms/modules/backpack" "git.golem15.com/golem15/summercms/modules/bonfire" "git.golem15.com/golem15/summercms/modules/lagoon" "gorm.io/gorm" ) func flagValue(in bonfire.Input, name string) string { v, ok := in.Flag(name) if !ok { return "" } return strings.TrimSpace(v) } func flagOn(in bonfire.Input, name string) bool { switch strings.ToLower(flagValue(in, name)) { case "true", "1", "yes": return true } return false } // lookupDB returns the app database. The generated main.go publishes no // database for plugin commands, so when none is published it opens // database.dsn and publishes it the way lagoon's migrate does. The returned // func closes a pool this call opened; it is a no-op otherwise. func lookupDB(ctx context.Context, app *backpack.App) (*gorm.DB, func(), error) { if app == nil { return nil, nil, fmt.Errorf("golem15.sitemap: app is nil") } if db, ok := app.Lookup[*gorm.DB](); ok && db != nil { return db, func() {}, nil } if lagoon.DSN(app.Config) == "" { return nil, nil, fmt.Errorf("golem15.sitemap: database is not configured (set SUMMER_DATABASE__DSN)") } sqlDB, gdb, err := lagoon.OpenFromApp(ctx, app) if err != nil { return nil, nil, err } if err := lagoon.Publish(app, sqlDB, gdb); err != nil { _ = sqlDB.Close() return nil, nil, err } return gdb, func() { _ = sqlDB.Close() }, nil } // singletonDefinition loads the definition with the fixed theme key, // creating it when there is none (PHP Definition::firstOrCreate). func singletonDefinition(tx *gorm.DB) (models.Definition, error) { var defs []models.Definition if err := tx.Where("theme = ?", models.DefinitionTheme).Order("id ASC").Limit(1).Find(&defs).Error; err != nil { return models.Definition{}, err } if len(defs) > 0 { return defs[0], nil } theme := models.DefinitionTheme def := models.Definition{Theme: &theme} if err := tx.Create(&def).Error; err != nil { return models.Definition{}, err } return def, nil }