metadata

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

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

Go to latest
Published: Sep 24, 2015 License: MIT Imports: 7 Imported by: 1

README

metadata-parser

A Chef cookbook metadata dependency parser lib for Golang

What, WHY?

Because although the chef-server understands and spews metadata as JSON all of the userspace tooling speaks the ruby form (see knife). That ruby form is what everyone puts in cookbooks, and in almost all cases the .json form isn't checked into repos :|

I needed a way to dependably parse name,version, and dependency info from cookbook metadata. I could have regexed, but I was interested in writing a toke-parser in Golang

Usage

Using this is pretty simple, just create a new parser from any io.Reader, and Parse().

meta, err := metadata.NewParser(strings.NewReader(metadata)).Parse()
if err != nil {
  fmt.Println(" Error parsing: ", err)
  t.Fail()
}

fmt.Println("Name: ", meta.Name)
fmt.Println("Version:", meta.Version)
for i, dep := range(meta.Depends) {
  fmt.Printf("\tdepends: %s@%s\n", dep.Name, dep.Constraint)
}

Contributing

Please contribute and help improve this project!

  • Fork the repo
  • Make sure the tests pass
  • Improve the code
  • Make sure your feature has test coverage
  • Make sure the tests pass
  • Submit a pull request

Thanks

Thanks to the InfluxDB team for MIT licensing their sql parser. Much of this code is derived from their impelemtnation: https://github.com/influxdb/influxdb Also thanks to GopherAcademy for this awesome article on writing lexers and parsers in Go: http://blog.gopheracademy.com/advent-2014/parsers-lexers/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ScanBareIdent

func ScanBareIdent(r io.RuneScanner) string

ScanBareIdent reads bare identifier from a rune reader.

func ScanDelimited

func ScanDelimited(r io.RuneScanner, start, end rune, escapes map[rune]rune, escapesPassThru bool) ([]byte, error)

func ScanString

func ScanString(r io.RuneScanner) (string, error)

ScanString reads a quoted string from a rune reader.

Types

type Dependency

type Dependency struct {
	Name       string
	Constraint version.Constraints // ... maybe look at hashi version.Constraint
}

type Metadata

type Metadata struct {
	Depends []Dependency
	Name    string
	Version version.Version
}

Metadata represents a coookbook metadata file

type Parser

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

Parser represents a parser.

func NewParser

func NewParser(r io.Reader) *Parser

NewParser returns a new instance of Parser.

func (*Parser) Parse

func (p *Parser) Parse() (md *Metadata, err error)

Parse parses a metadata file

type Pos

type Pos struct {
	Line int
	Char int
}

Pos specifies the line and character position of a token. The Char and Line are both zero-based indexes.

type Scanner

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

Scanner represents a lexical scanner

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner returns a new instance of Scanner.

func (*Scanner) Scan

func (s *Scanner) Scan() (tok Token, pos Pos, lit string)

Scan returns the next token and position from the underlying reader. Also returns the literal text read for strings, numbers, and duration tokens since these token types can have different literal representations.

type Token

type Token int
const (
	// Special tokens
	ILLEGAL Token = iota
	EOF
	WS

	// operators
	ADD // +
	SUB // -
	MUL // *
	DIV // /

	// Verion Specifiers
	EQ  // =
	LT  // <
	LTE // <=
	GT  // >
	GTE // >=
	PGT // ~> the pessimistic gt

	// Literals
	IDENT
	STRING
	BADSTRING
	BADESCAPE
	TRUE
	FALSE

	// Misc Characters
	COMMENT
	COMMA // ,

	//Keywords
	VERSION
	NAME
	DEPENDS
)

func Lookup

func Lookup(ident string) Token

Lookup returns the token associated with a given string.

func (Token) String

func (tok Token) String() string

String returns the string representation of the token.

Jump to

Keyboard shortcuts

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