Categorygithub.com/nginxinc/nginx-go-crossplane
modulepackage
0.4.63
Repository: https://github.com/nginxinc/nginx-go-crossplane.git
Documentation: pkg.go.dev

# README

nginx-go-crossplane

A Go port of the NGINX config/JSON converter crossplane.

Parse

This is an example that takes a path to an NGINX config file, converts it to JSON, and prints the result to stdout.

package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/nginxinc/nginx-go-crossplane"
)

func main() {
	path := os.Args[1]

	payload, err := crossplane.Parse(path, &crossplane.ParseOptions{})
	if err != nil {
		panic(err)
	}

	b, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(b))
}

Build

This is an example that takes a path to a JSON file, converts it to an NGINX config, and prints the result to stdout.

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"

	"github.com/nginxinc/nginx-go-crossplane"
)

func main() {
	path := os.Args[1]

	file, err := os.Open(path)
	if err != nil {
		panic(err)
	}

	content, err := ioutil.ReadAll(file)
	if err != nil {
		panic(err)
	}

	var payload crossplane.Payload
	if err = json.Unmarshal(content, &payload); err != nil {
		panic(err)
	}

	var buf bytes.Buffer
	if err = crossplane.Build(&buf, payload.Config[0], &crossplane.BuildOptions{}); err != nil {
		panic(err)
	}

	fmt.Println(buf.String())
}

Generate support for third-party modules

This is a simple example that takes the path of a third-party module source code to generate support for it. For detailed usage of the tool, please run go run ./cmd/generate/ --help. Assuming the source code path of that module is ./src, you can call go run ./cmd/generate/ --src-path=./src -directive-map-name=directives -match-func-name=Match -match-func-comment=comment. The output will be similar to:

/**
 * Copyright (c) F5, Inc.
 *
 * This source code is licensed under the Apache License, Version 2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

// Code generated by generator; DO NOT EDIT.
// All the definitions are extracted from the source code
// Each bit mask describes these behaviors:
//   - how many arguments the directive can take
//   - whether or not it is a block directive
//   - whether this is a flag (takes one argument that's either "on" or "off")
//   - which contexts it's allowed to be in

package crossplane

var directives = map[string][]uint{
    "my_directive_1": {
        bitmask01|bitmask02|...,
		bitmask11|bitmask12|...,
		...
    },
    "my_directive_2": {
        bitmask01|bitmask02|...,
		bitmask11|bitmask12|...,
		...
    },
}

// comment
func Match(directive string) ([]uint, bool) {
    m, ok := directives[directive]
    return m, ok
}

You can redirect the stdout into a .go file, and pass the generated matchFunc to ParseOptions.DirectiveSources when invoking Parse.

Contributing

If you'd like to contribute to the project, please read our Contributing guide.

License

Apache License, Version 2.0

© F5 Networks, Inc. 2022

# Packages

No description provided by the author

# Functions

Build creates an NGINX config from a crossplane.Config.
BuildFiles builds all of the config files in a crossplane.Payload and writes them to disk.
BuildInto builds all of the config files in a crossplane.Payload and writes them to the Creator.
BuildWithBuilder registers a builder to build the NGINX configuration for the given directives.
No description provided by the author
No description provided by the author
No description provided by the author
LexWithLexer registers a Lexer that implements tokenization of an NGINX configuration after one of the given stringTokens is encountered by Lex.
LexWithOptions allows for custom lexing behavior through external lexers specified in the LexOptions.
MatchAppProtectWAFv4 is a MatchFunc for App Protect v4 module.
MatchAppProtectWAFv5 is a MatchFunc for App Protect v5 module.
MatchGeoip2Latest is a MatchFunc for the latest version of geoip2.
MatchHeadersMoreLatest is a MatchFunc for the latest version of headersmore.
MatchLuaLatest is a MatchFunc for latest version of Lua.
No description provided by the author
No description provided by the author
No description provided by the author
MatchNjsLatest is a MatchFunc for the latest version of njs.
MatchOss124 contains directives in OSS 1.2.4 source code(including GEOIP, Perl, and XSLT).
MatchOss126 contains directives in OSS 1.2.6 source code(including GEOIP, Perl, and XSLT).
MatchOssLatest contains directives in latest version of OSS source code(including GEOIP, Perl, and XSLT).
MatchOtelLatest is a MatchFunc for latest version of otel.
Parse parses an NGINX configuration file.
note: this is only used during tests, should not be changed.

# Constants

No description provided by the author
No description provided by the author

# Variables

nolint:gochecknoglobals.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
FileString is a string representation of a file.
LexOptions allows customization of the lexing process by specifying external lexers that can handle specific directives.
Lua adds support for directives added to NGINX by the ngx_http_lua_module module.
No description provided by the author
No description provided by the author
ParseOptions determine the behavior of an NGINX config parse.
No description provided by the author
No description provided by the author
StringsCreator is an option for rendering config files to strings(s).
SubScanner provides an interface for scanning alternative grammars within NGINX configuration data.

# Interfaces

Builder is the interface implemented by types that can render a Directive as it appears in NGINX configuration files.
Creator abstracts file creation (to write configs to something other than files).
Lexer is an interface for implementing lexers that handle external NGINX tokens during the lexical analysis phase.
RegisterBuilder is an option that can be used to add a builder to build NGINX configuration for custom directives.
RegisterLexer is an option that cna be used to add a lexer to tokenize external NGINX tokens.

# Type aliases

No description provided by the author
MatchFunc is the signature of the match function used to identify NGINX directives that can be encountered when parsing an NGINX configuration that references dynamic or non-core modules.