- Eliminate content.Enhancer wrapper around engine.ContentEngine - Update cmd/enhance.go to call engine.ProcessFile/ProcessDirectory directly - Update sites/manager.go to use engine.NewContentEngineWithAuth directly - Remove unused EnhancementConfig and discovery configuration logic - Simplify enhance command to use input path as site ID - Remove CLI config dependency from config package
117 lines
3.4 KiB
Go
117 lines
3.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/insertr/insertr/internal/db"
|
|
"github.com/insertr/insertr/internal/engine"
|
|
)
|
|
|
|
var enhanceCmd = &cobra.Command{
|
|
Use: "enhance [input-path]",
|
|
Short: "Enhance HTML files by injecting content from database",
|
|
Long: `Enhance processes HTML files and injects latest content from the database
|
|
while adding editing capabilities. This is the core build-time enhancement
|
|
process that transforms static HTML into an editable CMS.
|
|
|
|
The input can be either a directory (to enhance all HTML files) or a single
|
|
HTML file. Non-HTML files in directories are copied as-is.`,
|
|
Args: cobra.ExactArgs(1),
|
|
Run: runEnhance,
|
|
}
|
|
|
|
var (
|
|
outputDir string
|
|
)
|
|
|
|
func init() {
|
|
enhanceCmd.Flags().StringVarP(&outputDir, "output", "o", "./dist", "Output directory for enhanced files")
|
|
}
|
|
|
|
func runEnhance(cmd *cobra.Command, args []string) {
|
|
inputPath := args[0]
|
|
siteID := inputPath
|
|
|
|
// Validate input path and determine if it's a file or directory
|
|
inputStat, err := os.Stat(inputPath)
|
|
if os.IsNotExist(err) {
|
|
log.Fatalf("Input path does not exist: %s", inputPath)
|
|
}
|
|
if err != nil {
|
|
log.Fatalf("Error accessing input path: %v", err)
|
|
}
|
|
|
|
isFile := !inputStat.IsDir()
|
|
if isFile {
|
|
// Validate that single files are HTML files
|
|
if !strings.HasSuffix(strings.ToLower(inputPath), ".html") {
|
|
log.Fatalf("Single file input must be an HTML file (.html extension): %s", inputPath)
|
|
}
|
|
}
|
|
|
|
// Load configuration
|
|
cfg, err := loadConfig()
|
|
if err != nil {
|
|
log.Fatalf("Failed to load configuration: %v", err)
|
|
}
|
|
|
|
// Override with flags if provided
|
|
if outputDir != "" {
|
|
// No output config in main config, use the flag value directly
|
|
} else {
|
|
outputDir = "./dist" // default
|
|
}
|
|
|
|
// Create content client
|
|
var client db.ContentRepository
|
|
if cfg.API.URL != "" {
|
|
fmt.Printf("🌐 Using content API: %s\n", cfg.API.URL)
|
|
client = db.NewHTTPClient(cfg.API.URL, cfg.API.Key)
|
|
} else if cfg.Database.Path != "" {
|
|
fmt.Printf("🗄️ Using database: %s\n", cfg.Database.Path)
|
|
database, err := db.NewDatabase(cfg.Database.Path)
|
|
if err != nil {
|
|
log.Fatalf("Failed to initialize database: %v", err)
|
|
}
|
|
defer database.Close()
|
|
client = database.NewContentRepository()
|
|
} else {
|
|
panic("🧪 No database or API configured\n")
|
|
}
|
|
|
|
// Create content engine directly
|
|
contentEngine := engine.NewContentEngine(client)
|
|
|
|
fmt.Printf("🚀 Starting enhancement process...\n")
|
|
fmt.Printf("📁 Input: %s\n", inputPath)
|
|
fmt.Printf("📁 Output: %s\n", outputDir)
|
|
fmt.Printf("🏷️ Site ID: %s\n\n", siteID)
|
|
|
|
// Enhance based on input type
|
|
if isFile {
|
|
// For single files, determine the output file path
|
|
outputFilePath := outputDir
|
|
if stat, err := os.Stat(outputDir); err == nil && stat.IsDir() {
|
|
// Output is a directory, use input filename in output directory
|
|
outputFilePath = filepath.Join(outputDir, filepath.Base(inputPath))
|
|
}
|
|
// If output doesn't exist or is a file, use it as-is as the output file path
|
|
|
|
if err := contentEngine.ProcessFile(inputPath, outputFilePath, siteID, engine.Enhancement); err != nil {
|
|
log.Fatalf("Enhancement failed: %v", err)
|
|
}
|
|
} else {
|
|
if err := contentEngine.ProcessDirectory(inputPath, outputDir, siteID, engine.Enhancement); err != nil {
|
|
log.Fatalf("Enhancement failed: %v", err)
|
|
}
|
|
}
|
|
|
|
fmt.Printf("\n✅ Enhancement complete! Enhanced files available in: %s\n", outputDir)
|
|
}
|