forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty.go
167 lines (150 loc) · 4.59 KB
/
pretty.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package pretty
import (
"fmt"
"html/template"
"io"
"os"
"github.com/docker/go-units"
"github.com/pachyderm/pachyderm/src/client/pfs"
"github.com/pachyderm/pachyderm/src/server/pkg/pretty"
)
// PrintRepoHeader prints a repo header.
func PrintRepoHeader(w io.Writer, printAuth bool) {
if printAuth {
fmt.Fprint(w, "NAME\tCREATED\tSIZE\tACCESS LEVEL\t\n")
return
}
fmt.Fprint(w, "NAME\tCREATED\tSIZE\t\n")
}
// PrintRepoInfo pretty-prints repo info.
func PrintRepoInfo(w io.Writer, repoInfo *pfs.RepoInfo) {
fmt.Fprintf(w, "%s\t", repoInfo.Repo.Name)
fmt.Fprintf(
w,
"%s\t",
pretty.Ago(repoInfo.Created),
)
fmt.Fprintf(w, "%s\t", units.BytesSize(float64(repoInfo.SizeBytes)))
if repoInfo.AuthInfo != nil {
fmt.Fprintf(w, "%s\t", repoInfo.AuthInfo.AccessLevel.String())
}
fmt.Fprintln(w)
}
// PrintDetailedRepoInfo pretty-prints detailed repo info.
func PrintDetailedRepoInfo(repoInfo *pfs.RepoInfo) error {
template, err := template.New("RepoInfo").Funcs(funcMap).Parse(
`Name: {{.Repo.Name}}{{if .Description}}
Description: {{.Description}}{{end}}
Created: {{prettyAgo .Created}}
Size: {{prettySize .SizeBytes}}{{if .Provenance}}
Provenance: {{range .Provenance}} {{.Name}} {{end}}{{end}}{{if .AuthInfo}}
Access level: {{ .AuthInfo.AccessLevel.String }}{{end}}
`)
if err != nil {
return err
}
err = template.Execute(os.Stdout, repoInfo)
if err != nil {
return err
}
return nil
}
// PrintBranchHeader prints a branch header.
func PrintBranchHeader(w io.Writer) {
fmt.Fprint(w, "BRANCH\tHEAD\t\n")
}
// PrintBranch pretty-prints a Branch.
func PrintBranch(w io.Writer, branch *pfs.BranchInfo) {
fmt.Fprintf(w, "%s\t", branch.Name)
fmt.Fprintf(w, "%s\t\n", branch.Head.ID)
}
// PrintCommitInfoHeader prints a commit info header.
func PrintCommitInfoHeader(w io.Writer) {
fmt.Fprint(w, "REPO\tID\tPARENT\tSTARTED\tDURATION\tSIZE\t\n")
}
// PrintCommitInfo pretty-prints commit info.
func PrintCommitInfo(w io.Writer, commitInfo *pfs.CommitInfo) {
fmt.Fprintf(w, "%s\t", commitInfo.Commit.Repo.Name)
fmt.Fprintf(w, "%s\t", commitInfo.Commit.ID)
if commitInfo.ParentCommit != nil {
fmt.Fprintf(w, "%s\t", commitInfo.ParentCommit.ID)
} else {
fmt.Fprint(w, "<none>\t")
}
fmt.Fprintf(
w,
"%s\t",
pretty.Ago(commitInfo.Started),
)
if commitInfo.Finished != nil {
fmt.Fprintf(w, fmt.Sprintf("%s\t", pretty.TimeDifference(commitInfo.Started, commitInfo.Finished)))
fmt.Fprintf(w, "%s\t\n", units.BytesSize(float64(commitInfo.SizeBytes)))
} else {
fmt.Fprintf(w, "-\t")
// Open commits don't have meaningful size information
fmt.Fprintf(w, "-\t\n")
}
}
// PrintDetailedCommitInfo pretty-prints detailed commit info.
func PrintDetailedCommitInfo(commitInfo *pfs.CommitInfo) error {
template, err := template.New("CommitInfo").Funcs(funcMap).Parse(
`Commit: {{.Commit.Repo.Name}}/{{.Commit.ID}}{{if .ParentCommit}}
Parent: {{.ParentCommit.ID}} {{end}}
Started: {{prettyAgo .Started}}{{if .Finished}}
Finished: {{prettyAgo .Finished}} {{end}}
Size: {{prettySize .SizeBytes}}{{if .Provenance}}
Provenance: {{range .Provenance}} {{.Repo.Name}}/{{.ID}} {{end}} {{end}}
`)
if err != nil {
return err
}
err = template.Execute(os.Stdout, commitInfo)
if err != nil {
return err
}
return nil
}
// PrintFileInfoHeader prints a file info header.
func PrintFileInfoHeader(w io.Writer) {
fmt.Fprint(w, "NAME\tTYPE\tSIZE\t\n")
}
// PrintFileInfo pretty-prints file info.
// If recurse is false and directory size is 0, display "-" instead
// If fast is true and file size is 0, display "-" instead
func PrintFileInfo(w io.Writer, fileInfo *pfs.FileInfo) {
fmt.Fprintf(w, "%s\t", fileInfo.File.Path)
if fileInfo.FileType == pfs.FileType_FILE {
fmt.Fprint(w, "file\t")
} else {
fmt.Fprint(w, "dir\t")
}
fmt.Fprintf(w, "%s\t\n", units.BytesSize(float64(fileInfo.SizeBytes)))
}
// PrintDetailedFileInfo pretty-prints detailed file info.
func PrintDetailedFileInfo(fileInfo *pfs.FileInfo) error {
template, err := template.New("FileInfo").Funcs(funcMap).Parse(
`Path: {{.File.Path}}
Type: {{fileType .FileType}}
Size: {{prettySize .SizeBytes}}
Children: {{range .Children}} {{.}} {{end}}
`)
if err != nil {
return err
}
return template.Execute(os.Stdout, fileInfo)
}
type uint64Slice []uint64
func (s uint64Slice) Len() int { return len(s) }
func (s uint64Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s uint64Slice) Less(i, j int) bool { return s[i] < s[j] }
func fileType(fileType pfs.FileType) string {
if fileType == pfs.FileType_FILE {
return "file"
}
return "dir"
}
var funcMap = template.FuncMap{
"prettyAgo": pretty.Ago,
"prettySize": pretty.Size,
"fileType": fileType,
}