Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasepe committed Feb 24, 2021
0 parents commit 5393c52
Show file tree
Hide file tree
Showing 16 changed files with 598 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Intellij
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/encodings.xml
.idea/**/compiler.xml
.idea/**/misc.xml
.idea/**/modules.xml
.idea/**/vcs.xml

## VSCode
.vscode/

## File-based project format:
*.iws
*.iml
.idea/

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
*.dat
*.DS_Store

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Goreleaser builds
**/dist/**

# This is my wip ideas folder
experiments/**
50 changes: 50 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
# Run locally with: goreleaser --rm-dist --snapshot --skip-publish
project_name: yml2dot
before:
hooks:
- go mod tidy
- go mod download
builds:
- binary: '{{ .ProjectName }}'
main: ./main.go
env:
- CGO_ENABLED=0
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}}
- -a -extldflags "-static"
goos:
- windows
- linux
- darwin
goarch:
- amd64
archives:
- replacements:
darwin: macOS
windows: win
amd64: 64-bit
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .ProjectName }}_{{ .Tag }}"
nfpms:
-
package_name: yml2dot
vendor: Luca Sepe
homepage: https://lucasepe.it/
maintainer: Luca Sepe <luca.sepe@gmail.com>
description: Turn YAML into beautiful Graph.
license: MIT
replacements:
amd64: 64-bit
formats:
- deb
- rpm
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright(c) Luca Sepe

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
BINARY := $(shell basename "$(PWD)")
SOURCES := ./

.PHONY: help
all: help
help: Makefile
@echo
@echo " Choose a command run in "$(PROJECTNAME)":"
@echo
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
@echo

.DEFAULT_GOAL := help

## build: Build the command line tool
build: clean
go build -o ${BINARY} ${SOURCES}

## examples: Generates the Graph PNG examples
examples:
rm -f ./_examples/*.png
go run main.go -from '/***' -to '***/' ./_examples/Box.java | dot -Tpng > ./_examples/Box.java.png
go run main.go ./_examples/docker-compose-sample.yml | dot -Tpng > ./_examples/docker-compose-sample.yml.png

## test: Starts unit test
test:
go test -v ./... -coverprofile coverage.out

release:
goreleaser --rm-dist --snapshot --skip-publish

## clean: Clean the binary
clean:
rm -f $(BINARY)
105 changes: 105 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# [yml2dot](https://github.com/lucasepe/yml2dot/releases/latest) - Turn YAML into beautiful Graph

## Use Cases

- Visualize your YAML files as Graph
- Generate additional info from your source code (simply define a YAML block and use this tool)

## How [yml2dot](https://github.com/lucasepe/yml2dot/releases/latest) works?

Takes in input:

- any YAML file
- any text file that has YAML between comments (like [front matter](https://jekyllrb.com/docs/front-matter/))

Generates a [dot script](https://en.wikipedia.org/wiki/DOT_(graph_description_language)) for [Graphviz](https://graphviz.gitlab.io/download/).

# Examples

## Visualize a [docker-compose](https://docs.docker.com/compose/compose-file/) YAML file

Given a sample `docker-compose.yml` file:

```yaml
version: "3.9"
services:
redis:
image: redis:latest
deploy:
replicas: 1
configs:
- source: my_config
target: /redis_config
uid: '103'
gid: '103'
mode: 0440
configs:
my_config:
file: ./my_config.txt
my_other_config:
external: true
```
Run [yml2dot](https://github.com/lucasepe/yml2dot/releases/latest) like this:
```bash
$ yml2dot docker-compose-sample.yml | dot -Tpng > docker-compose-sample.yml.png
```

and create this graph:

![](./_examples/docker-compose-sample.yml.png)

## Grab YAML info embedded in your source code

```java
/***
Box:
Object:
- set
- get
***/

public class Box {
private Object object;

public void set(Object object) {
this.object = object;
}
public Object get() {
return object;
}
}
```

> Use the `-from` and `-to` flags to mark your YAML block.
Run [yml2dot](https://github.com/lucasepe/yml2dot/releases/latest) like this:

```bash
$ yml2dot -from '/***' -to '***/' Box.java | dot -Tpng > Box.java.png
```

and create this graph:

![](./_examples/Box.java.png)


# How to install?

In order to use the `yml2dot` command, compile it using the following command:

```bash
go get -u github.com/lucasepe/yml2dot
```

This will create the executable under your $GOPATH/bin directory.

## Ready-To-Use Releases

If you don't want to compile the sourcecode yourself, [Here you can find the tool already compiled](https://github.com/lucasepe/yml2dot/releases/latest) for:

- MacOS
- Linux
- Windows

17 changes: 17 additions & 0 deletions _examples/Box.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/***
Box:
Object:
- set
- get
***/

public class Box {
private Object object;

public void set(Object object) {
this.object = object;
}
public Object get() {
return object;
}
}
Binary file added _examples/Box.java.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions _examples/docker-compose-sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: "3.9"
services:
redis:
image: redis:latest
deploy:
replicas: 1
configs:
- source: my_config
target: /redis_config
uid: '103'
gid: '103'
mode: 0440
configs:
my_config:
file: ./my_config.txt
my_other_config:
external: true
Binary file added _examples/docker-compose-sample.yml.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/lucasepe/yml2dot

go 1.16

// replace github.com/lucasepe/dot => ../dot

require (
github.com/lucasepe/dot v0.2.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/lucasepe/dot v0.2.0 h1:+jAprNmbr1NhMttoB9281p7CPSeKalPUkkBOu4P92dM=
github.com/lucasepe/dot v0.2.0/go.mod h1:5gEWjskJdc7e0jMJJLg/PVNv5ynTLvdTYq9OTgphX8Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
87 changes: 87 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/lucasepe/yml2dot/parser"
"github.com/lucasepe/yml2dot/renderer"
)

const (
maxFileSize int64 = 512 * 1024 // 512 Kb
banner = `+--------+ +-------+
| YAML +----->| Dot |
+--------+ +-------+`
)

var (
flagBlockStart string
flagBlockEnd string
)

func main() {
configureFlags()

if flag.CommandLine.Arg(0) == "" {
flag.CommandLine.Usage()
os.Exit(2)
}

src, err := os.Open(flag.Args()[0])
exitOnErr(err)
defer src.Close()

res, err := parser.Parse(src, flagBlockStart, flagBlockEnd)
exitOnErr(err)

fmt.Print(renderer.Render(res))
}

func configureFlags() {
name := appName()

flag.CommandLine.Usage = func() {
fmt.Print(banner, "\n")
fmt.Printf("Turn YAML into beautiful Graph.\n\n")

fmt.Print("USAGE:\n\n")
fmt.Printf(" %s [flags] <path/to/your/file>\n\n", name)

fmt.Print("EXAMPLE(s):\n\n")
fmt.Printf(" %s -from '/****' -to '****/' MyClass.java | dot -Tpng > output.png\n", name)
fmt.Printf(" %s config.yml | dot -Tpng > output.png\n\n", name)

fmt.Print("FLAGS:\n\n")
flag.CommandLine.SetOutput(os.Stdout)
flag.CommandLine.PrintDefaults()
flag.CommandLine.SetOutput(ioutil.Discard) // hide flag errors
fmt.Print(" -help\n\tprints this message\n")
fmt.Println()

fmt.Println("Crafted with passion by Luca Sepe - https://github.com/lucasepe/yml2dot")
}

flag.CommandLine.SetOutput(ioutil.Discard) // hide flag errors
flag.CommandLine.Init(os.Args[0], flag.ExitOnError)

flag.CommandLine.StringVar(&flagBlockStart, "from", "", "pattern that marks the beginning of the YAML block")
flag.CommandLine.StringVar(&flagBlockEnd, "to", "", "pattern that marks the end of the YAML block")

flag.CommandLine.Parse(os.Args[1:])
}

func appName() string {
return filepath.Base(os.Args[0])
}

// exitOnErr check for an error and eventually exit
func exitOnErr(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
os.Exit(1)
}
}
Loading

0 comments on commit 5393c52

Please sign in to comment.