From b84abcfb45fe601d81cd32691343502db48cd3f9 Mon Sep 17 00:00:00 2001 From: Antoine Gelloz Date: Fri, 19 Apr 2024 17:29:22 +0200 Subject: [PATCH] wip --- cmd/graph.go | 9 +++++---- pkg/dag/dag.go | 12 +++++++----- pkg/dag/printer.go | 8 ++++---- pkg/dib/generate_dag.go | 5 ++--- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/cmd/graph.go b/cmd/graph.go index 163e1479d..f1b9003a7 100644 --- a/cmd/graph.go +++ b/cmd/graph.go @@ -19,8 +19,8 @@ type GraphOpts struct { // buildCmd represents the build command. var graphCmd = &cobra.Command{ Use: "graph", - Short: "", - Long: `dib build will compute the graph of images, and print it.`, + Short: "Compute the graph of images, and print it.", + Long: "Compute the graph of images, and print it.", Run: func(cmd *cobra.Command, _ []string) { bindPFlagsSnakeCase(cmd.Flags()) @@ -53,7 +53,8 @@ func doGraph(opts GraphOpts) error { } logger.Debugf("Generate DAG -- Done") - graph.Print() - + logger.Debugf("Print DAG") + graph.Print(opts.BuildPath) + logger.Debugf("Print DAG -- Done") return nil } diff --git a/pkg/dag/dag.go b/pkg/dag/dag.go index af869a1d5..ee26fe105 100644 --- a/pkg/dag/dag.go +++ b/pkg/dag/dag.go @@ -13,11 +13,13 @@ type DAG struct { nodes []*Node // Root nodes of the graph. } -func (d *DAG) Print() { - for _, t := range d.nodes { - if err := DefaultTree.WithRoot(t).Render(); err != nil { - logger.Fatalf(err.Error()) - } +func (d *DAG) Print(name string) { + rootNode := &Node{ + Image: &Image{Name: name}, + children: d.nodes, + } + if err := DefaultTree.WithRoot(rootNode).Render(); err != nil { + logger.Fatalf(err.Error()) } } diff --git a/pkg/dag/printer.go b/pkg/dag/printer.go index 5eabb456b..b7211112e 100644 --- a/pkg/dag/printer.go +++ b/pkg/dag/printer.go @@ -114,12 +114,12 @@ func walkOverTree(nodes []*Node, printer TreePrinter, prefix string) string { if len(node.Children()) == 0 { // if there are no children ret += prefix + printer.TreeStyle.Sprint(printer.TopRightDownString) + strings.Repeat(printer.TreeStyle.Sprint(printer.HorizontalString), printer.Indent) + - printer.TextStyle.Sprint(node.Image.Name) + "\n" + printer.TextStyle.Sprint(node.Image.ShortName) + "\n" } else { // if there are children ret += prefix + printer.TreeStyle.Sprint(printer.TopRightDownString) + strings.Repeat(printer.TreeStyle.Sprint(printer.HorizontalString), printer.Indent-1) + printer.TreeStyle.Sprint(printer.RightDownLeftString) + - printer.TextStyle.Sprint(node.Image.Name) + "\n" + printer.TextStyle.Sprint(node.Image.ShortName) + "\n" ret += walkOverTree(node.Children(), printer, prefix+printer.TreeStyle.Sprint(printer.VerticalString)+strings.Repeat(" ", printer.Indent-1)) } @@ -127,12 +127,12 @@ func walkOverTree(nodes []*Node, printer TreePrinter, prefix string) string { if len(node.Children()) == 0 { // if there are no children ret += prefix + printer.TreeStyle.Sprint(printer.TopRightCornerString) + strings.Repeat(printer.TreeStyle.Sprint(printer.HorizontalString), printer.Indent) + - printer.TextStyle.Sprint(node.Image.Name) + "\n" + printer.TextStyle.Sprint(node.Image.ShortName) + "\n" } else { // if there are children ret += prefix + printer.TreeStyle.Sprint(printer.TopRightCornerString) + strings.Repeat(printer.TreeStyle.Sprint(printer.HorizontalString), printer.Indent-1) + printer.TreeStyle.Sprint(printer.RightDownLeftString) + - printer.TextStyle.Sprint(node.Image.Name) + "\n" + printer.TextStyle.Sprint(node.Image.ShortName) + "\n" ret += walkOverTree(node.Children(), printer, prefix+strings.Repeat(" ", printer.Indent)) } diff --git a/pkg/dib/generate_dag.go b/pkg/dib/generate_dag.go index 392d2cb6b..a5b6e3c39 100644 --- a/pkg/dib/generate_dag.go +++ b/pkg/dib/generate_dag.go @@ -174,7 +174,7 @@ func generateHashes(graph *dag.DAG, allFiles []string, customHashListPath string for { needRepass := false - err := graph.WalkErr(func(node *dag.Node) error { + if err := graph.WalkErr(func(node *dag.Node) error { var parentHashes []string for _, parent := range node.Parents() { if parent.Image.Hash == "" { @@ -218,8 +218,7 @@ func generateHashes(graph *dag.DAG, allFiles []string, customHashListPath string } node.Image.Hash = hash return nil - }) - if err != nil { + }); err != nil { return err } if !needRepass {