editor

package
v0.2.15 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2024 License: MIT Imports: 12 Imported by: 11

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeriveFile added in v0.2.0

func DeriveFile(filename string, w io.Writer, sink Sink) error

DeriveFile is a helper method which builds an DeriveOperator from a given sink and applies it to a single file. The outputs are written to stream.

func DeriveStream added in v0.2.0

func DeriveStream(r io.Reader, w io.Writer, filename string, sink Sink) error

DeriveStream is a helper method which builds a DeriveOperator from a given sink and applies it to stream. Note that a filename is used only for an error message.

func EditStream added in v0.2.0

func EditStream(r io.Reader, w io.Writer, filename string, filter Filter) error

EditStream is a helper method which builds an EditorOperator from a given filter and applies it to stream. Note that a filename is used only for an error message.

func GetAttributeValueAsString added in v0.2.4

func GetAttributeValueAsString(attr *hclwrite.Attribute, withComments bool) (string, error)

GetAttributeValueAsString returns a value of Attribute as string. There is no way to get value as string directly, so we parses tokens of Attribute and build string representation.

func ReadFile added in v0.2.0

func ReadFile(filename string, w io.Writer, filter Filter) error

ReadFile is a helper method which builds an EditorOperator from a given filter and applies it to a single file. The outputs are written to stream.

func UpdateFile added in v0.2.0

func UpdateFile(filename string, filter Filter) error

UpdateFile is a helper method which builds an EditorOperator from a given filter and applies it to a single file. The outputs are written to the input file in-place.

func VerticalFormat

func VerticalFormat(tokens hclwrite.Tokens) hclwrite.Tokens

VerticalFormat formats token in vertical.

Types

type AttributeAppendFilter added in v0.2.0

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

AttributeAppendFilter is a filter implementation for appending attribute.

func (*AttributeAppendFilter) Filter added in v0.2.0

func (f *AttributeAppendFilter) Filter(inFile *hclwrite.File) (*hclwrite.File, error)

Filter reads HCL and appends a new attribute to a given address. If a matched block not found, nothing happens. If the given attribute already exists, it returns an error. If a newline flag is true, it also appends a newline before the new attribute.

type AttributeGetSink added in v0.2.0

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

AttributeGetSink is a sink implementation for getting a value of attribute.

func (*AttributeGetSink) Sink added in v0.2.0

func (s *AttributeGetSink) Sink(inFile *hclwrite.File) ([]byte, error)

Sink reads HCL and writes value of attribute.

type AttributeRemoveFilter added in v0.2.0

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

AttributeRemoveFilter is a filter implementation for removing attribute.

func (*AttributeRemoveFilter) Filter added in v0.2.0

func (f *AttributeRemoveFilter) Filter(inFile *hclwrite.File) (*hclwrite.File, error)

Filter reads HCL and remove a matched attribute at a given address.

type AttributeSetFilter added in v0.2.0

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

AttributeSetFilter is a filter implementation for setting attribute.

func (*AttributeSetFilter) Filter added in v0.2.0

func (f *AttributeSetFilter) Filter(inFile *hclwrite.File) (*hclwrite.File, error)

Filter reads HCL and updates a value of matched an attribute at a given address.

type BlockAppendFilter added in v0.2.0

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

BlockAppendFilter is a filter implementation for appending block.

func (*BlockAppendFilter) Filter added in v0.2.0

func (f *BlockAppendFilter) Filter(inFile *hclwrite.File) (*hclwrite.File, error)

Filter reads HCL and appends only matched blocks at a given address. The child address is relative to parent one. If a newline flag is true, it also appends a newline before the new block.

type BlockGetFilter added in v0.2.0

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

BlockGetFilter is a filter implementation for getting block.

func (*BlockGetFilter) Filter added in v0.2.0

func (f *BlockGetFilter) Filter(inFile *hclwrite.File) (*hclwrite.File, error)

Filter reads HCL and writes only matched blocks at a given address.

type BlockListSink added in v0.2.0

type BlockListSink struct{}

BlockListSink is a sink implementation for getting a list of block addresses.

func (*BlockListSink) Sink added in v0.2.0

func (s *BlockListSink) Sink(inFile *hclwrite.File) ([]byte, error)

Sink reads HCL and writes a list of block addresses.

type BlockRemoveFilter added in v0.2.0

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

BlockRemoveFilter is a filter implementation for removing block.

func (*BlockRemoveFilter) Filter added in v0.2.0

func (f *BlockRemoveFilter) Filter(inFile *hclwrite.File) (*hclwrite.File, error)

Filter reads HCL and removes only matched blocks at a given address.

type BlockRenameFilter added in v0.2.0

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

BlockRenameFilter is a filter implementation for renaming block.

func (*BlockRenameFilter) Filter added in v0.2.0

func (f *BlockRenameFilter) Filter(inFile *hclwrite.File) (*hclwrite.File, error)

Filter reads HCL and renames matched blocks at a given address. The blocks which do not match the from address are output as is. Rename means setting the block type and labels corresponding to the new address. changing the block type does not make sense on an application context, but filters can chain to others and the later filter may edit its attributes. So we allow this filter to any block type and labels.

type BodyGetFilter added in v0.2.0

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

BodyGetFilter is a filter implementation for getting body of first matched block.

func (*BodyGetFilter) Filter added in v0.2.0

func (f *BodyGetFilter) Filter(inFile *hclwrite.File) (*hclwrite.File, error)

Filter reads HCL and writes body of first matched block at a given address.

type Client added in v0.2.0

type Client interface {
	// Edit reads a HCL file and appies a given filter.
	// If filename is `-`, reads the input from stdin.
	// If update is true, the outputs is written to the input file, else to stdout.
	Edit(filename string, update bool, filter Filter) error
	// Derive reads a HCL file and appies a given sink.
	// If filename is `-`, reads the input from stdin.
	// The outputs is always written to stdout.
	Derive(filename string, sink Sink) error
}

Client is an interface for the entrypoint of editor package

func NewClient added in v0.2.0

func NewClient(o *Option) Client

NewClient creates a new instance of Client.

type DefaultFormatter added in v0.2.0

type DefaultFormatter struct {
}

DefaultFormatter is a default Formatter implementation for formatting HCL.

func (*DefaultFormatter) Format added in v0.2.0

func (f *DefaultFormatter) Format(inFile *hclwrite.File) ([]byte, error)

Format reads HCL, formats tokens and writes formatted contents.

type DeriveOperator added in v0.2.0

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

DeriveOperator is an implementation of Operator for deriving any bytes from HCL.

func (*DeriveOperator) Apply added in v0.2.0

func (o *DeriveOperator) Apply(input []byte, filename string) ([]byte, error)

Apply reads an input bytes, applies a given sink for deriving, and writes output. The input contains arbitrary bytes in HCL, and the output contains arbitrary bytes in non-HCL. Note that a filename is used only for an error message.

type EditOperator added in v0.2.0

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

EditOperator is an implementation of Operator for editing HCL.

func (*EditOperator) Apply added in v0.2.0

func (o *EditOperator) Apply(input []byte, filename string) ([]byte, error)

Apply reads input bytes, applies some filters and formatter, and writes output. The input and output contain arbitrary bytes in HCL. Note that a filename is used only for an error message.

type Filter

type Filter interface {
	// Filter reads HCL and writes HCL
	Filter(*hclwrite.File) (*hclwrite.File, error)
}

Filter is an interface which reads HCL and writes HCL

func NewAttributeAppendFilter added in v0.2.0

func NewAttributeAppendFilter(address string, value string, newline bool) Filter

NewAttributeAppendFilter creates a new instance of AttributeAppendFilter.

func NewAttributeRemoveFilter added in v0.2.0

func NewAttributeRemoveFilter(address string) Filter

NewAttributeRemoveFilter creates a new instance of AttributeRemoveFilter.

func NewAttributeSetFilter added in v0.2.0

func NewAttributeSetFilter(address string, value string) Filter

NewAttributeSetFilter creates a new instance of AttributeSetFilter.

func NewBlockAppendFilter added in v0.2.0

func NewBlockAppendFilter(parent string, child string, newline bool) Filter

NewBlockAppendFilter creates a new instance of BlockAppendFilter.

func NewBlockGetFilter added in v0.2.0

func NewBlockGetFilter(address string) Filter

NewBlockGetFilter creates a new instance of BlockGetFilter.

func NewBlockRemoveFilter added in v0.2.0

func NewBlockRemoveFilter(address string) Filter

NewBlockRemoveFilter creates a new instance of BlockRemoveFilter.

func NewBlockRenameFilter added in v0.2.0

func NewBlockRenameFilter(from string, to string) Filter

NewBlockRenameFilter creates a new instance of BlockRenameFilter.

func NewBodyGetFilter added in v0.2.0

func NewBodyGetFilter(address string) Filter

NewBodyGetFilter creates a new instance of BodyGetFilter.

func NewFormatterFilter added in v0.2.0

func NewFormatterFilter() Filter

NewFormatterFilter creates a new instance of FormatterFilter.

func NewMultiFilter added in v0.2.0

func NewMultiFilter(filters []Filter) Filter

NewMultiFilter creates a new instance of MultiFilter.

type Formatter added in v0.2.0

type Formatter interface {
	// Format reads HCL, formats tokens and writes bytes.
	Format(*hclwrite.File) ([]byte, error)
}

Formatter is an interface which reads HCL, formats tokens and writes bytes. Formatter has a signature similar to Sink, but they have different features, so we distinguish them with types.

func NewDefaultFormatter added in v0.2.0

func NewDefaultFormatter() Formatter

NewDefaultFormatter creates a new instance of DefaultFormatter.

type FormatterFilter added in v0.2.0

type FormatterFilter struct {
}

FormatterFilter is a Filter implementation which applies the default formatter as a Filter.

func (*FormatterFilter) Filter added in v0.2.0

func (f *FormatterFilter) Filter(inFile *hclwrite.File) (*hclwrite.File, error)

Filter applies the default formatter as a Filter.

type MultiFilter added in v0.2.0

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

MultiFilter is a Filter implementation which applies multiple filters in sequence.

func (*MultiFilter) Filter added in v0.2.0

func (f *MultiFilter) Filter(inFile *hclwrite.File) (*hclwrite.File, error)

Filter applies multiple filters in sequence.

type Operator added in v0.2.0

type Operator interface {
	// Apply reads input bytes, apply some operations, and writes outputs.
	// The input and output contain arbitrary bytes (maybe HCL or not).
	// Note that a filename is used only for an error message.
	Apply(input []byte, filename string) ([]byte, error)
}

Operator is an interface which abstracts stream operations. The hcledit provides not only operations for editing HCL, but also for deriving such as listing. They need similar but different implementations.

func NewDeriveOperator added in v0.2.0

func NewDeriveOperator(sink Sink) Operator

NewDeriveOperator creates a new instance of operator for deriving any bytes from HCL.

func NewEditOperator added in v0.2.0

func NewEditOperator(filter Filter) Operator

NewEditOperator creates a new instance of operator for editing HCL. If you want to apply multiple filters, use the MultiFilter to compose them.

type Option added in v0.2.0

type Option struct {
	// InStream is the stdin stream.
	InStream io.Reader
	// OutStream is the stdout stream.
	OutStream io.Writer
	// ErrStream is the stderr stream.
	ErrStream io.Writer
}

Option is a set of options for Client.

type ParserSource added in v0.2.0

type ParserSource struct {
}

ParserSource is a Source implementation for parsing HCL.

func (*ParserSource) Source added in v0.2.0

func (s *ParserSource) Source(src []byte, filename string) (*hclwrite.File, error)

Source parses HCL and returns *hclwrite.File filename is a metadata of input stream and used only for an error message.

type Sink

type Sink interface {
	// Sink reads HCL and writes bytes.
	Sink(*hclwrite.File) ([]byte, error)
}

Sink is an interface which reads HCL and writes bytes.

func NewAttributeGetSink added in v0.2.0

func NewAttributeGetSink(address string, withComments bool) Sink

NewAttributeGetSink creates a new instance of AttributeGetSink.

func NewBlockListSink added in v0.2.0

func NewBlockListSink() Sink

NewBlockListSink creates a new instance of BlockListSink.

type Source

type Source interface {
	// Source parses HCL and returns *hclwrite.File
	// filename is a metadata of input stream and used only for an error message.
	Source(src []byte, filename string) (*hclwrite.File, error)
}

Source is an interface which reads string and writes HCL

func NewParserSource added in v0.2.0

func NewParserSource() Source

NewParserSource creates a new instance of ParserSource.

Jump to

Keyboard shortcuts

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