Skip to content

goca analyze

goca analyze performs a deep static self-analysis of the project generated by Goca. It audits architecture correctness, code quality, security patterns, Go standards, test coverage, and dependency hygiene — all in one command.

Syntax

bash
goca analyze [flags]

Purpose

Unlike goca doctor (which checks project health at a structural level), analyze inspects the content of your generated Go code:

  • Enforces Clean Architecture layer boundaries using Go's AST parser
  • Detects OWASP A03 security anti-patterns (SQL injection, hardcoded secrets, unsafe packages)
  • Validates Go naming and documentation conventions
  • Checks test quality patterns (table-driven, mocks, t.TempDir())
  • Audits dependency hygiene in go.mod

Flags

FlagTypeDefaultDescription
--archboolfalseOnly run architecture checks
--qualityboolfalseOnly run code quality checks
--securityboolfalseOnly run security checks
--standardsboolfalseOnly run Go standards checks
--testsboolfalseOnly run test quality checks
--depsboolfalseOnly run dependency checks
--outputstringtextOutput format: text or json
--fail-on-warnboolfalseExit with code 2 when warnings are present

When no category flag is set, all 6 categories run.

Exit Codes

CodeMeaning
0All checks passed
1One or more checks failed
2Warnings present with --fail-on-warn

Usage Examples

Full analysis

bash
goca analyze

Security-only for CI

bash
goca analyze --security --fail-on-warn

Architecture check only

bash
goca analyze --arch

JSON output for tooling

bash
goca analyze --output json | jq '.[] | select(.status == "✗")'

Strict CI pipeline (fail on any warning)

bash
goca analyze --fail-on-warn

Check Categories

Architecture (6 rules)

RuleDescription
layer-dirsAll 4 CA layers (domain, usecase, repository, handler) must exist
domain-purityinternal/domain must not import ORMs or external frameworks
usecase-no-handlerUse cases must not depend on the handler layer
repo-impl-coverageEach domain entity should have a repository implementation
di-containerA DI container must exist in internal/di
handler-no-repoHandlers must not import repositories directly (must go through use cases)

Quality (5 rules)

RuleDescription
no-empty-filesNo .go files with only a package declaration
package-namingPackages must be lowercase single words (no underscores)
no-todosNo // TODO or // FIXME comments in generated code
exported-docsAll exported functions must have godoc comments
main-goAn entry point main.go must exist

Security (5 rules)

RuleDescription
no-hardcoded-secretsNo string assignments that look like passwords/tokens/secrets
no-sql-injectionNo fmt.Sprintf("SELECT/INSERT/UPDATE/DELETE patterns (OWASP A03)
no-unsafeNo import "unsafe" in generated application code
no-tls-skipNo InsecureSkipVerify: true in TLS config
env-configSensitive config should be read from os.Getenv()

Standards (5 rules)

RuleDescription
file-namingGo files must use snake_case (no kebab-case)
no-init-in-domainNo init() functions in domain layer
go-mod-validgo.mod must have a valid module declaration
goca-yaml.goca.yaml must be present and non-empty
context-propagationcontext.Context must be used in repository and use case layers

Tests (4 rules)

RuleDescription
test-files-existEach non-empty layer must have *_test.go files
table-driven-testsAt least one []struct{...} table-driven test must exist
mocks-existA mocks directory must be present
test-tempdirTests must use t.TempDir(), not hardcoded /tmp paths

Dependencies (4 rules)

RuleDescription
go-mod-tidygo.sum must exist when go.mod has require blocks
no-replaceNo replace directives (local dev artifact)
go-versiongo.mod must declare a Go version
no-insecure-modulesNo known-insecure or deprecated modules (e.g., dgrijalva/jwt-go)

Sample Output

Goca Analyze — Deep Project Self-Analysis

Step 1 — Architecture analysis
Step 2 — Code quality analysis
Step 3 — Security analysis
Step 4 — Standards analysis
Step 5 — Test coverage analysis
Step 6 — Dependency analysis

Architecture
 ✓  layer-dirs           project   All 4 Clean Architecture layers present      —
 ✓  domain-purity        project   Domain layer imports only stdlib              —
 ✓  usecase-no-handler   project   Use cases do not import handler layer         —
 ✓  repo-impl-coverage   project   Repository implementations found for 3 ents  —
 ✓  di-container         project   DI container present (2 files)               —
 ✓  handler-no-repo      project   Handlers do not import repository directly   —

Security
 ✓  no-hardcoded-secrets  project  No hardcoded secret patterns detected         —
 ✗  no-sql-injection       internal/repository/user.go
                                   SQL query built with fmt.Sprintf              Use parameterised queries

Analyzed 29 rules — 28 passed, 0 warnings, 1 failed

MCP Integration

goca analyze is exposed as the goca_analyze tool in the MCP server, allowing AI assistants (GitHub Copilot, Claude, Cursor) to audit your project and suggest fixes automatically.

json
{
  "tool": "goca_analyze",
  "arguments": {
    "security": true,
    "output": "json"
  }
}

Released under the MIT License.