README

confyg

A suitably generic form of the Go module configuration file parser.

GoDoc

Usage is simple:

type server struct {
	port string
	keys *crypto.Keypair
	db   *storm.DB
}

func (s *server) Allow(verb string, block bool) bool {
	switch verb {
	case "port":
		return !block
	case "dbfile":
		return !block
	case "keys":
		return !block
	}

	return false
}

func (s *server) Read(errs *bytes.Buffer, fs *confyg.FileSyntax, line *confyg.Line, verb string, args []string) {
	switch verb {
	case "port":
		_, err := strconv.Atoi(args[0])
		if err != nil {
			fmt.Fprintf(errs, "%s:%d value is not a number: %s: %v\n", fs.Name, line.Start.Line, args[0], err)
			return
		}

		s.port = args[0]

	case "dbfile":
		dbFile := args[0][1 : len(args[0])-1] // shuck off quotes

		db, err := storm.Open(dbFile)
		if err != nil {
			fmt.Fprintf(errs, "%s:%d failed to open storm database: %s: %v\n", fs.Name, line.Start.Line, args[0], err)
			return
		}

		s.db = db

	case "keys":
		kp := &crypto.Keypair{}

		pubk, err := hex.DecodeString(args[0])
		if err != nil {
			fmt.Fprintf(errs, "%s:%d invalid public key: %v\n", fs.Name, line.Start.Line, err)
			return
		}

		privk, err := hex.DecodeString(args[1])
		if err != nil {
			fmt.Fprintf(errs, "%s:%d invalid private key: %v\n", fs.Name, line.Start.Line, err)
			return
		}

		copy(kp.Public[:], pubk[0:32])
		copy(kp.Private[:], privk[0:32])

		s.keys = kp
	}
}

var (
	configFile = flag.String("cfg", "./apig.cfg", "apig config file location")
)

func main() {
	flag.Parse()

	data, err := ioutil.ReadFile(*configFile)
	if err != nil {
		log.Fatal(err)
	}

	s := &server{}
	_, err = confyg.Parse(*configFile, data, s, s)
	if err != nil {
		log.Fatal(err)
	}

	_ = s
}

Or use flagconfyg:

var (
  config = flag.Config("cfg", "", "if set, configuration file to load (see https://github.com/Xe/x/blob/master/docs/man/flagconfyg.5)")
)

func main() {
  flag.Parse()
  
  if *config != "" {
    flagconfyg.CmdParse(*config)
  }
}
Expand ▾ Collapse ▴

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(f *FileSyntax) []byte

Types

type Allower

type Allower interface {
	Allow(verb string, block bool) bool
}

    Allower defines if a given verb and block combination is valid for configuration parsing.

    If this is intended to be a statement-like verb, block should be set to false. If this is intended to be a block-like verb, block should be set to true.

    type AllowerFunc

    type AllowerFunc func(verb string, block bool) bool

      AllowerFunc implements Allower for inline definitions.

      func (AllowerFunc) Allow

      func (a AllowerFunc) Allow(verb string, block bool) bool

        Allow implements Allower.

        type Comment

        type Comment struct {
        	Start  Position
        	Token  string // without trailing newline
        	Suffix bool   // an end of line (not whole line) comment
        }

          A Comment represents a single // comment.

          type CommentBlock

          type CommentBlock struct {
          	Comments
          	Start Position
          }

            A CommentBlock represents a top-level block of comments separate from any rule.

            func (*CommentBlock) Span

            func (x *CommentBlock) Span() (start, end Position)

            type Comments

            type Comments struct {
            	Before []Comment // whole-line comments before this expression
            	Suffix []Comment // end-of-line comments after this expression
            
            	// For top-level expressions only, After lists whole-line
            	// comments following the expression.
            	After []Comment
            }

              Comments collects the comments associated with an expression.

              func (*Comments) Comment

              func (c *Comments) Comment() *Comments

                Comment returns the receiver. This isn't useful by itself, but a Comments struct is embedded into all the expression implementation types, and this gives each of those a Comment method to satisfy the Expr interface.

                type Expr

                type Expr interface {
                	// Span returns the start and end position of the expression,
                	// excluding leading or trailing comments.
                	Span() (start, end Position)
                
                	// Comment returns the comments attached to the expression.
                	// This method would normally be named 'Comments' but that
                	// would interfere with embedding a type of the same name.
                	Comment() *Comments
                }

                  An Expr represents an input element.

                  type FileSyntax

                  type FileSyntax struct {
                  	Name string // file path
                  	Comments
                  	Stmt []Expr
                  }

                    A FileSyntax represents an entire go.mod file.

                    func Parse

                    func Parse(file string, data []byte, r Reader, al Allower) (*FileSyntax, error)

                    func (*FileSyntax) Span

                    func (x *FileSyntax) Span() (start, end Position)

                    type LParen

                    type LParen struct {
                    	Comments
                    	Pos Position
                    }

                      An LParen represents the beginning of a parenthesized line block. It is a place to store suffix comments.

                      func (*LParen) Span

                      func (x *LParen) Span() (start, end Position)

                      type Line

                      type Line struct {
                      	Comments
                      	Start Position
                      	Token []string
                      	End   Position
                      }

                        A Line is a single line of tokens.

                        func (*Line) Span

                        func (x *Line) Span() (start, end Position)

                        type LineBlock

                        type LineBlock struct {
                        	Comments
                        	Start  Position
                        	LParen LParen
                        	Token  []string
                        	Line   []*Line
                        	RParen RParen
                        }

                          A LineBlock is a factored block of lines, like

                          require (
                          	"x"
                          	"y"
                          )
                          

                          func (*LineBlock) Span

                          func (x *LineBlock) Span() (start, end Position)

                          type MapConfig

                          type MapConfig map[string][]string

                            MapConfig is a simple wrapper around a map.

                            func (MapConfig) Allow

                            func (mc MapConfig) Allow(verb string, block bool) bool

                              Allow accepts everything.

                              func (MapConfig) Read

                              func (mc MapConfig) Read(errs *bytes.Buffer, fs *FileSyntax, line *Line, verb string, args []string)

                              type Position

                              type Position struct {
                              	Line     int // line in input (starting at 1)
                              	LineRune int // rune in line (starting at 1)
                              	Byte     int // byte in input (starting at 0)
                              }

                                A Position describes the position between two bytes of input.

                                type RParen

                                type RParen struct {
                                	Comments
                                	Pos Position
                                }

                                  An RParen represents the end of a parenthesized line block. It is a place to store whole-line (before) comments.

                                  func (*RParen) Span

                                  func (x *RParen) Span() (start, end Position)

                                  type Reader

                                  type Reader interface {
                                  	Read(errs *bytes.Buffer, fs *FileSyntax, line *Line, verb string, args []string)
                                  }

                                    Reader is called when individual lines of the configuration file are being read. This is where you should populate any relevant structures with information.

                                    If something goes wrong in the file parsing step, add data to the errs buffer describing what went wrong.

                                    type ReaderFunc

                                    type ReaderFunc func(errs *bytes.Buffer, fs *FileSyntax, line *Line, verb string, args []string)

                                      ReaderFunc implements Reader for inline definitions.

                                      func (ReaderFunc) Read

                                      func (r ReaderFunc) Read(errs *bytes.Buffer, fs *FileSyntax, line *Line, verb string, args []string)

                                      Directories

                                      Path Synopsis
                                      Package flagconfyg is a hack around confyg.
                                      Package flagconfyg is a hack around confyg.