crossplane

package module
v0.0.0-...-f987c53 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2021 License: Apache-2.0 Imports: 10 Imported by: 12

README

go-crossplane

An unofficial 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/aluttik/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/aluttik/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())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(w io.Writer, config Config, options *BuildOptions) error

Build creates an NGINX config from a crossplane.Config.

func BuildFiles

func BuildFiles(payload Payload, dir string, options *BuildOptions) error

BuildFiles builds all of the config files in a crossplane.Payload and writes them to disk.

Types

type BuildOptions

type BuildOptions struct {
	Indent int
	Tabs   bool
	Header bool
}

type Config

type Config struct {
	File   string        `json:"file"`
	Status string        `json:"status"`
	Errors []ConfigError `json:"errors"`
	Parsed []Directive   `json:"parsed"`
}

type ConfigError

type ConfigError struct {
	Line  *int   `json:"line"`
	Error string `json:"error"`
}

type Directive

type Directive struct {
	Directive string       `json:"directive"`
	Line      int          `json:"line"`
	Args      []string     `json:"args"`
	Includes  *[]int       `json:"includes,omitempty"`
	Block     *[]Directive `json:"block,omitempty"`
	Comment   *string      `json:"comment,omitempty"`
}

func (Directive) IsBlock

func (d Directive) IsBlock() bool

IsBlock returns true if this is a block directive.

func (Directive) IsComment

func (d Directive) IsComment() bool

IsComment returns true iff the directive represents a comment.

func (Directive) IsInclude

func (d Directive) IsInclude() bool

IsInclude returns true if this is an include directive.

type ParseError

type ParseError struct {
	// contains filtered or unexported fields
}

func (ParseError) Error

func (e ParseError) Error() string

type ParseOptions

type ParseOptions struct {
	// If true, parsing will stop immediately if an error is found.
	StopParsingOnError bool

	// An array of directives to skip over and not include in the payload.
	IgnoreDirectives []string

	// If true, include directives are used to combine all of the Payload's
	// Config structs into one.
	CombineConfigs bool

	// If true, only the config file with the given filename will be parsed
	// and Parse will not parse files included files.
	SingleFile bool

	// If true, comments will be parsed and added to the resulting Payload.
	ParseComments bool

	// If true, add an error to the payload when encountering a directive that
	// is unrecognized. The unrecognized directive will not be included in the
	// resulting Payload.
	ErrorOnUnknownDirectives bool

	// If true, checks that directives are in valid contexts.
	SkipDirectiveContextCheck bool

	// If true, checks that directives have a valid number of arguments.
	SkipDirectiveArgsCheck bool

	// If an error is found while parsing, it will be passed to this callback
	// function. The results of the callback function will be set in the
	// PayloadError struct that's added to the Payload struct's Errors array.
	ErrorCallback func(error) interface{}

	// If specified, use this alternative to open config files
	Open func(path string) (io.Reader, error)
}

ParseOptions determine the behavior of an NGINX config parse.

type Payload

type Payload struct {
	Status string         `json:"status"`
	Errors []PayloadError `json:"errors"`
	Config []Config       `json:"config"`
}

func Parse

func Parse(filename string, options *ParseOptions) (*Payload, error)

Parse parses an NGINX configuration file.

func (Payload) Combined

func (p Payload) Combined() (*Payload, error)

Combined returns a new Payload that is the same except that the inluding logic is performed on its configs. This means that the resulting Payload will always have 0 or 1 configs in its Config field.

type PayloadError

type PayloadError struct {
	File     string      `json:"file"`
	Line     *int        `json:"line"`
	Error    string      `json:"error"`
	Callback interface{} `json:"callback,omitempty"`
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL