This file provides guidance to AI coding agents when working with code in this repository.
Contentful Commander — a Go library and CLI for Contentful CMA migrations. Provides a unified Entity interface over entries and assets, locale-aware field access, dual CMA/CDA loading with CDA views, batch migration execution, and DeepL translation integration.
Go 1.25 · module github.com/foomo/contentfulcommander
- mise (
.mise.toml) — manages tool versions: lefthook, golangci-lint - lefthook (
.lefthook.yaml) — git hooks: pre-commit (branch namingfeature/|fix/, golangci fmt/lint), commit-msg (conventional commits), post-checkout (mise install) - dependabot (
.github/dependabot.yml) — automated dependency updates for GitHub Actions and Go modules
make check # Run tidy, generate, lint, test (full CI check)
make build # Build binary to bin/contenfulcommander (with -tags=safe)
make install # Install to $GOPATH/bin (with -tags=safe)
make test # go test -tags=safe -coverprofile=coverage.out
make test.race # go test with -race flag
make test.update # go test with -update flag
make lint # golangci-lint run
make lint.fix # golangci-lint run --fix
make tidy # go mod tidy
make generate # go generate ./...
make outdated # Show outdated direct dependencies
make godocs # Open go docs
# Run a single test
go test ./commanderclient -run TestNameSingle library package (commanderclient/) with a thin CLI entry point (main.go).
-
Entityinterface (types.go) — unified API overEntryEntityandAssetEntity. All field access is locale-aware with fallback support. Publishing status derived from version arithmetic:draft(PublishedVersion==0),published(Version-PublishedVersion==1),changed(>1). -
MigrationClient(client.go) — wraps the foomo/contentful CMA SDK. Optionally pairs a CDA client for published-view access.LoadSpaceModel()fetches locales, content types, entries, and assets concurrently into aSpaceModelcache. Initial entry loading is split by content type with 3 workers, starts at 1000-entry pages, and halves the page size only for content types that hit Contentful response-size limits.UpdateSpaceModel()incrementally refreshes only recently changed entities (ordered by-sys.updatedAt, pages of 100, stops when reaching entities older than the previous update start). When a CDA key is provided, CDA views are loaded and attached to each entity (entity.HasCDAView(),entity.CDAView()). SetConfig.SkipAssets = trueto skip asset loading entirely, orConfig.SkipEntries = trueto skip entry loading — both apply to the CMA and CDA phases ofLoadSpaceModelandUpdateSpaceModel; locales and content types always load. Provides entity lookups and filtering. -
Reverse-reference index (
references.go) — edges "referrer -> target" per (referrer ID, field, locale), built lazily on the first reverse query (GetParents,GetParentsViaFields,GetReferrers,GetReferrerPath) and guarded bycacheMu. Every library path that adds, replaces, mutates or removes a cached entity must keep it consistent (see the comment block at the top ofreferences.go);LoadSpaceModeldrops it.RefreshEntityremoves entities Contentful no longer has (ErrEntityNotFound);RefreshEntityOutcomereports that asRefreshOutcomeRemoved. Archived entities stay cached withStatusArchivedand never carry a CDA view. -
EntityCollection(collection.go) — chainable operations on entity sets: filtering (50+ built-in filters), pagination, concurrent iteration (ForEachConcurrent), field extraction, grouping, stats, and conversion to migration operations. -
MigrationExecutor(executor.go) — runsMigrationOperationbatches with configurable concurrency, dry-run mode, and per-operation confirmation. Update operations preserve publishing status. -
Asset creation (
assets.go) —CreateAssetFromURL()andCreateAssetFromURLAndPublish()wrap the CMA upsert → process → poll cycle for assets Contentful fetches from a remote URL. The file name is derived from the URL (falling back to the asset ID), a title is required (the SDK'sAssets.Processonly processes locales that have one), and the processing wait is bounded by the context with a 2-minute default. Creating over an existing ID is an error, not an overwrite. -
Entry creation (
create_entry.go) —CreateEntry()creates a draft entry (ID generated by Contentful when empty), adds it to the cache and the reverse-reference index, and never publishes. Creating over an existing ID is an error, as for assets.
DeepLTranslator(translate.go,deepl.go) — translates entity fields via DeepL API v2. Handles both simple strings and RichText (extracts text nodes, translates, reassembles). Tracks billed characters.- RichText processing (
richtext_internal.go,hyperlinks.go) — parses Contentful RichText JSON, extracts/replaces text nodes, processes hyperlinks. - Text utilities (
textutils.go) —MatchCase,FixURI(diacritics removal, slug generation),ToLowerURL.
Environment variables: CONTENTFUL_CMAKEY, CONTENTFUL_CDAKEY (optional, enables CDA views), CONTENTFUL_SPACE_ID, CONTENTFUL_ENVIRONMENT, CONTENTFUL_VERBOSE. Loaded via LoadConfigFromEnv() in utils.go. Config.SkipAssets and Config.SkipEntries are code-only (no env vars).
- Local imports grouped under
github.com/foomo/contentfulcommander(enforced by goimports) - Tabs for indentation, trailing whitespace trimmed, LF line endings
- Build tag:
-tags=safeused across build, install, and test targets golangci-lintv2 config in.golangci.yml—default: allwith explicit disable list; formatters: gofmt, goimports- Branch names must follow
feature/orfix/prefix convention (enforced by lefthook pre-commit hook) - Commit messages must follow Conventional Commits format:
type(scope?): subject(enforced by lefthook commit-msg hook). Valid types:build,chore,ci,docs,feat,fix,perf,refactor,style,test,sec,wip,revert