ast

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package ast provides the abstract syntax tree, parser, and everything else in puppetfileparser

Package ast provides the abstract syntax tree, parser, and everything else in puppetfileparser

Package ast provides the abstract syntax tree, parser, and everything else in puppetfileparser

Index

Constants

This section is empty.

Variables

View Source
var PuppetfileLexer = lexer.Must(regex.New(`
	Keyword = ^mod
	Forge = ^forge
	String = '([^']*)'
	Int = \d
	Char = [[:alpha:]]
	Ident = :[A-Za-z0-9_]+
	Comment = #.*
	Punct = [,@:.]
	Assign = (=>)
	Whitespace = [\s\t]+
`))

PuppetfileLexer is a custom regex lexer for Puppetfiles

View Source
var ReAssignment = regexp.MustCompile(`(?P<Key>:[A-Za-z0-9_]+)\s?=?>?\s?'?(?P<Value>[^\s=>']*)'?`)

ReAssignment is a compiled regular expression for Module properties with assignements. ReAssignment matches should return two named groups, Key and Value.

View Source
var ReComment = regexp.MustCompile(`#.*`)

ReComment is a compiled regular expression for Puppetfile comments

View Source
var ReForgeURL = regexp.MustCompile(`forge ["']([^"']*)["']`)

ReForgeURL is a compiled regular expression for extracting a Forge URL from a forge line in a Puppetfile.

View Source
var ReGitData = regexp.MustCompile(`(?P<Proto>https?|(?:ssh:\/\/)?git)(?::\/\/.*|@.*)`)

ReGitData is a compiled regular expression for parsing Modules' :git property value. ReGitData matches should return a named group Proto. Proto is the protocol used by git. If Proto is "git" or "ssh://git", this means SSH is to be used. The full match is the URI. ReGitData does not validate URLs.

View Source
var ReIdent = regexp.MustCompile(`:[A-Za-z0-9_]+`)

ReIdent is a compiled regular expression for Puppetfile symbols

View Source
var ReKeyword = regexp.MustCompile(`^mod`)

ReKeyword is a compiled regular expression for the "mod" keyword

View Source
var ReMeta = regexp.MustCompile(`#\s?@(?P<Tag>[\w_-]+):\s*(?P<Data>.*)`)

ReMeta is a compiled regular expression for Puppetfile meta tags and data. ReMeta matches should return two named capture groups, Tag and Data.

View Source
var ReMetaData = regexp.MustCompile(`#\s?@[\w_-]+:\s*(?P<Data>.*)`)

ReMetaData is a compiled regular expression for Puppetfile meta data

View Source
var ReMetaTag = regexp.MustCompile(`#\s?@(?P<Tag>[\w_-]+):.*`)

ReMetaTag is a compiled regular expression for Puppetfile metadata

View Source
var ReString = regexp.MustCompile(`'([^']*)'`)

ReString is a compiled regular expression for Puppetfile strings

View Source
var ReValidIdent = regexp.MustCompile(`(:latest|:git|:install_path|:ref|:branch|:tag|:commit|:default_branch|:install_path)`)

ReValidIdent is a compiled regular expression used to validate Idents in the Puppetfile.

Functions

func DataFromString

func DataFromString(input string) string

DataFromString parses a given string and returns metadata if present

func DummyPos

func DummyPos() lexer.Position

DummyPos returns a lexer.Position with fake data

func MapNamedCaptureGroups

func MapNamedCaptureGroups(re *regexp.Regexp, input string) map[string]string

MapNamedCaptureGroups returns a map of named regex capture groups where map[<group name>] = match Param re: A compiled regular expression Param input: A string to evaluate with the regular expression

func NewParser

func NewParser() (*participle.Parser, error)

NewParser creates and returns a Puppetfile parser

func SingleMatch

func SingleMatch(re *regexp.Regexp, input string) string

SingleMatch returns the first matching capture group. Useful for when you only want to return one value from a match. Param re: A compiled regular expression Param inputL A string to evaluate with the regular expression

func TagFromString

func TagFromString(input string) string

TagFromString parses a given string and returns a meta tag if present

Types

type ByName

type ByName []*Module

ByName implements sort.Interface base on the Module struct's Name field

func (ByName) Len

func (a ByName) Len() int

func (ByName) Less

func (a ByName) Less(i, j int) bool

func (ByName) Swap

func (a ByName) Swap(i, j int)

type Comment

type Comment struct {
	Pos  lexer.Position
	Text string `@Comment`
}

Comment holds the text and lexer position of a comment in the AST

func (Comment) Checksum

func (c Comment) Checksum() [16]byte

Checksum returns the md5 checksum of the Text field

func (*Comment) Data

func (c *Comment) Data() string

Data returns the metadata data in the comment text, if present

func (*Comment) MetaPair

func (c *Comment) MetaPair() (MetaPair, error)

MetaPair returns a MetaPair if the Comment's text contains one

func (*Comment) Tag

func (c *Comment) Tag() string

Tag returns the metadata tag in the comment text, if present

type Forge

type Forge struct {
	URL string `Forge @String`
}

Forge holds a Forge declaration

func (Forge) Checksum

func (f Forge) Checksum() [16]byte

Checksum returns an md5 checksum of the Sprint() output

func (*Forge) Sprint

func (f *Forge) Sprint() string

Sprint returns a string representation of type

type MetaPair

type MetaPair struct {
	Tag  string
	Data string
}

MetaPair holds the actual metadata, all tags with that metadata, and all modules tagged with that metadata.

func PairFromString

func PairFromString(input string) (MetaPair, error)

PairFromString parses a given string and returns a MetaPair, if possible

func (*MetaPair) Sprint

func (m *MetaPair) Sprint() string

Sprint returns a string of the MetaPair

type Metadata

type Metadata struct {
	MetaPairs []MetaPair
}

Metadata holds an array of MetaPair objects

func (*Metadata) AllTags

func (m *Metadata) AllTags() []string

AllTags returns a sorted array of all tag strings

func (*Metadata) DataExists

func (m *Metadata) DataExists(data string) ([]int, bool)

DataExists checks if data exists in Metadata and returns the an array of indexs of the data MetaPairs and a boolean if it exists or not. The index return value of non existent data is -1.

func (*Metadata) SearchByData

func (m *Metadata) SearchByData(data string) []MetaPair

SearchByData returns all metapairs with the specified data or nil

func (*Metadata) SearchByTag

func (m *Metadata) SearchByTag(tag string) []MetaPair

SearchByTag returns all metapairs with the specified tag or nil

func (*Metadata) Sprint

func (m *Metadata) Sprint() string

Sprint returns a string representation of the object

func (*Metadata) TagExists

func (m *Metadata) TagExists(tag string) ([]int, bool)

TagExists checks if a tag exists in Metadata and returns the an array of indexs of the tag MetaPairs and a boolean if it exists or not. The index return value of a non existent tag is -1.

type Module

type Module struct {
	Pos        lexer.Position
	Name       string      `Keyword @String","?`
	Properties []*Property `( @@ )*`
}

Module Struct that holds each Module and related properties

func (*Module) AddProperties

func (m *Module) AddProperties(props []string)

AddProperties accepts strings in the form of "key=>value", parses them into Property objects, and adds them to the module. Key must have a ":" prefixing it, just like in a Puppetfile.

func (*Module) AddProperty

func (m *Module) AddProperty(prop string)

AddProperty accepts a string and adds it as a property to the module. AddProperty is used to add special properties (bare version string / :latest symbol)

func (Module) Checksum

func (m Module) Checksum() [16]byte

Checksum returns an md5 checksum of the Sprint() output

func (Module) ChecksumName

func (m Module) ChecksumName() [16]byte

ChecksumName returns an md5 checksum of the Module Name

func (Module) ChecksumProperties

func (m Module) ChecksumProperties() [][][16]byte

ChecksumProperties returns an array of [k, v] arrays where k = Property Key field checksum and v= Property Value field checksum.

func (*Module) EditProperty

func (m *Module) EditProperty(key, value string)

EditProperty overwrites the value of an existing property

func (Module) GetProperty

func (m Module) GetProperty(key string) *Property

GetProperty returns a pointer to a Property of the module, if the Module has said property. GetProperty performs this search by Property key.

func (Module) GetPropertyValue

func (m Module) GetPropertyValue(name string) string

GetPropertyValue accepts a string of a param name and returns the associated value if present. As Puppetfiles are distinctly idomatic, there are some inputs that can potentially match several values: Input "version": Returns a bare version string, the :latest

symbol as a string, :tag, or :ref if :ref is a semver.

Input "branch": Returns :branch, :default_branch, or :ref

if :ref is a git branch.

func (*Module) NewBareProperty

func (m *Module) NewBareProperty(value string)

NewBareProperty overwrites all other properties in a module with a single bare property.

func (*Module) Sprint

func (m *Module) Sprint() string

Sprint returns string representation of type

type ModuleMetadata

type ModuleMetadata struct {
	Name     string
	Metadata Metadata
}

ModuleMetadata holds the module name and all the metadata assigned to the module.

func (*ModuleMetadata) SearchByData

func (m *ModuleMetadata) SearchByData(data string) []MetaPair

SearchByData returns all metapairs with the specified data or nil

func (*ModuleMetadata) SearchByTag

func (m *ModuleMetadata) SearchByTag(tag string) []MetaPair

SearchByTag returns all metapairs with the specified tag or nil

type Property

type Property struct {
	Pos   lexer.Position
	Key   *Value `@@ `
	Value *Value `( Assign @@ )?(",")?`
}

Property Struct that hold key-value properties pairs

func (Property) Checksum

func (p Property) Checksum() [16]byte

Checksum returns an md5 checksum of the Sprint() output

func (Property) ChecksumKey

func (p Property) ChecksumKey() [16]byte

ChecksumKey returns the md5 checksum of the Key field

func (Property) ChecksumValue

func (p Property) ChecksumValue() [16]byte

ChecksumValue returns the md5 checksum of the Value field

func (*Property) OverwriteValue

func (p *Property) OverwriteValue(val string)

OverwriteValue creates a new *Value adds it to the Property Value field.

func (*Property) Sprint

func (p *Property) Sprint() string

Sprint returns string representation of type

type Puppetfile

type Puppetfile struct {
	Forge               *Forge       `( @@ )?`
	Statements          []*Statement `{ @@ }`
	Metadata            Metadata
	ModuleMetadata      []ModuleMetadata
	TopBlockComments    []*Statement
	BottomBlockComments []*Statement
	ModuleVersionMap    map[string]string
}

Puppetfile Struct that holds entire Puppetfile

func Parse

func Parse(text string) (*Puppetfile, error)

Parse parses a Puppetfile and returns the parsed AST

func (*Puppetfile) AddComment

func (p *Puppetfile) AddComment(location, text string) error

AddComment adds a Comment to either the top or bottom block comments

func (*Puppetfile) AddModule

func (p *Puppetfile) AddModule(slug string, props []string) error

AddModule adds a module and its properties to the Puppetfile

func (*Puppetfile) AddModuleMetadata

func (p *Puppetfile) AddModuleMetadata(name string, tag string, data string) error

AddModuleMetadata adds a new metadata statement above a module in the Puppetfile

func (*Puppetfile) AddStatement

func (p *Puppetfile) AddStatement(s *Statement) error

AddStatement appends a Statement the Puppetfiles Statements property and sorts Statements

func (*Puppetfile) AddStatementAbove

func (p *Puppetfile) AddStatementAbove(idx int, s *Statement, stmts []*Statement) []*Statement

AddStatementAbove adds a Statement to a slice of Statements above the given index

func (Puppetfile) Checksum

func (p Puppetfile) Checksum() [16]byte

Checksum returns an md5 checksum of the stringified contents of Puppetfile

func (Puppetfile) GetModule

func (p Puppetfile) GetModule(name string) *Module

GetModule returns a Module struct by name

func (Puppetfile) HasModule

func (p Puppetfile) HasModule(name string) (bool, int)

HasModule returns true if the named module exists in the Puppetfile

func (*Puppetfile) ParseMetadata

func (p *Puppetfile) ParseMetadata() error

ParseMetadata goes through all statements in the Puppetfile and makes metadata to module associations. It then populates the module's Metadata field.

func (*Puppetfile) RemoveComments

func (p *Puppetfile) RemoveComments(stmts []*Statement) []*Statement

RemoveComments removes all Statements from the Puppetfile that have a Comment

func (*Puppetfile) RenameModule

func (p *Puppetfile) RenameModule(name, new string) error

RenameModule renames a module in the Puppetfile while keeping the module's properties the same. The Puppetfile is then sorted by name.

func (*Puppetfile) SearchMetaByModuleName

func (p *Puppetfile) SearchMetaByModuleName(name string) string

SearchMetaByModuleName returns a string of all metadata for the given module name

func (*Puppetfile) SearchModulesByMetaData

func (p *Puppetfile) SearchModulesByMetaData(data string) []string

SearchModulesByMetaData returns a slice of module name strings that have the given metadata associated with them.

func (*Puppetfile) SearchModulesByMetaTag

func (p *Puppetfile) SearchModulesByMetaTag(tag string) []string

SearchModulesByMetaTag returns a slice of module name strings that have the given tag associated with them.

func (*Puppetfile) SortByName

func (p *Puppetfile) SortByName() error

SortByName reorders all Statements to be sorted alphabetically by name. Comment statements are reordered based on their original positions relative to the modules below them in the Puppetfile.

func (*Puppetfile) Sprint

func (p *Puppetfile) Sprint() string

Sprint returns string representation of type

type Statement

type Statement struct {
	Pos     lexer.Position
	Module  *Module  `@@`
	Comment *Comment `| @@`
}

Statement holds statements that make up the Puppetfile, either modules or comments

func DummyComment

func DummyComment() *Statement

DummyComment returns a Statement pointer with a dummy Comment field

func DummyModule

func DummyModule() *Statement

DummyModule returns a Statement pointer with a dummy Module field

func DummyStatement

func DummyStatement() *Statement

DummyStatement returns a Statement pointer with nil fields

func (Statement) Checksum

func (s Statement) Checksum() [16]byte

Checksum returns an md5 checksum of the Sprint() output

func (*Statement) Sprint

func (s *Statement) Sprint() string

Sprint returns string representation of type

type StmtByName

type StmtByName []*Statement

StmtByName implements sort.Interface base on slice of Statements base on Module field's Name field

func (StmtByName) Len

func (a StmtByName) Len() int

func (StmtByName) Less

func (a StmtByName) Less(i, j int) bool

func (StmtByName) Swap

func (a StmtByName) Swap(i, j int)

type Value

type Value struct {
	Pos    lexer.Position
	String string `@String`
	Ident  string `| @Ident`
}

Value Struct that holds the types that a value can be

func (Value) Checksum

func (v Value) Checksum() [16]byte

Checksum returns an md5 checksum of the String or Ident contained in the value.

func (*Value) Sprint

func (v *Value) Sprint() string

Sprint returns string representation of type

Jump to

Keyboard shortcuts

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