tercul-backend/cmd/cli/commands/enrich.go
Damir Mukimov d50722dad5
Some checks failed
Test / Integration Tests (push) Successful in 4s
Build / Build Binary (push) Failing after 2m9s
Docker Build / Build Docker Image (push) Failing after 2m32s
Test / Unit Tests (push) Failing after 3m12s
Lint / Go Lint (push) Failing after 1m0s
Refactor ID handling to use UUIDs across the application
- Updated database models and repositories to replace uint IDs with UUIDs.
- Modified test fixtures to generate and use UUIDs for authors, translations, users, and works.
- Adjusted mock implementations to align with the new UUID structure.
- Ensured all relevant functions and methods are updated to handle UUIDs correctly.
- Added necessary imports for UUID handling in various files.
2025-12-27 00:33:34 +01:00

111 lines
2.8 KiB
Go

package commands
import (
"context"
"fmt"
"tercul/cmd/cli/internal/bootstrap"
"tercul/internal/enrichment"
"tercul/internal/platform/config"
"tercul/internal/platform/db"
"tercul/internal/platform/log"
"github.com/google/uuid"
"github.com/spf13/cobra"
)
// NewEnrichCommand creates a new Cobra command for enriching entities
func NewEnrichCommand() *cobra.Command {
var (
entityType string
entityID string
)
cmd := &cobra.Command{
Use: "enrich",
Short: "Enrich an entity with external data",
Long: `Enrich an entity (e.g., author) with external data from sources like OpenLibrary.
Example:
tercul enrich --type author --id 123`,
RunE: func(cmd *cobra.Command, args []string) error {
if entityType == "" || entityID == "" {
return fmt.Errorf("both --type and --id are required")
}
entityIDUUID, err := uuid.Parse(entityID)
if err != nil {
return fmt.Errorf("invalid entity ID: %w", err)
}
// Load configuration
cfg, err := config.LoadConfig()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
// Initialize logger
log.Init("enrich-tool", "development")
database, err := db.InitDB(cfg, nil) // No metrics needed for this tool
if err != nil {
log.Fatal(err, "Failed to initialize database")
}
defer func() {
if err := db.Close(database); err != nil {
log.Error(err, "Error closing database")
}
}()
// Bootstrap dependencies
weaviateClient, err := bootstrap.NewWeaviateClient(cfg)
if err != nil {
return fmt.Errorf("failed to create weaviate client: %w", err)
}
deps, err := bootstrap.Bootstrap(cfg, database, weaviateClient)
if err != nil {
return fmt.Errorf("failed to bootstrap: %w", err)
}
enrichmentSvc := enrichment.NewService()
// Fetch, enrich, and save the entity
ctx := context.Background()
log.Info(fmt.Sprintf("Enriching %s with ID %s", entityType, entityIDUUID.String()))
switch entityType {
case "author":
author, err := deps.Repos.Author.GetByID(ctx, entityIDUUID)
if err != nil {
return fmt.Errorf("failed to get author: %w", err)
}
if err := enrichmentSvc.EnrichAuthor(ctx, author); err != nil {
return fmt.Errorf("failed to enrich author: %w", err)
}
if err := deps.Repos.Author.Update(ctx, author); err != nil {
return fmt.Errorf("failed to save enriched author: %w", err)
}
log.Info("Successfully enriched and saved author")
default:
return fmt.Errorf("unknown entity type: %s", entityType)
}
return nil
},
}
// Add flags
cmd.Flags().StringVarP(&entityType, "type", "t", "", "The type of entity to enrich (e.g., 'author')")
cmd.Flags().StringVarP(&entityID, "id", "i", "", "The ID of the entity to enrich")
// Mark flags as required
_ = cmd.MarkFlagRequired("type")
_ = cmd.MarkFlagRequired("id")
return cmd
}