-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (76 loc) · 1.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"archive/tar"
"flag"
"fmt"
"log"
"os"
"slices"
"strings"
"github.com/dustin/go-humanize"
"github.com/wagoodman/dive/dive/filetree"
"github.com/wagoodman/dive/dive/image/docker"
)
func main() {
user := flag.Bool("u", false, "show user")
flag.Parse()
name := flag.Arg(0)
if name == "" {
panic("specify image name to fetch")
}
engine := docker.NewResolverFromEngine()
img, err := engine.Fetch(name)
if err != nil {
log.Fatal(err)
}
w := os.Stdout
nodes := map[string]*filetree.FileNode{}
visitor := func(node *filetree.FileNode) error {
nodes[node.Path()] = node
return nil
}
visitEvaluator := func(node *filetree.FileNode) bool {
return node.IsLeaf()
}
for _, t := range img.Trees {
err := t.VisitDepthChildFirst(visitor, visitEvaluator)
if err != nil {
log.Fatal(err)
}
}
var listNodes []*filetree.FileNode
for _, n := range nodes {
listNodes = append(listNodes, n)
}
slices.SortFunc(listNodes, func(a, b *filetree.FileNode) int {
return strings.Compare(a.Path(), b.Path())
})
for _, node := range listNodes {
uinfo := ""
if *user {
uinfo = fmt.Sprintf("%d\t%d\t", node.Data.FileInfo.Uid, node.Data.FileInfo.Gid)
}
fmt.Fprintf(w, "%s\t%s%s\t%s\n",
node.Data.FileInfo.Mode.String(),
uinfo,
formatSize(node.Data.FileInfo),
formatPath(node))
}
}
func formatSize(info filetree.FileInfo) string {
if info.TypeFlag == tar.TypeDir {
return "-"
}
if info.Size == 0 {
return "0"
}
return strings.Replace(humanize.Bytes(uint64(info.Size)), " ", "", -1)
}
func formatPath(node *filetree.FileNode) string {
ret := node.Path()
switch node.Data.FileInfo.TypeFlag {
case tar.TypeLink, tar.TypeSymlink:
ret += fmt.Sprintf(" -> %s", node.Data.FileInfo.Linkname)
}
return ret
}