-
Notifications
You must be signed in to change notification settings - Fork 456
/
diskusage.go
129 lines (103 loc) · 3.27 KB
/
diskusage.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package talos
import (
"context"
"errors"
"fmt"
"os"
"strconv"
"text/tabwriter"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/helpers"
machineapi "github.com/siderolabs/talos/pkg/machinery/api/machine"
"github.com/siderolabs/talos/pkg/machinery/client"
)
var (
all bool
threshold int64
)
// duCmd represents the du command.
var duCmd = &cobra.Command{
Use: "usage [path1] [path2] ... [pathN]",
Aliases: []string{"du"},
Short: "Retrieve a disk usage",
Long: ``,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveError | cobra.ShellCompDirectiveNoFileComp
}
var completeOnlyPaths []string
for _, path := range completePathFromNode(toComplete) {
if path[len(path)-1:] == "/" {
completeOnlyPaths = append(completeOnlyPaths, path)
}
}
return completeOnlyPaths, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
return WithClient(func(ctx context.Context, c *client.Client) error {
var paths []string
if len(args) == 0 {
paths = []string{"/"}
} else {
paths = args
}
stream, err := c.DiskUsage(ctx, &machineapi.DiskUsageRequest{
RecursionDepth: recursionDepth + 1,
All: all,
Threshold: threshold,
Paths: paths,
})
if err != nil {
return fmt.Errorf("error fetching disk usage: %s", err)
}
addedHeader := false
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
stringifySize := func(s int64) string {
if humanizeFlag {
return humanize.Bytes(uint64(s))
}
return strconv.FormatInt(s, 10)
}
defer w.Flush() //nolint:errcheck
return helpers.ReadGRPCStream(stream, func(info *machineapi.DiskUsageInfo, node string, multipleNodes bool) error {
if info.Error != "" {
return helpers.NonFatalError(errors.New(info.Error))
}
pattern := "%s\t%s\n"
size := stringifySize(info.Size)
args := []interface{}{
size, info.RelativeName,
}
if info.Metadata != nil && info.Metadata.Hostname != "" {
multipleNodes = true
node = info.Metadata.Hostname
}
if !addedHeader {
if multipleNodes {
fmt.Fprintln(w, "NODE\tSIZE\tNAME")
} else {
fmt.Fprintln(w, "SIZE\tNAME")
}
addedHeader = true
}
if multipleNodes {
pattern = "%s\t%s\t%s\n"
args = append([]interface{}{node}, args...)
}
fmt.Fprintf(w, pattern, args...)
return nil
})
})
},
}
func init() {
duCmd.Flags().BoolVarP(&humanizeFlag, "humanize", "H", false, "humanize size and time in the output")
duCmd.Flags().BoolVarP(&all, "all", "a", false, "write counts for all files, not just directories")
duCmd.Flags().Int64VarP(&threshold, "threshold", "t", 0, "threshold exclude entries smaller than SIZE if positive, or entries greater than SIZE if negative")
duCmd.Flags().Int32VarP(&recursionDepth, "depth", "d", 0, "maximum recursion depth")
addCommand(duCmd)
}