### Install YAML v3 Package Source: https://github.com/google/yamlfmt/blob/main/pkg/yaml/README.md Use 'go get' to install the YAML v3 package. This command fetches and installs the specified package. ```bash go get gopkg.in/yaml.v3 ``` -------------------------------- ### yamlfmt Configuration File Example Source: https://context7.com/google/yamlfmt/llms.txt Example `.yamlfmt` configuration file demonstrating command-level, path, regex exclude, extensions, and formatter settings. ```yaml # .yamlfmt # Command configuration line_ending: lf doublestar: false continue_on_error: false match_type: standard gitignore_excludes: true gitignore_path: .gitignore output_format: default # Path configuration include: - ./config/ - ./manifests/ exclude: - ./vendor/ - ./node_modules/ - ./.git/ # Regex exclude patterns (exclude files by content) regex_exclude: - "^# DO NOT FORMAT" # Extensions to include (standard mode only) extensions: - yaml - yml # Formatter configuration formatter: type: basic indent: 2 include_document_start: true line_ending: lf retain_line_breaks: false max_line_length: 120 indentless_arrays: false pad_line_comments: 2 trim_trailing_whitespace: true eof_newline: true ``` -------------------------------- ### Basic Formatter Configuration Example Source: https://context7.com/google/yamlfmt/llms.txt This is a comprehensive example of a .yamlfmt configuration file, demonstrating various options for indentation, document structure, line handling, comments, and advanced settings. ```yaml formatter: type: basic # Indentation indent: 2 array_indent: 2 indent_root_array: false indentless_arrays: false # Document structure include_document_start: true # Add --- at document start line_ending: lf # lf or crlf max_line_length: 80 # 0 for no limit # Line handling retain_line_breaks: false retain_line_breaks_single: false # Keep single blank lines trim_trailing_whitespace: true eof_newline: true # Comments pad_line_comments: 1 # Spaces before line comments # Advanced options scan_folded_as_literal: false # Preserve newlines in folded blocks disallow_anchors: false # Reject YAML anchors/aliases drop_merge_tag: false # Drop !!merge from merge operations strip_directives: false # Handle YAML directives disable_alias_key_correction: false # Style forcing force_array_style: "" # "flow", "block", or empty force_quote_style: "" # "single", "double", or empty ``` -------------------------------- ### Gitignore Patterns File Example Source: https://context7.com/google/yamlfmt/llms.txt Example content for a gitignore-style patterns file used with `match_type: gitignore`. ```bash *.yaml *.yml !testdata/ !vendor/ ``` -------------------------------- ### Basic Formatter with Include Document Start Source: https://github.com/google/yamlfmt/blob/main/docs/config-file.md Configure the basic formatter to include the document start marker. ```yaml formatter: type: basic include_document_start: true ``` -------------------------------- ### Install yamlfmt using Go Source: https://github.com/google/yamlfmt/blob/main/README.md Install the yamlfmt command-line tool using Go. Requires Go version 1.21 or greater. ```bash go install github.com/google/yamlfmt/cmd/yamlfmt@latest ``` -------------------------------- ### GitLab CI Integration Example Source: https://github.com/google/yamlfmt/blob/main/docs/output.md Example configuration for a GitLab CI pipeline to run yamlfmt and generate a Code Quality report artifact. Ensure 'yamlfmt-report' is the correct filename. ```yaml yamlfmt: script: - yamlfmt -dry -output_format gitlab . >yamlfmt-report artifacts: when: always reports: codequality: yamlfmt-report ``` -------------------------------- ### Print Version Information Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Display the installed yamlfmt version using the '-version' flag. ```bash yamlfmt -version ``` -------------------------------- ### String Array Flag Examples Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Demonstrates the two ways to provide values for string array flags: individual flags or a single comma-separated flag. ```bash -arrFlag a -arrFlag b -arrFlag c ``` ```bash -arrFlag a,b,c ``` ```bash -arrFlag a,b -arrFlag c ``` -------------------------------- ### Default Output Format Source: https://github.com/google/yamlfmt/blob/main/docs/output.md Shows the default diff format when differences are found. No specific setup is required. ```text The following formatting differences were found: y.yaml: a: a: - b: 1 b: 1 + z.yaml: a: a: - b: 1 b: 1 + x.yaml: a: a: - b: 1 b: 1 + ``` -------------------------------- ### GitLab CI Basic Usage Source: https://context7.com/google/yamlfmt/llms.txt Example GitLab CI configuration to run yamlfmt on all YAML files in the repository. ```yaml yamlfmt: image: ghcr.io/google/yamlfmt:latest script: - yamlfmt -lint . ``` -------------------------------- ### Extract and Run yamlfmt Source: https://context7.com/google/yamlfmt/llms.txt Extract the yamlfmt binary from its archive and verify its installation by checking the version. ```bash tar -xzf "yamlfmt_${VERSION}_Linux_x86_64.tar.gz" ./yamlfmt -version ``` -------------------------------- ### GitLab CI job for yamlfmt linting Source: https://github.com/google/yamlfmt/blob/main/README.md Example GitLab CI job configuration to use the yamlfmt Docker image for linting YAML files. ```yaml yamllint: image: ghcr.io/google/yamlfmt:latest script: - yamlfmt -lint . ``` -------------------------------- ### Run yamlfmt on custom file types Source: https://github.com/google/yamlfmt/blob/main/docs/pre-commit.md Override the default behavior to run yamlfmt on specific file types by configuring the 'types' and 'files' options. This example uses a placeholder regex for file paths. ```yaml - repo: https://github.com/google/yamlfmt rev: v0.19.0 hooks: - id: yamlfmt types: [file] files: ``` -------------------------------- ### Print Help Information Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Display command usage information by using the '-help' flag. ```bash yamlfmt -help ``` -------------------------------- ### Provide Formatter Configuration Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use the `-formatter` flag with comma-separated `key=value` pairs to provide specific formatter configurations. Refer to documentation for available options. ```bash yamlfmt -formatter indent=2,include_document_start=true ``` -------------------------------- ### Print Version and Configuration Source: https://context7.com/google/yamlfmt/llms.txt Display the current yamlfmt version using -version or print the effective configuration using -print_conf. ```bash yamlfmt -version ``` ```bash yamlfmt -print_conf ``` -------------------------------- ### Use Global Config Source: https://context7.com/google/yamlfmt/llms.txt Use the -global_conf flag to enable the use of the global system-wide configuration file. ```bash yamlfmt -global_conf . ``` -------------------------------- ### Basic Formatter Configuration Source: https://github.com/google/yamlfmt/blob/main/docs/config-file.md Use this configuration to set up the default basic formatter. ```yaml formatter: type: basic ``` -------------------------------- ### Format a Single YAML File Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use the default configuration to format a single YAML file. ```bash yamlfmt x.yaml ``` -------------------------------- ### Create New Integration Test Case Source: https://github.com/google/yamlfmt/blob/main/docs/integration-test.md Scaffolds a new integration test by creating the necessary directory structure and Go test file. You will need to manually add input files and expected output. ```shell TESTNAME=my_new_test make command_test_case ``` -------------------------------- ### Format YAML files with default settings Source: https://github.com/google/yamlfmt/blob/main/README.md Run the yamlfmt tool with default settings, providing one or more file paths as arguments. ```bash yamlfmt x.yaml y.yaml <...> ``` -------------------------------- ### Specify Configuration File Path Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use the `-conf` flag to specify a path to a custom configuration file. ```bash yamlfmt -conf ./config/.yamlfmt ``` -------------------------------- ### Docker Container Basic Usage Source: https://context7.com/google/yamlfmt/llms.txt Run yamlfmt using the official Docker container by mounting the current directory to '/project'. ```bash # Basic usage - mount current directory docker run -v "$(pwd):/project" ghcr.io/google/yamlfmt:latest . ``` -------------------------------- ### Line Output Format Source: https://github.com/google/yamlfmt/blob/main/docs/output.md Displays a concise list of files with formatting differences. Useful for a quick overview. ```text x.yaml: formatting difference found y.yaml: formatting difference found z.yaml: formatting difference found ``` -------------------------------- ### Uncompress and run yamlfmt binary Source: https://github.com/google/yamlfmt/blob/main/README.md After successful checksum validation, uncompress the downloaded archive and run the yamlfmt binary. ```bash tar -xzf yamlfmt_A.B.C_Linux_x86_64.tar.gz ./yamlfmt ``` -------------------------------- ### Use system-installed yamlfmt with pre-commit Source: https://github.com/google/yamlfmt/blob/main/docs/pre-commit.md Configure the pre-commit hook to use a system-installed yamlfmt binary by setting the language to 'system'. Ensure yamlfmt is in your PATH. ```yaml - repo: https://github.com/google/yamlfmt rev: v0.19.0 hooks: - id: yamlfmt language: system ``` -------------------------------- ### Download release artifacts for verification Source: https://github.com/google/yamlfmt/blob/main/README.md Download necessary files (checksums, signature, certificate) from a specific release version for artifact verification. ```text curl -sfLO https://github.com/google/yamlfmt/releases/download/vA.B.C/checksums.txt curl -sfLO https://github.com/google/yamlfmt/releases/download/vA.B.C/checksums.txt.pem curl -sfLO https://github.com/google/yamlfmt/releases/download/vA.B.C/checksums.txt.sig ``` -------------------------------- ### Enable Global Configuration Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use the `-global_conf` flag to force yamlfmt to use the system-wide configuration file. ```bash yamlfmt -global_conf ``` -------------------------------- ### Print Merged Configuration Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md View the effective merged configuration by using the '-print_conf' flag. ```bash yamlfmt -print_conf ``` -------------------------------- ### Verify Release Artifacts with Cosign Source: https://context7.com/google/yamlfmt/llms.txt Verify the authenticity of downloaded yamlfmt binaries using `cosign`. This process involves downloading checksums, signatures, and certificates, then using `cosign verify-blob`. ```bash # Download release artifacts (replace A.B.C with version) VERSION="0.19.0" curl -sfLO "https://github.com/google/yamlfmt/releases/download/v${VERSION}/checksums.txt" curl -sfLO "https://github.com/google/yamlfmt/releases/download/v${VERSION}/checksums.txt.pem" curl -sfLO "https://github.com/google/yamlfmt/releases/download/v${VERSION}/checksums.txt.sig" # Verify signature with cosign cosign verify-blob checksums.txt \ --certificate checksums.txt.pem \ --signature checksums.txt.sig \ --certificate-identity-regexp 'https://github\.com/google/yamlfmt/\.github/workflows/.+' \ --certificate-oidc-issuer "https://token.actions.githubusercontent.com" # Download and verify the binary curl -sfLO "https://github.com/google/yamlfmt/releases/download/v${VERSION}/yamlfmt_${VERSION}_Linux_x86_64.tar.gz" sha256sum --ignore-missing -c checksums.txt ``` -------------------------------- ### Format YAML files using doublestar globs Source: https://github.com/google/yamlfmt/blob/main/README.md Use the `-dstar` flag to enable an alternate mode that searches paths using doublestar globs for YAML files. ```bash yamlfmt -dstar **/*.{yaml,yml} ``` -------------------------------- ### Add Custom Extensions Source: https://context7.com/google/yamlfmt/llms.txt Use the -extensions flag to include additional file extensions for formatting, particularly useful in standard mode. ```bash yamlfmt -extensions yaml.gotmpl . ``` -------------------------------- ### Set Output Format Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use the `-output_format` flag to choose a different output format, such as `line`. Defaults to `default`. See output documentation for details. ```bash yamlfmt -output_format line ``` -------------------------------- ### Formatter Configuration via Flags Source: https://context7.com/google/yamlfmt/llms.txt Override specific formatter settings directly from the command line using the -formatter flag. ```bash yamlfmt -formatter indent=4,include_document_start=true . ``` ```bash yamlfmt -formatter retain_line_breaks=true,max_line_length=100 . ``` -------------------------------- ### Run Integration Tests Source: https://github.com/google/yamlfmt/blob/main/docs/integration-test.md Execute all integration tests for yamlfmt. This command builds and runs the end-to-end tests. ```shell make integrationtest ``` -------------------------------- ### Docker Container with Custom Config File Source: https://context7.com/google/yamlfmt/llms.txt Use a custom configuration file with the Docker container by specifying its path with the -conf flag. ```bash # With custom config file docker run -v "$(pwd):/project" ghcr.io/google/yamlfmt:latest -conf .yamlfmt . ``` -------------------------------- ### Docker Container Format Specific Files Source: https://context7.com/google/yamlfmt/llms.txt Format specific YAML files by providing their names as arguments to the Docker container command. ```bash # Format specific files docker run -v "$(pwd):/project" ghcr.io/google/yamlfmt:latest config.yaml ``` -------------------------------- ### Debug Logging Options Source: https://context7.com/google/yamlfmt/llms.txt Enable detailed debug logging for specific components or all components using the -debug flag. ```bash yamlfmt -debug paths . ``` ```bash yamlfmt -debug config . ``` ```bash yamlfmt -debug diffs . ``` ```bash yamlfmt -debug all . ``` -------------------------------- ### Use KYAML Formatter Source: https://context7.com/google/yamlfmt/llms.txt Specify the KYAML formatter to be used via the -kyaml flag. ```bash yamlfmt -kyaml . ``` -------------------------------- ### Enable All Debug Codes Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use the `-debug all` flag to enable all available debug logging codes. ```bash yamlfmt -debug all ``` -------------------------------- ### Format YAML Files Source: https://context7.com/google/yamlfmt/llms.txt Format one or more YAML files by providing their paths as arguments. Supports recursive formatting and pattern matching. ```bash # Format a single file yamlfmt config.yaml ``` ```bash # Format multiple files yamlfmt config.yaml deployment.yaml values.yaml ``` ```bash # Format all YAML files in a directory recursively yamlfmt . ``` ```bash # Format files matching specific patterns (doublestar mode) yamlfmt -dstar "**/*.yaml" ``` ```bash # Format only specific subdirectories yamlfmt ./config/ ./manifests/ ``` -------------------------------- ### Format YAML files in a directory recursively Source: https://github.com/google/yamlfmt/blob/main/README.md Format all YAML files within a specified directory, searching recursively. ```bash yamlfmt . ``` -------------------------------- ### Output Format Options Source: https://context7.com/google/yamlfmt/llms.txt Control the output format using -output_format. Use 'line' for line-based output or 'gitlab' for GitLab CI Code Quality report format. The -dry flag performs a dry run. ```bash yamlfmt -output_format line -dry . ``` ```bash yamlfmt -output_format gitlab -dry . > report.json ``` -------------------------------- ### Run yamlfmt using Docker Source: https://github.com/google/yamlfmt/blob/main/README.md Execute the yamlfmt command within a Docker container, mounting the current directory as a volume. ```bash docker run -v "$(pwd):/project" ghcr.io/google/yamlfmt:latest ``` -------------------------------- ### Docker Container Format from Stdin Source: https://context7.com/google/yamlfmt/llms.txt Format YAML content piped to the Docker container's standard input using the -in flag. ```bash # Format from stdin cat config.yaml | docker run -i ghcr.io/google/yamlfmt:latest -in ``` -------------------------------- ### Download and validate release archive checksum Source: https://github.com/google/yamlfmt/blob/main/README.md Download a specific release archive and validate its checksum against the downloaded checksums file. ```bash curl -sfLO https://github.com/google/yamlfmt/releases/download/vA.B.C/yamlfmt_A.B.C_Linux_x86_64.tar.gz sha256sum --ignore-missing -c checksums.txt ``` -------------------------------- ### Use Formatter Registry in Go Source: https://context7.com/google/yamlfmt/llms.txt Manage multiple yamlfmt formatter types using a registry. This allows for dynamic selection and use of different formatting strategies within your Go application. ```go // Using the formatter registry for multiple formatter types package main import ( "fmt" "log" "github.com/google/yamlfmt" "github.com/google/yamlfmt/formatters/basic" "github.com/google/yamlfmt/formatters/kyaml" ) func main() { // Create registry with default formatter registry := yamlfmt.NewFormatterRegistry(&basic.BasicFormatterFactory{}) // Add additional formatters registry.Add(&kyaml.KyamlFormatterFactory{}) // Get factory by type factory, err := registry.GetFactory("basic") if err != nil { log.Fatalf("failed to get factory: %v", err) } // Get default factory defaultFactory, err := registry.GetDefaultFactory() if err != nil { log.Fatalf("failed to get default factory: %v", err) } fmt.Printf("Factory type: %s\n", factory.Type()) fmt.Printf("Default factory type: %s\n", defaultFactory.Type()) } ``` -------------------------------- ### Format YAML Files with Doublestar Globbing Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Utilize doube-star globbing for more dynamic path patterns to select files for formatting. ```bash yamlfmt -dstar "**/*.yaml" ``` -------------------------------- ### Enable Doublestar Mode via Flag Source: https://context7.com/google/yamlfmt/llms.txt Override the default path collection mode to 'doublestar' using the -match_type or -dstar flag. ```bash yamlfmt -match_type doublestar "**/*.yaml" ``` ```bash yamlfmt -dstar "**/*.yaml" ``` -------------------------------- ### Format YAML with Basic Formatter in Go Source: https://context7.com/google/yamlfmt/llms.txt Use the basic formatter to programmatically format YAML content within your Go applications. Configure settings like indentation and line endings. ```go package main import ( "fmt" "log" "github.com/google/yamlfmt" "github.com/google/yamlfmt/formatters/basic" ) func main() { // Create a formatter factory factory := &basic.BasicFormatterFactory{} // Configure formatter settings config := map[string]interface{}{ "indent": 2, "include_document_start": true, "line_ending": "lf", "retain_line_breaks": false, "pad_line_comments": 2, } // Create the formatter formatter, err := factory.NewFormatter(config) if err != nil { log.Fatalf("failed to create formatter: %v", err) } // Input YAML content input := []byte(`foo: bar baz: qux items: - one - two`) // Format the content output, err := formatter.Format(input) if err != nil { log.Fatalf("failed to format: %v", err) } fmt.Printf("Formatted output:\n%s", output) // Output: // --- // foo: bar // baz: qux // items: // - one // - two } ``` -------------------------------- ### Lint YAML Files for Formatting Differences Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use Lint mode with the '-lint' flag. This mode checks for formatting differences and exits with code 1 if any are found, outputting diffs to stdout. ```bash yamlfmt -lint . ``` -------------------------------- ### Specify Custom Config File Location Source: https://context7.com/google/yamlfmt/llms.txt Use the -conf flag to specify a custom path for the yamlfmt configuration file. ```bash yamlfmt -conf ./config/.yamlfmt . ``` -------------------------------- ### Dry Run YAML Formatting Source: https://context7.com/google/yamlfmt/llms.txt Preview formatting changes without modifying files. Displays diffs for files that would be altered. Supports quiet mode. ```bash # Dry run on all YAML files yamlfmt -dry . ``` ```bash # Dry run with quiet mode (only file paths) yamlfmt -dry -quiet . ``` ```bash # Dry run on specific files yamlfmt -dry config.yaml ``` ```bash # Example output: # config.yaml: # foo: foo: # - bar: 1 bar: 1 # + ``` -------------------------------- ### Verify signature of release artifacts Source: https://github.com/google/yamlfmt/blob/main/README.md Verify the signature of the checksums file using cosign, specifying certificate identity and OIDC issuer. ```shell cosign verify-blob checksums.txt \ --certificate checksums.txt.pem \ --signature checksums.txt.sig \ --certificate-identity-regexp 'https://github\.com/google/yamlfmt/\.github/workflows/.+' \ --certificate-oidc-issuer "https://token.actions.githubusercontent.com" ``` -------------------------------- ### Unmarshal and Marshal YAML Data in Go Source: https://github.com/google/yamlfmt/blob/main/pkg/yaml/README.md Demonstrates unmarshalling YAML data into a struct and a map, then marshalling them back to YAML. Ensure struct fields are public for correct unmarshalling. The `yaml:"flow"` tag controls the output format for slices. ```Go package main import ( "fmt" "log" "gopkg.in/yaml.v3" ) var data = ` a: Easy! b: c: 2 d: [3, 4] ` // Note: struct fields must be public in order for unmarshal to // correctly populate the data. type T struct { A string B struct { RenamedC int `yaml:"c"` D []int `yaml:",flow"` } } func main() { t := T{} err := yaml.Unmarshal([]byte(data), &t) if err != nil { log.Fatalf("error: %v", err) } fmt.Printf("--- t:\n%v\n\n", t) d, err := yaml.Marshal(&t) if err != nil { log.Fatalf("error: %v", err) } fmt.Printf("--- t dump:\n%s\n\n", string(d)) m := make(map[interface{}]interface{}) err = yaml.Unmarshal([]byte(data), &m) if err != nil { log.Fatalf("error: %v", err) } fmt.Printf("--- m:\n%v\n\n", m) d, err = yaml.Marshal(&m) if err != nil { log.Fatalf("error: %v", err) } fmt.Printf("--- m dump:\n%s\n\n", string(d)) } ``` -------------------------------- ### Pre-commit Hook with System-Installed yamlfmt Source: https://context7.com/google/yamlfmt/llms.txt Configure the pre-commit hook to use a system-installed version of yamlfmt by setting the language to 'system'. ```yaml # .pre-commit-config.yaml # Use system-installed yamlfmt - repo: https://github.com/google/yamlfmt rev: v0.19.0 hooks: - id: yamlfmt language: system ``` -------------------------------- ### Use Gitignore for Excludes Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Enable the `-gitignore_excludes` flag to use a `.gitignore` file for excluding paths. This is in addition to other exclude patterns. ```bash yamlfmt -gitignore_excludes ``` -------------------------------- ### Enable Debug Logging Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use the `-debug` flag with comma-separated codes to enable specific debug logging categories. See Debug Logging section for supported codes. ```bash yamlfmt -debug paths,config ``` -------------------------------- ### Kyaml Formatter Configuration Source: https://github.com/google/yamlfmt/blob/main/docs/config-file.md Switch to the kyaml formatter by changing the type in the formatter configuration. ```yaml formatter: type: kyaml ``` -------------------------------- ### Enable KYAML Formatter Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use the `-kyaml` flag to enable the alternate KYAML formatter. This overrides any formatter configuration from a detected config file. ```bash yamlfmat -kyaml ``` -------------------------------- ### Dry Run Formatting Differences Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Enable Dry Run mode using the '-dry' flag. This mode formats files and prints any files that would have had a formatting diff to stdout. ```bash yamlfmt -dry . ``` -------------------------------- ### Pre-commit Hook with Custom File Types Source: https://context7.com/google/yamlfmt/llms.txt Customize the file types and patterns that the pre-commit hook will process by specifying 'types' and 'files'. ```yaml # .pre-commit-config.yaml # Custom file types - repo: https://github.com/google/yamlfmt rev: v0.19.0 hooks: - id: yamlfmt types: [file] files: \.(yaml|yml|yaml\.gotmpl)$ ``` -------------------------------- ### Pre-commit Hook with Configuration File Source: https://context7.com/google/yamlfmt/llms.txt Configure the pre-commit hook to use a specific configuration file and disable passing filenames to yamlfmt. ```yaml # .pre-commit-config.yaml # Use with configuration file (disable passing filenames) - repo: https://github.com/google/yamlfmt rev: v0.19.0 hooks: - id: yamlfmt pass_filenames: false ``` -------------------------------- ### Format YAML from Stdin Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Read YAML input from stdin and write the formatted output to stdout. This can be achieved using '-', '/dev/stdin', or the '-in' flag. ```bash cat x.yaml | yamlfmt - ``` ```bash cat x.yaml | yamlfmt /dev/stdin ``` ```bash cat x.yaml | yamlfmt -in ``` -------------------------------- ### Standard Path Collection Mode Source: https://context7.com/google/yamlfmt/llms.txt Configure yamlfmt to use standard file and directory path matching. This is the default mode and is suitable for direct path specifications. ```yaml # .yamlfmt match_type: standard include: - ./config/ - ./manifests/ exclude: - ./test/fixtures/ extensions: - yaml - yml ``` -------------------------------- ### Specify File Extensions Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use the `-extensions` flag with comma-separated values to define file extensions for standard path collection. This has no effect in Doublestar mode. ```bash yamlfmt -extensions yaml,yml ``` -------------------------------- ### yamlfmt Dry Run with Different Output Formats Source: https://context7.com/google/yamlfmt/llms.txt Utilize the `yamlfmt -dry` command to check for formatting differences. Supports various output formats like default (diff), line, and GitLab Code Quality report for CI/CD integration. ```bash # Default output - shows side-by-side diff yamlfmt -dry . # Output: # config.yaml: # foo: foo: # - bar: 1 bar: 1 # + # Line output - simple one-line-per-file format yamlfmt -dry -output_format line . # Output: # config.yaml: formatting difference found # deployment.yaml: formatting difference found # GitLab Code Quality report format yamlfmt -dry -output_format gitlab . # Output: # [ # { # "description": "Not formatted correctly, run yamlfmt to resolve.", # "check_name": "yamlfmt", # "fingerprint": "c1dddeed9a8423b815cef59434fe3dea90d946016c8f71ecbd7eb46c528c0179", # "severity": "major", # "location": { # "path": "config.yaml" # } # } # ] # Compact GitLab output with quiet mode yamlfmt -dry -output_format gitlab -quiet . > report.json ``` -------------------------------- ### Verbose Mode in Format Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Enable verbose mode with '-verbose' or '-v'. This flag only affects the Format mode, providing more detailed output. ```bash yamlfmt -v . ``` -------------------------------- ### GitLab CI with Code Quality Report Source: https://context7.com/google/yamlfmt/llms.txt Configure GitLab CI to use yamlfmt in dry run mode with 'gitlab' output format to generate a Code Quality report. ```yaml yamlfmt: image: ghcr.io/google/yamlfmt:latest script: - yamlfmt -dry -output_format gitlab . > yamlfmt-report.json artifacts: when: always reports: codequality: yamlfmt-report.json ``` -------------------------------- ### Add Exclude Patterns from Command Line Source: https://context7.com/google/yamlfmt/llms.txt Specify exclude patterns directly on the command line using the -exclude flag. ```bash yamlfmt -exclude ./vendor/,./node_modules/ . ``` -------------------------------- ### Process YAML via Stdin/Stdout Source: https://context7.com/google/yamlfmt/llms.txt Read YAML from stdin and output formatted YAML to stdout. Useful for pipeline integration. Triggered by '-', '/dev/stdin', or '-in'. ```bash # Using dash notation cat unformatted.yaml | yamlfmt - ``` ```bash # Using /dev/stdin cat unformatted.yaml | yamlfmt /dev/stdin ``` ```bash # Using -in flag cat unformatted.yaml | yamlfmt -in ``` ```bash # Example with inline YAML echo -e "foo: bar\nbaz: qux" | yamlfmt - # Output: # foo: bar # baz: qux ``` ```bash # Pipe from kubectl and format kubectl get configmap my-config -o yaml | yamlfmt - ``` -------------------------------- ### Gitignore Path Collection Mode Source: https://context7.com/google/yamlfmt/llms.txt Configure yamlfmt to use patterns from a separate file, similar to .gitignore. This allows for complex exclusion rules. ```yaml # .yamlfmt match_type: gitignore include: - yamlfmt.patterns # File containing gitignore-style patterns ``` -------------------------------- ### Disable Global Configuration Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use the `-no_global_conf` flag to disable the use of the system-wide configuration file. ```bash yamlfmt -no_global_conf ``` -------------------------------- ### Specify Gitignore File Path Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use the `-gitignore_path` flag to specify a custom path to a gitignore file. Defaults to `.gitignore`. ```bash yamlfmt -gitignore_path .special_gitignore ``` -------------------------------- ### Doublestar Path Collection Mode Source: https://context7.com/google/yamlfmt/llms.txt Configure yamlfmt to use glob patterns for file discovery. This mode is powerful for matching files recursively across directories. ```yaml # .yamlfmt match_type: doublestar include: - "**/*.yaml" - "**/*.yml" - "config/**/*.yaml" exclude: - "**/vendor/**" - "**/node_modules/**" - "**/*_test.yaml" ``` -------------------------------- ### Lint YAML Files Source: https://context7.com/google/yamlfmt/llms.txt Check YAML files for formatting differences without modifying them. Exits with code 1 if changes are needed, suitable for CI checks. Supports quiet mode. ```bash # Lint all YAML files in current directory yamlfmt -lint . ``` ```bash # Lint specific files yamlfmt -lint config.yaml deployment.yaml ``` ```bash # Lint with quiet mode (only show file paths) yamlfmt -lint -quiet . ``` ```bash # Lint in doublestar mode yamlfmt -dstar -lint "**/*.{yaml,yml}" ``` ```bash # Example CI/CD usage - fails if any files need formatting yamlfmt -lint . || exit 1 ``` -------------------------------- ### GitLab Code Quality Report Format Source: https://github.com/google/yamlfmt/blob/main/docs/output.md Generates a JSON report for GitLab Code Quality. Requires the 'gitlab' output format. ```json [ { "description": "Not formatted correctly, run yamlfmt to resolve.", "check_name": "yamlfmt", "fingerprint": "c1dddeed9a8423b815cef59434fe3dea90d946016c8f71ecbd7eb46c528c0179", "severity": "major", "location": { "path": ".gitlab-ci.yml" } }, ] ``` -------------------------------- ### Set Match Type for Path Collection Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use the `-match_type` flag to control how `include` and `exclude` patterns are interpreted. Refer to path documentation for details. ```bash yamlfmt -match_type standard ``` -------------------------------- ### Add yamlfmt to pre-commit config Source: https://github.com/google/yamlfmt/blob/main/docs/pre-commit.md Include this configuration in your .pre-commit-config.yaml to add yamlfmt as a pre-commit hook. This uses the default behavior of yamlfmt. ```yaml - repo: https://github.com/google/yamlfmt rev: v0.19.0 hooks: - id: yamlfmt ``` -------------------------------- ### Verbose and Quiet Modes Source: https://context7.com/google/yamlfmt/llms.txt Control the verbosity of the output using -verbose for more details or -quiet (-q) for less output. Combine with -dry for dry run operations. ```bash yamlfmt -verbose . ``` ```bash yamlfmt -dry -quiet . ``` ```bash yamlfmt -lint -q . ``` -------------------------------- ### Docker Container Lint Mode Source: https://context7.com/google/yamlfmt/llms.txt Run yamlfmt in lint mode within the Docker container to check for formatting errors without modifying files. ```bash # Lint mode docker run -v "$(pwd):/project" ghcr.io/google/yamlfmt:latest -lint . ``` -------------------------------- ### Exclude Paths from Collection Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Use the `-exclude` flag with comma-separated patterns to specify paths to exclude from collection. These are in addition to config file excludes. ```bash yamlfmt -exclude ./not/,these_paths.yaml ``` -------------------------------- ### Docker Container Dry Run Source: https://context7.com/google/yamlfmt/llms.txt Perform a dry run of yamlfmt within the Docker container to see which files would be modified, using the -dry flag. ```bash # Dry run docker run -v "$(pwd):/project" ghcr.io/google/yamlfmt:latest -dry . ``` -------------------------------- ### Update Golden Files Source: https://github.com/google/yamlfmt/blob/main/docs/integration-test.md Regenerate the "golden" or expected output files used by the integration tests. This is useful after making changes to yamlfmt's behavior. ```shell make integrationtest_update ``` -------------------------------- ### Ignore File Metadata in yamlfmt Source: https://github.com/google/yamlfmt/blob/main/docs/metadata.md Add this metadata to a YAML file to exclude it from formatting by yamlfmt. Ensure the format is exactly `#!yamlfmt!:ignore` with no leading/trailing spaces. ```yaml # !yamlfmt!:ignore ``` -------------------------------- ### Quiet Mode in Dry Run or Lint Source: https://github.com/google/yamlfmt/blob/main/docs/command-usage.md Activate quiet mode using '-quiet' or '-q'. This flag only affects Dry Run or Lint modes, printing only the paths of files with diffs. ```bash yamlfmt -dry -q . ``` -------------------------------- ### File Modifications Report Source: https://github.com/google/yamlfmt/blob/main/integrationtest/command/testdata/debug_diffs/stdout/stdout.txt This output indicates which files were modified by the yamlfmt tool and shows the specific changes made. It is useful for reviewing formatting adjustments. ```text [DEBUG]: The following files were modified: a.yaml: - a: 1 a: 1 + b.yaml: - a: 1 a: 1 + c.yaml: - a: 1 a: 1 + ``` -------------------------------- ### Run yamlfmt without passing filenames Source: https://github.com/google/yamlfmt/blob/main/docs/pre-commit.md Prevent the pre-commit hook from passing filenames as arguments by setting 'pass_filenames' to false. This ensures that path configurations in your .yamlfmt file are correctly applied. ```yaml - repo: https://github.com/google/yamlfmt rev: v0.19.0 hooks: - id: yamlfmt pass_filenames: false ``` -------------------------------- ### Invalid Metadata Formats in yamlfmt Source: https://github.com/google/yamlfmt/blob/main/docs/metadata.md These formats will cause errors in yamlfmt. Ensure there is no space after the colon and that the identifier is correctly formatted. ```yaml # !yamlfmt!: ignore ``` ```yaml # !yamlfmt!ignore ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.