Skip to content

Commit

Permalink
refresh from upstream, file closer added and relative subdirectory su…
Browse files Browse the repository at this point in the history
…pport added for include
  • Loading branch information
jeevatkm committed Mar 22, 2017
2 parents a32f78d + c9dd35b commit 0c0b73e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ import (
)

// Version represent current release version of forge.
var Version = "0.4.3"
var Version = "0.4.4"

// ParseBytes takes a []byte representation of the config file, parses it
// and responds with `*Section` and potentially an `error` if it cannot
Expand Down
9 changes: 2 additions & 7 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ func NewParser(reader io.Reader) *Parser {
// NewFileParser will create and initialize a new Parser from a provided from a filename string
func NewFileParser(filename string) (*Parser, error) {
reader, err := os.Open(filename)
defer func() {
_ = reader.Close()
}()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -240,8 +237,9 @@ func (parser *Parser) parseInclude() error {
return parser.syntaxError(msg)
}

// if it is not absolute path, resolve to relative from parent config directory
if !filepath.IsAbs(pattern) && len(parser.files) > 0 {
pattern = filepath.Join(filepath.Dir(parser.files[0]), filepath.Base(pattern))
pattern = filepath.Join(filepath.Dir(parser.files[0]), filepath.Clean(pattern))
}

filenames, err := filepath.Glob(pattern)
Expand All @@ -256,9 +254,6 @@ func (parser *Parser) parseInclude() error {
continue
}
reader, err := os.Open(filename)
defer func() {
_ = reader.Close()
}()
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ type Scanner struct {
curCh rune
newline bool
reader *bufio.Reader
freader io.Reader
}

// NewScanner creates and initializes a new *Scanner from an io.Readerx
func NewScanner(reader io.Reader) *Scanner {
scanner := &Scanner{
reader: bufio.NewReader(reader),
freader: reader,
curLine: 1,
curCol: 1,
newline: false,
Expand Down Expand Up @@ -179,7 +181,7 @@ func (scanner *Scanner) parseComment() {
scanner.curTok.Literal = ""
for {
scanner.readRune()
if scanner.curCh == '\n' {
if scanner.curCh == '\n' || scanner.curCh == eof {
break
}
scanner.curTok.Literal += string(scanner.curCh)
Expand Down Expand Up @@ -221,6 +223,9 @@ func (scanner *Scanner) NextToken() token.Token {
case ch == eof:
scanner.curTok.ID = token.EOF
scanner.curTok.Literal = "EOF"
if f, ok := scanner.freader.(io.ReadCloser); ok {
_ = f.Close()
}
default:
scanner.readRune()
scanner.curTok.Literal = string(ch)
Expand Down

0 comments on commit 0c0b73e

Please sign in to comment.