Over the last few years, I've worked on open-source distributed systems in Go at Google. As a result, I've thought a lot about dependency management, systems configuration, programming languages, and compilers.
Again and again, I saw the same fundamental data structure underpinning these technologies: the directed acyclic graph. The most frustrating part was modeling graph-based configuration in languages that optimized for hierarchical data structures. That's why I created Virgo.
Virgo is a graph-based configuration language. It has two main features: edge definitions and vertex definitions. The vgo configuration file then parses into an adjacency list. You can achieve similar results by adding additional conventions and restrictions on YAML or JSON. Much like YAML optimized for human readability, Virgo optimizes natural graph readability, editability, and representation.
// config.vgo
a -> b, c, d -> e <- f, g
A graphical representation of the Virgo graph
Virgo is open to proposals and language changes. Please open up an issue to start a discussion at https://github.com/r2d4/virgo.
Graphs are everywhere in configuration management. One graph that engineers may be familiar with is the Makefile
target graph. The make
tool topologically sorts the targets that it resolves, which lets it build the files in order. Virgo's CLI or Go library allows developers to replicate this feature easily.
clean -> parser, lexer -> "src files" -> test
parser = `goyacc parser.y`
lexer = `golex lex.l`
clean = `rm lex.yy.go parser.go || true`
test = `go test-v`
"src files" = `go build ./...`
A simple example is to build the Virgo CLI tool with the language itself.
There are three entry points to parsing the Virgo file. First, you can use the Go library found in the same repository to parse the file into a native Go struct. Second, there is also a published CLI binary that exposes the parsing function for other environments. Finally, someone from the community has written a binding in Python https://github.com/jwg4/pyvirgo.
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/pkg/errors"
"matt-rickard.com/virgo/pkg/virgo"
)
func main() {
if err := run("config.go"); err != nil {
log.Fatal(err)
os.Exit(1)
}
}
func run(fname string) error {
f, err := ioutil.ReadFile(fname)
if err != nil {
return errors.Wrap(err, "reading file")
}
g, err := virgo.Parse(f)
if err != nil {
return errors.Wrap(err, "parsing virgo file")
}
nodes, err := virgo.TopSort(g)
if err != nil {
return errors.Wrap(err, "topological sort")
}
out := []string{}
for _, n := range nodes {
out = append(out, g.Vertices[n]...)
}
fmt.Println(strings.Join(out, "\n"))
return nil
}
Code snippet to read a Virgo file, topologically sort the graph and print out the vertex definitions for each node in order.
$ virgo run build.vgo
Or build with the CLI tool.
One operation we frequently want to perform on graphs is a topological sort. Topological sorting is a linear ordering of vertices such that for every directed edge u -> v, vertex u comes before v in the sequence.
The CLI tool topologically sorts the graph and can even start from a particular vertex (analogous to a Make target).
$ virgo run build.vgo:parser
Build systems are not the only type of configuration schema that can benefit from a graphical representation. Some other examples include:
- deployment of microservices
- docker build instructions
- continuous-integration pipelines
- package dependencies
- git commits
For complete documentation on the language and features of Virgo, visit the GitHub page https://github.com/r2d4/virgo.