Skip to content

Commit

Permalink
Merge pull request #17 from ray-g/pr
Browse files Browse the repository at this point in the history
PRs for a bunch of issues
  • Loading branch information
kaishuu0123 authored Feb 11, 2021
2 parents f50dfa0 + c3db457 commit 1d06733
Show file tree
Hide file tree
Showing 16 changed files with 1,257 additions and 929 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build_no_peg: depend bindata
go build

build: depend peg bindata
go build
go build -mod=readonly

peg:
peg erd.peg
Expand Down
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ get binary from [releases page](https://github.com/kaishuu0123/erd-go/releases).

or

```
```shell
go get github.com/kaishuu0123/erd-go
```

or (for Mac)

```
```shell
brew tap kaishuu0123/erd-go
brew install erd-go
```

## Usage

```
```shell
Usage:
erd-go [OPTIONS] PATTERN [PATH]

Expand All @@ -45,19 +45,19 @@ Help Options:

support input from STDIN.

```
```shell
cat examples/nfldb.er | erd-go
```

ex.) convert to png from dot (use dot command)

```
```shell
cat examples/nfldb.er | erd-go | dot -Tpng -o nfldb.png
```

## Usage (Used by Docker container)

```
```shell
cat examples/nfldb.er | docker run --rm -i kaishuu0123/erd-go | docker run --rm -i risaacson/graphviz dot -T png > nfldb.png
```

Expand All @@ -68,19 +68,26 @@ see [examples directory](https://github.com/kaishuu0123/erd-go/blob/master/examp
## Build Instruction

1. install glide
```

```shell
go get github.com/Masterminds/glide
```

1. install go-bindata
```

```shell
go get github.com/jteeuwen/go-bindata
```

1. install peg
```

```shell
go get github.com/pointlander/peg
```

1. make
```

```shell
make
```

Expand All @@ -89,7 +96,8 @@ see [examples directory](https://github.com/kaishuu0123/erd-go/blob/master/examp
MIT

## Credits

This work is based off of several existing projects:
* https://github.com/BurntSushi/erd
* https://github.com/unok/erdm

* <https://github.com/BurntSushi/erd>
* <https://github.com/unok/erdm>
45 changes: 42 additions & 3 deletions erd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package main

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
"text/template"

Expand Down Expand Up @@ -56,7 +62,11 @@ func main() {
}

parser := &Parser{Buffer: contents}
parser.Init()
err = parser.Init()
if err != nil {
logStderr.Println(err)
os.Exit(1)
}
err = parser.Parse()
if err != nil {
logStderr.Println(err)
Expand All @@ -68,12 +78,13 @@ func main() {
if parser.Erd.IsError {
os.Exit(1)
}
parser.Erd.CalcIsolated()

dot, _ := Asset("templates/dot.tmpl")
tables, _ := Asset("templates/dot_tables.tmpl")
relations, _ := Asset("templates/dot_relations.tmpl")
templates := template.Must(
template.New("").Parse(
template.New("").Funcs(template.FuncMap{"StringsJoin": strings.Join}).Parse(
string(dot) +
string(tables) +
string(relations)))
Expand All @@ -87,5 +98,33 @@ func main() {
}
}

templates.ExecuteTemplate(fd, "dot", parser.Erd)
var erdbuf bytes.Buffer
err = templates.ExecuteTemplate(&erdbuf, "dot", parser.Erd)
if err != nil {
logStderr.Println(err)
os.Exit(1)
}

// The OutFormat only works with Graphviz together
if opts.OutFormat != "" {
dotcmd := "dot"
if runtime.GOOS == "windows" {
dotcmd = "dot.exe"
}
cmd := exec.Command(dotcmd, fmt.Sprintf("-T%s", opts.OutFormat))
cmd.Stdin = &erdbuf
cmd.Stdout = fd
cmd.Stderr = fd
err = cmd.Run()
if err != nil {
logStderr.Println(err)
os.Exit(1)
}
} else {
n, err := io.Copy(fd, &erdbuf)
if err != nil {
logStderr.Printf("failed to copy buffer: err: %v, copied %d bytes\n", err, n)
os.Exit(1)
}
}
}
10 changes: 7 additions & 3 deletions erd.peg
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ root <- expression EOT /
EOT <- !.

expression <-
(title_info / relation_info / table_info / comment_line / empty_line)*
(title_info / color_info / relation_info / table_info / comment_line / empty_line)*

empty_line <- ws { p.ClearTableAndColumn() }
empty_line <- ws { p.ClearTableAndColumn() }
comment_line <- space* '#' comment_string newline

color_info <- 'colors' ws* '{' ws* (color_key_value ws* attribute_sep? ws*)* ws* '}' newline
color_key_value <-
attribute_key space* ':' space* attribute_value { p.AddColorDefine() }

title_info <- 'title' ws* '{' ws* (title_attribute ws* attribute_sep? ws*)* ws* '}' newline

table_info <-
Expand Down Expand Up @@ -66,4 +70,4 @@ newline_or_eot <- newline / EOT
space <- [ \t]+
string <- (!["\t\r\n/:,\[\]{} ].)+
string_in_quote <- (!["\t\r\n].)+
cardinality <- [01*+]
cardinality <- [01?*+]
Loading

0 comments on commit 1d06733

Please sign in to comment.