crossplane
repository·master·Indexed 20 days ago
https://github.com/nginxinc/crossplaneA high-performance tool for parsing and building NGINX configuration files, available as a standalone Command Line Interface and a Python module. It allows developers to programmatically convert NGINX configs into structured JSON and rebuild them back into valid syntax. Key CLI commands include parse, build, lex, format, and minify. The Python module provides corresponding parse(), build(), and lex() functions for programmatic manipulation.
What's inside crossplane
- crossplane is a reliable and fast NGINX configuration file parser and builder. It allows developers to programmatically parse NGINX configuration files into structured data and rebuild them back into valid NGINX configuration syntax.
Use the crossplane CLI
masterThe
crossplanecommand-line interface allows you to perform various operations on NGINX configuration files, such as parsing them into JSON, building them from JSON, lexing tokens, formatting, and minifying.Available Commands:
parse: Converts an NGINX config file into a JSON payload.build: Reconstructs NGINX config files from a JSON payload.lex: Splits a config file into a JSON array of tokens.format: Formats an NGINX config file (usesparseinternally).minify: Removes whitespace from an NGINX config (useslexinternally).
usage: crossplane <command> [options]Parse an NGINX config into JSON with `crossplane parse`
masterThe
crossplane parsecommand takes an NGINX configuration file and converts it into a structured JSON payload following a specific schema. This is useful for programmatic manipulation of NGINX configurations.Key Features:
- Privacy: Use
--ignore DIRECTIVES(comma-separated) to exclude sensitive directives (e.g.,ssl_certificate_key) from the output. - Error Handling:
--no-catch: Stop parsing after the first error is encountered.--tb-onerror: Include Python tracebacks in the error objects for easier debugging.
- File Handling:
--combine: Useincludedirectives to create one single unified file representation.--single-file: Do not follow or include other configuration files.--include-comments: Include comments in the resulting JSON.
Example:
crossplane parse --indent=4 /etc/nginx/nginx.confusage: crossplane parse [-h] [-o OUT] [-i NUM] [--ignore DIRECTIVES] [--no-catch] [--tb-onerror] [--single-file] [--include-comments] [--strict] filename- Privacy: Use
Install crossplane via pip
masterYou can install both the Command Line Interface (CLI) and the Python Module using
pip. This provides access to thecrossplanecommand and thecrossplanePython package.pip install crossplaneBuild NGINX configs from JSON with `crossplane build`
masterThe
crossplane buildcommand takes a JSON file (structured according to thecrossplane parseschema) and reconstructs the NGINX configuration files.Options:
-d PATH, --dir PATH: The base directory to build in.-f, --force: Overwrite existing files.-i NUM, --indent NUM: Number of spaces for indentation.-t, --tabs: Use tabs instead of spaces for indentation.--no-headers: Do not write header lines to the generated configs.--stdout: Write the resulting configs to stdout instead of files.-v, --verbose: Enable verbose output.
usage: crossplane build [-h] [-d PATH] [-f] [-i NUM | -t] [--no-headers] [--stdout] [-v] filenameLex tokens from an NGINX config with `crossplane lex`
masterThe
crossplane lexcommand splits an NGINX configuration file into individual tokens (removing whitespace and comments) and outputs them as a JSON array.Options:
-o OUT, --out OUT: Write output to a file.-i NUM, --indent NUM: Number of spaces to indent output.-n, --line-numbers: Include the line number for each token in the output.
Example (Standard):
crossplane lex /etc/nginx/nginx.conf # Output: ["events","{","worker_connections", ...]Example (With Line Numbers):
crossplane lex -n /etc/nginx/nginx.conf # Output: [["events",1],["{",1],["worker_connections",2], ...]usage: crossplane lex [-h] [-o OUT] [-i NUM] [-n] filenameUse the crossplane Python module
masterBeyond the CLI, you can use
crossplaneas a Python module to programmatically parse, build, or lex NGINX configuration files. The module provides three primary functions:parse(),build(), andlex().import crossplaneFormat and Minify NGINX configs
masterCrossplane provides two lightweight utilities for quick configuration adjustments:
Format (
crossplane format) Usescrossplane parseinternally to re-format an NGINX config file. It is intended as a demonstration tool and is not a feature-rich formatter. For advanced formatting, use the Python API.Minify (
crossplane minify) Usescrossplane lexinternally to remove as much whitespace as possible from an NGINX config without changing its functionality.Usage:
crossplane format [options] filename crossplane minify [-o OUT] filenameLex NGINX configurations with crossplane.lex()
masterThe
crossplane.lex()function performs lexical analysis on an NGINX configuration file. It returns a list of 2-tuples representing the tokens found in the file. This is the programmatic equivalent of thecrossplane lexCLI command.import crossplane tokens = crossplane.lex('/etc/nginx/nginx.conf')Parse NGINX configurations with crossplane.parse()
masterThe
crossplane.parse()function takes a path to an NGINX configuration file and returns its structure as Python dictionaries. This is the programmatic equivalent of thecrossplane parseCLI command, but returns native Python types instead of a JSON string.import crossplane payload = crossplane.parse('/etc/nginx/nginx.conf')Build NGINX configurations with crossplane.build()
masterThe
crossplane.build()function takes a list of directive dictionaries and returns a single string containing a complete NGINX configuration file.import crossplane config = crossplane.build( [{ "directive": "events", "args": [], "block": [{ "directive": "worker_connections", "args": ["1024"] }] }] )Crossplane implementations in other languages
masterWhile the primary implementation is in Python, ports are available for other languages:
- Go: go-crossplane by @aluttik
- Ruby: crossplane by @gdanko