Skip to content

Commit

Permalink
added file system interface to process config files from any file system
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Dec 7, 2018
1 parent 9f3b186 commit 43d441f
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 25 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ branches:
- /^v[0-9]\.[0-9]/

go:
- 1.6
- 1.7
- 1.8
- 1.11.x
- tip

go_import_path: aahframework.org/forge.v0
go_import_path: github.com/go-aah/forge

script:
- bash <(curl -s https://aahframework.org/go-test)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ forge

[![Build Status](https://travis-ci.org/go-aah/forge.svg?branch=master)](https://travis-ci.org/go-aah/forge)
[![codecov](https://codecov.io/gh/go-aah/forge/branch/master/graph/badge.svg)](https://codecov.io/gh/go-aah/forge)
[![GoDoc](https://godoc.org/aahframework.org/forge.v0?status.svg)](https://godoc.org/aahframework.org/forge.v0)
[![GoDoc](https://godoc.org/github.com/go-aah/forge?status.svg)](https://godoc.org/github.com/go-aah/forge)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

Forge is a configuration syntax and parser forked for [aah framework](https://aahframework.org).

## Installation

`go get -u aahframework.org/forge.v0`
`go get -u github.com/go-aah/forge`

## Example

Expand Down Expand Up @@ -64,7 +64,7 @@ import (
"fmt"
"json"

"aahframework.org/forge.v0"
"github.com/go-aah/forge"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"testing"

"aahframework.org/forge.v0"
"github.com/go-aah/forge"
)

var exampleConfigBytes = []byte(`
Expand Down
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

"aahframework.org/forge.v0"
"github.com/go-aah/forge"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"fmt"

"aahframework.org/forge.v0"
"github.com/go-aah/forge"
)

func Example() {
Expand Down
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.4"
var Version = "0.7.0"

// ParseBytes takes a []byte representation of the config file, parses it
// and responds with `*Section` and potentially an `error` if it cannot
Expand Down
2 changes: 1 addition & 1 deletion forge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"testing"

"aahframework.org/forge.v0"
"github.com/go-aah/forge"
)

var testConfigBytes = []byte(`
Expand Down
39 changes: 39 additions & 0 deletions fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package forge

import (
"io"
"os"
"path/filepath"
)

var fs FileSystem = new(NativeFS)

// FileSystem interface to expand configuration file parsing possiblities.
type FileSystem interface {
Open(filename string) (io.Reader, error)
Glob(pattern string) ([]string, error)
}

// RegisterFS method to register new file system into forge.
func RegisterFS(fileSystem FileSystem) {
fs = fileSystem
}

// NativeFS type defines methods for native file system.
type NativeFS uint8

func (NativeFS) Open(filename string) (io.Reader, error) {
return os.Open(filename)
}

func (NativeFS) Glob(pattern string) ([]string, error) {
return filepath.Glob(pattern)
}

// Close method closes the give file if its compliant to `io.Closer`
func close(f interface{}) error {
if c, ok := f.(io.Closer); ok {
return c.Close()
}
return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module github.com/go-aah/forge
8 changes: 4 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path/filepath"
"strconv"

"aahframework.org/forge.v0/token"
"github.com/go-aah/forge/token"
)

func isSemicolonOrNewline(id token.TokenID) bool {
Expand Down Expand Up @@ -39,7 +39,7 @@ 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)
reader, err := fs.Open(filename)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -242,7 +242,7 @@ func (parser *Parser) parseInclude() error {
pattern = filepath.Join(filepath.Dir(parser.files[0]), filepath.Clean(pattern))
}

filenames, err := filepath.Glob(pattern)
filenames, err := fs.Glob(pattern)
if err != nil {
return err
}
Expand All @@ -253,7 +253,7 @@ func (parser *Parser) parseInclude() error {
if parser.hasParsed(filename) {
continue
}
reader, err := os.Open(filename)
reader, err := fs.Open(filename)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion primative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package forge_test
import (
"testing"

"aahframework.org/forge.v0"
"github.com/go-aah/forge"
)

func TestNewPrimative(t *testing.T) {
Expand Down
6 changes: 2 additions & 4 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io"
"strings"

"aahframework.org/forge.v0/token"
"github.com/go-aah/forge/token"
)

var eof = rune(0)
Expand Down Expand Up @@ -223,9 +223,7 @@ 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()
}
_ = close(scanner.freader)
default:
scanner.readRune()
scanner.curTok.Literal = string(ch)
Expand Down
5 changes: 2 additions & 3 deletions section_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package forge_test

import (
"encoding/json"
"fmt"
"testing"

"aahframework.org/forge.v0"
"github.com/go-aah/forge"
)

func TestSectionKeys(t *testing.T) {
Expand Down Expand Up @@ -111,7 +110,7 @@ prod {
}

bytes, _ := json.MarshalIndent(config1.ToMap(), "", " ")
fmt.Println(string(bytes))
t.Log(string(bytes))
}

func TestMergeSectionFailSectionToField(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Token struct {

func (this Token) String() string {
return fmt.Sprintf(
"ID<%s> Literal<%s> Line<%s> Column<%s>",
"ID<%s> Literal<%s> Line<%v> Column<%v>",
this.ID, this.Literal, this.Line, this.Column,
)
}
Expand Down

0 comments on commit 43d441f

Please sign in to comment.