File: stats.go

package info (click to toggle)
continuity 0.0~git20180216.d8fb858-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 620 kB
  • sloc: makefile: 46; sh: 28; asm: 3
file content (58 lines) | stat: -rw-r--r-- 1,296 bytes parent folder | download
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
package commands

import (
	"fmt"
	"log"
	"os"

	"github.com/dustin/go-humanize"
	"github.com/spf13/cobra"
)

var (
	StatsCmd = &cobra.Command{
		Use:   "stats <manifest>",
		Short: "display statistics about the specified manifest",
		Run: func(cmd *cobra.Command, args []string) {
			if len(args) != 1 {
				log.Fatalln("please specify a manifest")
			}

			bm, err := readManifestFile(args[0])
			if err != nil {
				log.Fatalf("error reading manifest: %v", err)
			}

			var stats struct {
				resources   int
				files       int
				directories int
				totalSize   int64
				symlinks    int
			}

			for _, entry := range bm.Resource {
				stats.resources++
				stats.totalSize += int64(entry.Size)

				mode := os.FileMode(entry.Mode)
				if mode.IsRegular() {
					stats.files += len(entry.Path) // count hardlinks!
				} else if mode.IsDir() {
					stats.directories++
				} else if mode&os.ModeSymlink != 0 {
					stats.symlinks++
				}
			}

			w := newTabwriter(os.Stdout)
			defer w.Flush()

			fmt.Fprintf(w, "resources\t%v\n", stats.resources)
			fmt.Fprintf(w, "directories\t%v\n", stats.directories)
			fmt.Fprintf(w, "files\t%v\n", stats.files)
			fmt.Fprintf(w, "symlinks\t%v\n", stats.symlinks)
			fmt.Fprintf(w, "size\t%v\n", humanize.Bytes(uint64(stats.totalSize)))
		},
	}
)