/
ls.go
164 lines (148 loc) · 3.17 KB
/
ls.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
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"time"
"code.cloudfoundry.org/bytefmt"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
)
type flagdef struct {
isLong bool
isDronly bool
isFOnly bool
isHr bool
isTree bool
}
type fileRef struct {
path string
name string
}
var walk []fileRef
func checkerr(err error) {
if err != nil {
panic(err)
}
}
func retsize(nsize int64, hr bool) string {
if hr {
return bytefmt.ByteSize(uint64(nsize))
}
return fmt.Sprintf("%v", nsize)
}
func inspect(spath string, finfo os.FileInfo, err error) error {
var sname string
if err != nil {
sname = err.Error()
}
sname = finfo.Name()
foo := fileRef{path: spath, name: sname}
walk = append(walk, foo)
return nil
}
func serveDecoratedDirTree(dir string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Path", "Name"})
dirname := dir
err := filepath.Walk(dir, inspect)
checkerr(err)
for _, item := range walk {
dirname = item.path
data := []string{
filepath.Dir(dirname),
item.name,
}
table.Append(data)
}
table.SetAutoMergeCells(true)
table.SetAutoWrapText(true)
table.SetRowLine(true)
table.Render()
}
func serveDecoratedDir(dir string, flags flagdef) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Size", "Perms", "At", "Dir"})
f, err := os.OpenFile(dir, os.O_RDONLY, 0666)
checkerr(err)
finfo, err := f.Readdir(0)
checkerr(err)
for _, item := range finfo {
if flags.isDronly && item.Mode().IsRegular() {
continue
}
if flags.isFOnly && item.IsDir() {
continue
}
data := []string{
item.Name(),
retsize(item.Size(), flags.isHr),
item.Mode().String(),
item.ModTime().Format("2006-01-02 15:04:05"),
strconv.FormatBool(item.IsDir()),
}
table.Append(data)
}
table.Render()
}
func serveDir(dir string) {
f, err := os.OpenFile(dir, os.O_RDONLY, 0666)
checkerr(err)
files, err := f.Readdirnames(0)
checkerr(err)
for _, file := range files {
fmt.Println(file)
}
}
func main() {
app := cli.NewApp()
app.Name = "ls ( implemented in golang )"
app.Usage = "ls [flags] [command][args]"
app.Author = "Viswa Kumar"
app.Email = ""
app.Version = "0.1"
app.Copyright = "MIT Licensed"
app.Compiled = time.Now()
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "l, long",
Usage: "include extended information",
},
cli.BoolFlag{
Name: "d, dronly",
Usage: "include only directories",
},
cli.BoolFlag{
Name: "f, fileonly",
Usage: "include only regular files",
},
cli.BoolFlag{
Name: "hr, humanfriendly",
Usage: "view size in humar readable format",
},
cli.BoolFlag{
Name: "t, tree",
Usage: "view tree structure ( recursive lookup )",
},
}
app.Action = func(c *cli.Context) error {
decor := c.Bool("long") || c.Bool("dronly") || c.Bool("fileonly") || c.Bool("hr") || c.Bool("t")
flags := flagdef{c.Bool("long"), c.Bool("dronly"), c.Bool("fileonly"), c.Bool("hr"), c.Bool("t")}
dir := "."
if c.NArg() > 0 {
dir = c.Args()[0]
}
if decor {
if flags.isTree {
serveDecoratedDirTree(dir)
} else {
serveDecoratedDir(dir, flags)
}
} else {
serveDir(dir)
}
return nil
}
app.Run(os.Args)
}