package plug import ( "context" "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/database" "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/codes" ) func upsertSource(ctx context.Context, source models.Source) (models.Source, error) { ctx, span := tracer.Start(ctx, "upsertSource") var err error localSource, err := database.GetSourceByDomain(ctx, source.Domain) if err != nil { if err.Error() == "Database error: NoDataFound" { span.AddEvent("No Source found, initializing source") log.WithContext(ctx).WithField("source_domain", source.Domain).Info("No Source found, initializing source!") source, err = database.CreateSource(ctx, source) if err != nil { span.RecordError(err) span.SetStatus(codes.Error, err.Error()) log.WithError(err).WithField("source_domain", source.Domain).Error("Failed to create source") return models.Source{}, err } span.AddEvent("Source created") log.WithContext(ctx).WithError(err).WithField("source_domain", source.Domain).WithField("source_id", source.ID).Info("Source created!") return source, nil } else { span.RecordError(err) span.SetStatus(codes.Error, err.Error()) log.WithContext(ctx).WithError(err).WithField("source_domain", source.Domain).Error("Failed to get source by domain") return models.Source{}, err } } return localSource, nil }