-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilestat.go
146 lines (124 loc) · 3.12 KB
/
filestat.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
package main
import (
// "flag"
"os"
"path/filepath"
"fmt"
"log"
"github.com/urfave/cli"
)
var app = cli.NewApp()
// Files represents a list of files with their metadata
type Files struct {
Files []FileMetadata `json:"files"`
}
// ExtInfo represents informations about an extension
type ExtInfo struct {
Extension string `json:"extension"`
NumOccurrences int64 `json:"num_occurrences"`
}
// FileMetadata represent a file with its metadata
type FileMetadata struct {
Path string `json:"path"` // the file's absolute path
Size int64 `json:"size"` //the file size
IsBinary bool `json:"is_binary"` // whether the file is a binary file or a simple text file
}
// FileStats represents statistics about files
type FileStats struct {
Numfiles int64 `json:"num_files"`
LargestFile FileMetadata `json:"largest_file"`
AverageFileSize float64 `json:"avg_file_size"`
MostFrequentExt ExtInfo `json:"most_frequent_ext"`
TextPercentage float32 `json:"text_percentage"`
MostRecentPaths []string `json:"most_recent_paths"`
}
// AddFile adds a file in a Files list
func (f *Files) AddFile(filename ...string) error {
for _, i := range filename {
abspath, err := filepath.Abs(i)
if err != nil {
return err
}
stat, err := os.Stat(abspath)
if err != nil {
return err
}
f.Files = append(f.Files, FileMetadata{Path: abspath, Size: stat.Size()})
}
return nil
}
// getLargestFile retrieves largest file in list
func (f *Files) getLargestFile() FileMetadata {
l := FileMetadata{Path: "", Size: 0}
for _, i := range f.Files {
if i.Size > l.Size {
l = i
}
}
return l
}
// getAverageFile retrieves average size of files in list
func (f *Files) getAverageFile() float64 {
var sum int64
for _, i := range f.Files {
sum += i.Size
}
return float64(sum) / float64(len(f.Files))
}
// MostFrequentExt returns most frequent extension
func (f *Files) MostFrequentExt() ExtInfo {
m := make(map[string]int64)
for _, i := range f.Files {
m[filepath.Ext(i.Path)]++
}
p := ExtInfo{}
for i, j := range m {
if j > p.NumOccurrences {
p.Extension = i
p.NumOccurrences = j
}
}
return p
}
// GetStats returns statistics about files in list
func (f *Files) GetStats() FileStats {
filestats := FileStats{
Numfiles: int64(len(f.Files)),
LargestFile: f.getLargestFile(),
AverageFileSize: f.getAverageFile(),
MostFrequentExt: f.MostFrequentExt(),
// TextPercentage: f.getTextPercentage(),
// MostRecentPaths: f.getMostRecentPath(),
}
return filestats
}
func info() {
app.Name = "file stat utility"
app.Author = "rajibmitra,Issif"
app.Usage = "An example CLI for checking the stats of your files"
app.Version = "0.0.1"
}
func commands() {
app.Commands = []cli.Command{
{
Name: "addfile",
Aliases: []string{"file"},
Usage: "see your file stats",
Action: func(c *cli.Context) {
},
},
}
}
func main() {
info()
commands()
err := app.Run(os.Args)
file := os.Args[len(os.Args)-1]
f := Files{Files: []FileMetadata{}}
f.AddFile(file)
fmt.Printf("%#v\n", f.GetStats())
fmt.Println(file)
if err != nil {
log.Fatal(err)
}
}