Skip to content

Instantly share code, notes, and snippets.

@akhenakh
Created March 4, 2021 16:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhenakh/f92055171065d95d968528fca1fdfea4 to your computer and use it in GitHub Desktop.
Save akhenakh/f92055171065d95d968528fca1fdfea4 to your computer and use it in GitHub Desktop.
Visual graph for Drone pipelines
package main
import (
"fmt"
"log"
"os"
"github.com/drone/drone-yaml/yaml"
"github.com/goccy/go-graphviz"
"github.com/goccy/go-graphviz/cgraph"
"github.com/namsral/flag"
)
var ()
func main() {
filename := flag.String("filename", ".drone.yml", "path for the drone file to analyze")
flag.Parse()
f, err := os.Open(*filename)
if err != nil {
log.Fatal(err)
}
manifest, err := yaml.Parse(f)
if err != nil {
log.Fatal(err)
}
g := graphviz.New()
graph, err := g.Graph()
if err != nil {
log.Fatal(err)
}
for _, ressource := range manifest.Resources {
if ressource.GetKind() == yaml.KindPipeline {
m := make(map[string]*cgraph.Node)
pipeline := ressource.(*yaml.Pipeline)
first := true
for _, step := range pipeline.Steps {
n, err := graph.CreateNode(step.Name)
if err != nil {
log.Fatal(err)
}
if first {
_ = n.SetLabel(fmt.Sprintf("%s %s", pipeline.Name, step.Name))
first = false
}
m[step.Name] = n
for _, sdep := range step.DependsOn {
pnode, ok := m[sdep]
if !ok {
log.Fatal("cant find step", sdep)
}
_, err := graph.CreateEdge(n.Name(), pnode, n)
if err != nil {
log.Fatal(err)
}
}
}
}
}
if err := g.RenderFilename(graph, graphviz.PNG, "graph.png"); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment