Skip to content

Commit d1d4cca

Browse files
committed
wip
1 parent d1e9551 commit d1d4cca

File tree

7 files changed

+150
-59
lines changed

7 files changed

+150
-59
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ install: ## Generate binary and copy it to $GOPATH/bin (equivalent to go install
1616
goreleaser build --clean --snapshot --single-target -o $(GOPATH)/bin/dib
1717

1818
build: ## Build the CLI binary.
19-
CGO_ENABLED=1 go build -o ./dist/dib ./cmd
19+
CGO_ENABLED=0 go build -o ./dist/dib ./cmd
2020

2121
docs: build
2222
./dist/dib docgen

go.mod

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/aws/aws-sdk-go-v2/config v1.27.11
88
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1
99
github.com/docker/cli v26.0.1+incompatible
10-
github.com/goccy/go-graphviz v0.1.2
1110
github.com/google/uuid v1.6.0
1211
github.com/mholt/archiver/v3 v3.5.1
1312
github.com/moby/patternmatcher v0.6.0
@@ -67,7 +66,6 @@ require (
6766
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
6867
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
6968
github.com/fatih/camelcase v1.0.0 // indirect
70-
github.com/fogleman/gg v1.3.0 // indirect
7169
github.com/fsnotify/fsnotify v1.7.0 // indirect
7270
github.com/fvbommel/sortorder v1.1.0 // indirect
7371
github.com/go-errors/errors v1.4.2 // indirect
@@ -76,7 +74,6 @@ require (
7674
github.com/go-openapi/jsonreference v0.20.2 // indirect
7775
github.com/go-openapi/swag v0.22.3 // indirect
7876
github.com/gogo/protobuf v1.3.2 // indirect
79-
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
8077
github.com/golang/protobuf v1.5.4 // indirect
8178
github.com/golang/snappy v0.0.2 // indirect
8279
github.com/google/btree v1.0.1 // indirect
@@ -139,7 +136,6 @@ require (
139136
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
140137
go.uber.org/multierr v1.11.0 // indirect
141138
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
142-
golang.org/x/image v0.14.0 // indirect
143139
golang.org/x/net v0.23.0 // indirect
144140
golang.org/x/oauth2 v0.15.0 // indirect
145141
golang.org/x/sys v0.18.0 // indirect

go.sum

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ github.com/containerd/containerd v1.7.12 h1:+KQsnv4VnzyxWcfO9mlxxELaoztsDEjOuCMP
8080
github.com/containerd/containerd v1.7.12/go.mod h1:/5OMpE1p0ylxtEUGY8kuCYkDRzJm9NO1TFMWjUpdevk=
8181
github.com/containerd/stargz-snapshotter/estargz v0.14.3 h1:OqlDCK3ZVUO6C3B/5FSkDwbkEETK84kQgEeFwDC+62k=
8282
github.com/containerd/stargz-snapshotter/estargz v0.14.3/go.mod h1:KY//uOCIkSuNAHhJogcZtrNHdKrA99/FCCRjE3HD36o=
83-
github.com/corona10/goimagehash v1.0.2 h1:pUfB0LnsJASMPGEZLj7tGY251vF+qLGqOgEP4rUs6kA=
84-
github.com/corona10/goimagehash v1.0.2/go.mod h1:/l9umBhvcHQXVtQO1V6Gp1yD20STawkhRnnX0D1bvVI=
8583
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
8684
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
8785
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
@@ -115,8 +113,6 @@ github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d h1:105gxyaGwC
115113
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d/go.mod h1:ZZMPRZwes7CROmyNKgQzC3XPs6L/G2EJLHddWejkmf4=
116114
github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8=
117115
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
118-
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
119-
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
120116
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
121117
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
122118
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
@@ -135,12 +131,8 @@ github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/
135131
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
136132
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
137133
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
138-
github.com/goccy/go-graphviz v0.1.2 h1:sWSJ6w13BCm/ZOUTHDVrdvbsxqN8yyzaFcHrH/hQ9Yg=
139-
github.com/goccy/go-graphviz v0.1.2/go.mod h1:pMYpbAqJT10V8dzV1JN/g/wUlG/0imKPzn3ZsrchGCI=
140134
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
141135
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
142-
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
143-
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
144136
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
145137
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
146138
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -266,8 +258,6 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
266258
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
267259
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J1GEMiLbxo1LJaP8RfCpH6pymGZus=
268260
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
269-
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5 h1:BvoENQQU+fZ9uukda/RzCAL/191HHwJA5b13R6diVlY=
270-
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
271261
github.com/nwaples/rardecode v1.1.0 h1:vSxaY8vQhOcVr4mm5e8XllHWTiM4JF507A0Katqw7MQ=
272262
github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
273263
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
@@ -381,8 +371,6 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
381371
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
382372
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
383373
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
384-
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
385-
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
386374
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
387375
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
388376
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=

pkg/dag/dag.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package dag
22

33
import (
4-
"os"
54
"sync"
65

7-
"github.com/goccy/go-graphviz"
8-
"github.com/goccy/go-graphviz/cgraph"
96
"github.com/radiofrance/dib/internal/logger"
107
"golang.org/x/sync/errgroup"
118
"gopkg.in/yaml.v3"
@@ -14,13 +11,13 @@ import (
1411
// DAG represents a direct acyclic graph.
1512
type DAG struct {
1613
nodes []*Node // Root nodes of the graph.
17-
GV *graphviz.Graphviz
18-
CG *cgraph.Graph
1914
}
2015

2116
func (d *DAG) Print() {
22-
if err := d.GV.Render(d.CG, graphviz.XDOT, os.Stdout); err != nil {
23-
logger.Fatalf(err.Error())
17+
for _, t := range d.nodes {
18+
if err := DefaultTree.WithRoot(t).Render(); err != nil {
19+
logger.Fatalf(err.Error())
20+
}
2421
}
2522
}
2623

pkg/dag/node.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package dag
33
import (
44
"sync"
55

6-
"github.com/goccy/go-graphviz/cgraph"
76
"golang.org/x/sync/errgroup"
87
)
98

@@ -22,20 +21,13 @@ type Node struct {
2221

2322
parents []*Node
2423
children []*Node
25-
26-
N *cgraph.Node
2724
}
2825

2926
// NewNode creates a new instance of a Node.
30-
func NewNode(image *Image, n ...*cgraph.Node) *Node {
31-
var cNode *cgraph.Node
32-
if len(n) > 0 {
33-
cNode = n[0]
34-
}
27+
func NewNode(image *Image) *Node {
3528
return &Node{
3629
Image: image,
3730
waitCond: sync.NewCond(&sync.Mutex{}),
38-
N: cNode,
3931
}
4032
}
4133

pkg/dag/printer.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package dag
2+
3+
import (
4+
"io"
5+
"strings"
6+
7+
"github.com/pterm/pterm"
8+
)
9+
10+
// DefaultTree contains standards, which can be used to render a TreePrinter.
11+
var DefaultTree = TreePrinter{
12+
TreeStyle: &pterm.ThemeDefault.TreeStyle,
13+
TextStyle: &pterm.ThemeDefault.TreeTextStyle,
14+
TopRightCornerString: "└",
15+
HorizontalString: "─",
16+
TopRightDownString: "├",
17+
VerticalString: "│",
18+
RightDownLeftString: "┬",
19+
Indent: 2,
20+
}
21+
22+
// TreePrinter is able to render a list.
23+
type TreePrinter struct {
24+
Root *Node
25+
TreeStyle *pterm.Style
26+
TextStyle *pterm.Style
27+
TopRightCornerString string
28+
TopRightDownString string
29+
HorizontalString string
30+
VerticalString string
31+
RightDownLeftString string
32+
Indent int
33+
Writer io.Writer
34+
}
35+
36+
// WithTreeStyle returns a new list with a specific tree style.
37+
func (p TreePrinter) WithTreeStyle(style *pterm.Style) *TreePrinter {
38+
p.TreeStyle = style
39+
return &p
40+
}
41+
42+
// WithTopRightCornerString returns a new list with a specific TopRightCornerString.
43+
func (p TreePrinter) WithTopRightCornerString(s string) *TreePrinter {
44+
p.TopRightCornerString = s
45+
return &p
46+
}
47+
48+
// WithTopRightDownStringOngoing returns a new list with a specific TopRightDownString.
49+
func (p TreePrinter) WithTopRightDownStringOngoing(s string) *TreePrinter {
50+
p.TopRightDownString = s
51+
return &p
52+
}
53+
54+
// WithHorizontalString returns a new list with a specific HorizontalString.
55+
func (p TreePrinter) WithHorizontalString(s string) *TreePrinter {
56+
p.HorizontalString = s
57+
return &p
58+
}
59+
60+
// WithVerticalString returns a new list with a specific VerticalString.
61+
func (p TreePrinter) WithVerticalString(s string) *TreePrinter {
62+
p.VerticalString = s
63+
return &p
64+
}
65+
66+
// WithRoot returns a new list with a specific Root.
67+
func (p TreePrinter) WithRoot(root *Node) *TreePrinter {
68+
p.Root = root
69+
return &p
70+
}
71+
72+
// WithIndent returns a new list with a specific amount of spacing between the levels.
73+
// Indent must be at least 1.
74+
func (p TreePrinter) WithIndent(indent int) *TreePrinter {
75+
if indent < 1 {
76+
indent = 1
77+
}
78+
p.Indent = indent
79+
return &p
80+
}
81+
82+
// Render prints the list to the terminal.
83+
func (p TreePrinter) Render() error {
84+
s, _ := p.Srender()
85+
pterm.Fprintln(p.Writer, s)
86+
87+
return nil
88+
}
89+
90+
// Srender renders the list as a string.
91+
func (p TreePrinter) Srender() (string, error) {
92+
if p.TreeStyle == nil {
93+
p.TreeStyle = pterm.NewStyle()
94+
}
95+
if p.TextStyle == nil {
96+
p.TextStyle = pterm.NewStyle()
97+
}
98+
99+
var result string
100+
if p.Root.Image.Name != "" {
101+
result += p.TextStyle.Sprint(p.Root.Image.Name) + "\n"
102+
}
103+
result += walkOverTree(p.Root.Children(), p, "")
104+
return result, nil
105+
}
106+
107+
// walkOverTree is a recursive function,
108+
// which analyzes a TreePrinter and connects the items with specific characters.
109+
// Returns TreePrinter as string.
110+
func walkOverTree(nodes []*Node, printer TreePrinter, prefix string) string {
111+
var ret string
112+
for nodeIndex, node := range nodes {
113+
if len(nodes) > nodeIndex+1 { // if not last in nodes
114+
if len(node.Children()) == 0 { // if there are no children
115+
ret += prefix + printer.TreeStyle.Sprint(printer.TopRightDownString) +
116+
strings.Repeat(printer.TreeStyle.Sprint(printer.HorizontalString), printer.Indent) +
117+
printer.TextStyle.Sprint(node.Image.Name) + "\n"
118+
} else { // if there are children
119+
ret += prefix + printer.TreeStyle.Sprint(printer.TopRightDownString) +
120+
strings.Repeat(printer.TreeStyle.Sprint(printer.HorizontalString), printer.Indent-1) +
121+
printer.TreeStyle.Sprint(printer.RightDownLeftString) +
122+
printer.TextStyle.Sprint(node.Image.Name) + "\n"
123+
ret += walkOverTree(node.Children(), printer,
124+
prefix+printer.TreeStyle.Sprint(printer.VerticalString)+strings.Repeat(" ", printer.Indent-1))
125+
}
126+
} else if len(nodes) == nodeIndex+1 { // if last in nodes
127+
if len(node.Children()) == 0 { // if there are no children
128+
ret += prefix + printer.TreeStyle.Sprint(printer.TopRightCornerString) +
129+
strings.Repeat(printer.TreeStyle.Sprint(printer.HorizontalString), printer.Indent) +
130+
printer.TextStyle.Sprint(node.Image.Name) + "\n"
131+
} else { // if there are children
132+
ret += prefix + printer.TreeStyle.Sprint(printer.TopRightCornerString) +
133+
strings.Repeat(printer.TreeStyle.Sprint(printer.HorizontalString), printer.Indent-1) +
134+
printer.TreeStyle.Sprint(printer.RightDownLeftString) +
135+
printer.TextStyle.Sprint(node.Image.Name) + "\n"
136+
ret += walkOverTree(node.Children(), printer,
137+
prefix+strings.Repeat(" ", printer.Indent))
138+
}
139+
}
140+
}
141+
return ret
142+
}

pkg/dib/generate_dag.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"strings"
1414

1515
"github.com/docker/cli/cli/command/image/build"
16-
"github.com/goccy/go-graphviz"
1716
"github.com/moby/patternmatcher"
1817
"github.com/radiofrance/dib/internal/logger"
1918
"github.com/radiofrance/dib/pkg/dag"
@@ -29,18 +28,6 @@ const (
2928
// GenerateDAG discovers and parses all Dockerfiles at a given path,
3029
// and generates the DAG representing the relationships between images.
3130
func GenerateDAG(buildPath, registryPrefix, customHashListPath string, buildArgs map[string]string) (*dag.DAG, error) {
32-
gGraph := graphviz.New()
33-
cGraph, err := gGraph.Graph()
34-
if err != nil {
35-
logger.Fatalf(err.Error())
36-
}
37-
defer func() {
38-
if err := cGraph.Close(); err != nil {
39-
logger.Fatalf(err.Error())
40-
}
41-
gGraph.Close()
42-
}()
43-
4431
var allFiles []string
4532
cache := make(map[string]*dag.Node)
4633
allParents := make(map[string][]dockerfile.ImageRef)
@@ -89,11 +76,7 @@ func GenerateDAG(buildPath, registryPrefix, customHashListPath string, buildArgs
8976
img.IgnorePatterns = ignorePatterns
9077

9178
allParents[img.Name] = dckfile.From
92-
n, err := cGraph.CreateNode(img.Name)
93-
if err != nil {
94-
return err
95-
}
96-
cache[img.Name] = dag.NewNode(img, n)
79+
cache[img.Name] = dag.NewNode(img)
9780
}
9881
return nil
9982
}); err != nil {
@@ -120,18 +103,11 @@ func GenerateDAG(buildPath, registryPrefix, customHashListPath string, buildArgs
120103
continue
121104
}
122105

123-
_, err := cGraph.CreateEdge(fmt.Sprintf("%s -> %s", node.Image.Name, name), node.N, cache[name].N)
124-
if err != nil {
125-
return nil, err
126-
}
127106
node.AddChild(cache[name])
128107
}
129108
}
130109

131-
graph := &dag.DAG{
132-
GV: gGraph,
133-
CG: cGraph,
134-
}
110+
graph := &dag.DAG{}
135111
// If an image has no parents in the DAG, we consider it a root image
136112
for name, img := range cache {
137113
if len(img.Parents()) == 0 {

0 commit comments

Comments
 (0)