-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.go
84 lines (72 loc) · 1.73 KB
/
paths.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
// Copyright (c) 2022-2023 Jean-Francois Smigielski
// 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 main
import (
"encoding/json"
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/jfsmig/cui"
)
func main() {
if err := cui.Monitor(&directorySource{}, "/var/log"); err != nil {
log.Fatalln(err)
}
}
type directorySource struct{}
type fileItem struct {
Path string `json:"path"`
Size int64 `json:"size"`
Mode fs.FileMode `json:"mode"`
CTime time.Time `json:"ctime"`
}
func (fi *fileItem) GetPrimaryKey() string { return "path" }
func (fi *fileItem) GetKeys() []string { return []string{"path", "size", "mode", "ctime"} }
func (fi *fileItem) GetValue(k string) string {
switch k {
case "path":
return fi.Path
case "size":
return strconv.FormatInt(fi.Size, 10)
case "mode":
return fi.Mode.String()
case "ctime":
return fi.CTime.String()
default:
return "-"
}
}
func (fi *fileItem) GetDetail() string {
var builder strings.Builder
encoder := json.NewEncoder(&builder)
encoder.SetIndent("", " ")
encoder.Encode(*fi)
return builder.String()
}
func (dl *directorySource) FetchAll(query string) ([]cui.MonitoredItem, error) {
var out []cui.MonitoredItem
if query == "" {
return out, errors.New("Empty query")
}
if !filepath.IsAbs(query) {
return out, errors.New("Relative query path")
}
entries, err := os.ReadDir(query)
for _, entry := range entries {
info, _ := entry.Info()
out = append(out, &fileItem{
entry.Name(),
info.Size(),
info.Mode(),
info.ModTime(),
})
}
return out, err
}