Skip to main content

Commands and parameters

This page describes every command and every parameter of codeguard 0.3.5.

Structure of a call​

codeguard [globale Optionen] <kommando> [kommando-optionen]

Global options go before the command. Example:

codeguard --format json --output build/codeguard.json check-diff --base main

Global options​

OptionValuesDefaultMeaning
--formattext, json, sarif, gitlab-sariftextOutput format. Not every command supports every format (see below).
--configFile path–Specify the project configuration explicitly. Disables the automatic search. A relative path is resolved against the current directory.
--no-project-configFlagoffIgnore .codeguard.yml/.codeguard.yaml in the project root and use the defaults.
--project-rootDirectorycurrent directoryProject root. Canonicalized via realpath. A path that cannot be resolved is exit 2.
--outputFile path–Write the report of a check-* run atomically to this file. stdout stays empty.
--ciFlagoffNon-interactive CI profile. Local trust tickets do not apply, builds and tests need an attested environment, trust grant is not allowed.
--colorauto, always, neverautoColor policy. JSON and SARIF never contain ANSI sequences.
-h, --help––Show help, also per command (codeguard check-diff --help).

Using --config and --no-project-config together is exit 2:

--config and --no-project-config cannot be used together

Which formats each command supports​

Commandtextjsonsarif / gitlab-sarif--output
check-file, check-diff, check-projectyesyesyesyes
version, config validate, doctor, trust …yesyesnono, output always goes to stdout
schemaalways prints the JSON schemano

An unsupported format aborts the run, for example:

Error: version supports only text or json output
Exit 64 for usage errors

Errors detected while parsing the arguments (unknown option, unsupported format for version, doctor, config validate or trust) end with exit 64. This value is not part of the 0–9 exit contract. In scripts, treat every code other than 0 as an error.

version​

Prints the binary and schema versions.

codeguard version
codeguard --format json version
codeguard 0.3.5 (configuration schema 1, report schema 1)
{"configurationSchemaMajor":1,"reportSchemaMajor":1,"version":"0.3.5"}

schema​

Prints an embedded JSON schema.

OptionValuesRequired
--kindconfiguration, report, sarifyes
codeguard schema --kind configuration > configuration.schema.json
codeguard schema --kind report > report.schema.json
codeguard schema --kind sarif > sarif.schema.json

With the configuration schema, your editor can validate and autocomplete .codeguard.yml. The report schema describes the JSON reports.

config validate​

Loads the organization policy and the project configuration, merges them and validates them. Nothing is checked.

codeguard config validate
codeguard --config pfad/zu/codeguard.yml config validate
codeguard --format json config validate

Text output:

configuration valid (schema 1, sha256:8325303d9119ac4ae183fc91b89ae6ebdecd34e52fc14c27d79da8ceeaf72fdb)
policy: /Library/Application Support/CodeGuard/policy.yml (sha256:22a9eaedcf877ffbbdcbcaad6d510810c334a29c194ea81404cd2eca324aee80)
metrics: rules.metrics.severity = error (built-in, locked: false)
metrics: rules.metrics.line_length.warning = 120 (project, locked: false)
metrics: rules.metrics.line_length.error = 160 (project, locked: false)
  • Line 1: schema version and configuration_hash of the effective configuration.
  • policy: source of the organization policy: built-in (no file) or the file path, each with policy_hash.
  • metrics: only appears if at least one metric rule has a threshold. Each line shows the value, its source (built-in, organization, project) and whether the value is locked.
  • project.platforms = … only appears if project.platforms is set.

JSON output (keys in camelCase, projectSource only with a project configuration):

{"configurationHash":"sha256:d6c09975a10d5760e687a744b97721503cfd86a89d3aadd331b08ccac078f41d","policyHash":"sha256:22a9eaedcf877ffbbdcbcaad6d510810c334a29c194ea81404cd2eca324aee80","policySource":{"hash":"sha256:22a9eaedcf877ffbbdcbcaad6d510810c334a29c194ea81404cd2eca324aee80","kind":"file","path":"/Library/Application Support/CodeGuard/policy.yml"},"projectSource":{"mode":"explicit","path":"waiver.yml","sha256":"sha256:60a5b5f47aa112f3521cbdcfb62882a915505396bc8c28052f7ff7bc08edb68d"},"redactionsApplied":true,"schemaVersion":1}

redactionsApplied: true means: redacted fields (e.g. owner/reason of waivers) went into the hash, but not their values.

An invalid configuration ends with exit 2 and names the source, field and reason:

configuration validation failed: project:/checks/tests/required: required checks cannot be disabled by a lower-precedence source

doctor​

Read-only diagnosis: which capabilities and tools are available? doctor runs no lint and no build and needs no trust ticket.

codeguard doctor
codeguard --format json doctor

The full sample output and its meaning are described under Getting started. A rendered result ends with exit 0, an invalid configuration with exit 2.

Important capabilities:

CapabilityMeaning
swift-format, swiftlintTool found and compatible; the version in parentheses, if it can be determined
swiftlint-metricsOnly visible if metric rules are configured
compile, testsBuild tool for the project in the current directory (inactive if there is no package and no Xcode project)
platform-checksPlatform checks active; the API catalog version in parentheses
content-rulesContent rules active; the secret detector catalog in parentheses
unauthorized-network-destinationactive only with a non-empty network allowlist
custom-rule:<id>inactive for custom rules with an unsupported engine
platform.<name>SDK available locally, the SDK version in parentheses
simctl, simulator.<name>Simulator tool, runtimes and automatically selected device type
Verwaiste CodeGuard-GeräteSimulators from aborted runs. doctor only reports them, it never deletes them.

trust status, trust grant, trust revoke​

Shows or changes the local project trust. For background, see Trust and security.

codeguard trust status
codeguard trust grant --duration 24h
codeguard trust revoke
CommandOptionValuesDefault
trust grant--durationpositive number plus m or h, at most 24h (e.g. 30m, 8h)24h

trust grant shows the execution surfaces and asks for confirmation. Only the input yes grants the ticket. It ends with exit 6 if

  • --ci is set (project trust cannot be granted in CI),
  • stdin is not a terminal (project trust requires an interactive terminal),
  • anything other than yes is entered (project trust was not granted).

Output of trust status:

trust: not-trusted
surfaces: manifest:Package.swift

Possible states: trusted, not-trusted, expired, invalid. surfaces lists the files the ticket is bound to, with their kind (manifest, plugin, build-script, xcode-project, scheme).

check-file​

Checks one or more files.

codeguard check-file Sources/App/Login.swift Sources/App/Session.swift
  • Paths are project-relative and canonical: no absolute path, no .., no ./. Otherwise exit 2 (check-file paths must be canonical and project-relative).
  • Without a path: exit 2 (check-file requires at least one project-relative path).
  • A path outside paths.include or inside paths.exclude results in exit 6 with path-outside-allowed-scope.
  • check-file never runs compile or tests, regardless of the configuration. Only security, platform, format, swiftlint and swiftlint_metrics run. That is why it needs no trust ticket.

check-diff​

Checks Git changes.

OptionMeaning
--base <rev>Compares revision <rev> with the working tree (git diff <rev>). Allowed are a branch, tag, remote branch or commit SHA.
--stagedChecks only the staging area (git diff --cached). Takes precedence over --base.
--include-untrackedAdds untracked files (git ls-files --others --exclude-standard). Default: off.

Without --base and without --staged, CodeGuard compares the working tree with the index (git diff). Changes that are already staged are then left out.

Rules you should know:

  • The project root must be the root of the Git repository. If the project lives in a subfolder of the repository, the run aborts (Git repository root differs from requested project root).
  • An unknown reference is exit 2, and so is an ambiguous one. A commit SHA whose object is missing locally (e.g. in a shallow clone) is exit 3.
  • Renames and copies are detected and checked at the new path. Deleted files, binary files, symlinks and submodules are not checked for content.
  • Without changes, the run ends with exit 0 and termination_reason: no_changes.
  • compile and tests run (default configuration) and need a trust ticket, or an attestation in CI.

Example of an unknown reference:

check failed before report creation (cause: CodeGuardTooling.GitDiffError: missingBase("doesnotexist"))

check-project​

Checks all configured, readable files of the project, then builds and tests it.

codeguard check-project

Like check-diff, check-project needs a trust ticket for compile/tests. For large projects, it is better suited as a nightly run.

Exit codes​

The exit code is the same in all formats.

CodeSymbolStatus in reportMeaning
0CG_OKpassedRun completed, no blocking findings
1CG_VALIDATION_FAILEDfailedBlocking finding (rule, format, lint, compile error, test failure)
2CG_USAGE_CONFIGerrorInvalid input, reference, path or configuration
3CG_DEPENDENCY_UNAVAILABLEerrorTool, SDK, runtime or dependency missing, or a required check stayed incomplete
4CG_TOOL_FAILUREerrorTool crash, unexpected exit, unknown or truncated output
5CG_TIMEOUTerrorTime limit exceeded
6CG_POLICY_BLOCKEDblockedPath, protection, trust or tool policy blocked the run
7CG_ITERATION_LIMITfailedReserved for the inactive fix workflows
8CG_CANCELLEDcancelledRun cancelled
9CG_INTERNAL_ERRORerrorInvariant, cleanup or atomic write failed, incomplete installation

If several failures occur, the first one detected wins. Internal errors (exit 9) always take precedence. The others are listed in the JSON report under secondary_failures. Example from a real run: a diff with content findings in a project without a trust ticket ends with exit 1, primary_failure: validation_failed and secondary_failures: ["policy_blocked"]. Without the content findings, the same run ends with exit 6.

How to react in a script:

codeguard --format json --output codeguard.json check-diff --base main
rc=$?
case $rc in
0) echo "sauber" ;;
1) echo "blockierende Funde, Bericht ansehen" ;;
2) echo "Aufruf oder Konfiguration falsch" ;;
3) echo "Werkzeug, SDK oder Abhängigkeit fehlt" ;;
6) echo "Policy hat blockiert (Trust, Pfad, geschützte Datei)" ;;
*) echo "Lauf unvollständig oder Bedienfehler (Code $rc)" ;;
esac
exit $rc

Output and streams​

  • A fully generated report goes to stdout or exactly into the --output file, even if the exit code is not 0.
  • Machine output (JSON, SARIF) is exactly one document, with no log lines before or after it.
  • Messages about usage, configuration and rendering errors go to stderr. In these cases there is no report.
  • --output writes via a temporary file in the target directory and renames it atomically. If that fails, the exit code is 9.
  • The text output shows at most 20 findings. Any further ones appear as N additional diagnostics omitted. at the end. JSON and SARIF contain all of them.

See also​