/
main.go
170 lines (144 loc) · 3.6 KB
/
main.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
168
169
170
// Copyright ©2018 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Command xrd-ls lists directory contents on a remote xrootd server.
//
// Usage:
//
// $> xrd-ls [OPTIONS] <dir-1> [<dir-2> [...]]
//
// Example:
//
// $> xrd-ls root://server.example.com/some/dir
// $> xrd-ls -l root://server.example.com/some/dir
// $> xrd-ls -R root://server.example.com/some/dir
// $> xrd-ls -l -R root://server.example.com/some/dir
//
// Options:
// -R list subdirectories recursively
// -l use a long listing format
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"path"
"text/tabwriter"
"go-hep.org/x/hep/xrootd"
"go-hep.org/x/hep/xrootd/xrdfs"
"go-hep.org/x/hep/xrootd/xrdio"
)
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `xrd-ls lists directory contents on a remote xrootd server.
Usage:
$> xrd-ls [OPTIONS] <dir-1> [<dir-2> [...]]
Example:
$> xrd-ls root://server.example.com/some/dir
$> xrd-ls -l root://server.example.com/some/dir
$> xrd-ls -R root://server.example.com/some/dir
$> xrd-ls -l -R root://server.example.com/some/dir
Options:
`)
flag.PrintDefaults()
}
}
func main() {
log.SetPrefix("xrd-ls: ")
log.SetFlags(0)
var (
recFlag = flag.Bool("R", false, "list subdirectories recursively")
longFlag = flag.Bool("l", false, "use a long listing format")
)
flag.Parse()
if flag.NArg() == 0 {
flag.Usage()
log.Fatalf("missing directory operand")
}
for i, dir := range flag.Args() {
if i > 0 {
// separate consecutive files by an empty line
fmt.Printf("\n")
}
err := xrdls(dir, *longFlag, *recFlag)
if err != nil {
log.Fatalf("could not list %q content: %+v", dir, err)
}
}
}
func xrdls(name string, long, recursive bool) error {
url, err := xrdio.Parse(name)
if err != nil {
return fmt.Errorf("could not parse %q: %w", name, err)
}
ctx := context.Background()
c, err := xrootd.NewClient(ctx, url.Addr, url.User)
if err != nil {
return fmt.Errorf("could not create client: %w", err)
}
defer c.Close()
fs := c.FS()
fi, err := fs.Stat(ctx, url.Path)
// TODO fi.Name() here is an empty string (see handling in format() below)
if err != nil {
return fmt.Errorf("could not stat %q: %w", url.Path, err)
}
err = display(ctx, fs, url.Path, fi, long, recursive)
if err != nil {
return err
}
return nil
}
func display(ctx context.Context, fs xrdfs.FileSystem, root string, fi os.FileInfo, long, recursive bool) error {
if !fi.IsDir() {
// TODO fi.Name() here is an empty string (see handling in format() below)
format(os.Stdout, root, fi, long)
return nil
}
end := ""
if recursive {
end = ":"
}
dir := path.Join(root, fi.Name())
fmt.Printf("%s%s\n", dir, end)
if long {
fmt.Printf("total %d\n", fi.Size())
}
ents, err := fs.Dirlist(ctx, dir)
if err != nil {
return fmt.Errorf("could not list dir %q: %w", dir, err)
}
o := tabwriter.NewWriter(os.Stdout, 8, 4, 0, ' ', tabwriter.AlignRight)
for _, e := range ents {
format(o, dir, e, long)
}
o.Flush()
if recursive {
for _, e := range ents {
if !e.IsDir() {
continue
}
// make an empty line before going into a subdirectory.
fmt.Printf("\n")
err := display(ctx, fs, dir, e, long, recursive)
if err != nil {
return err
}
}
}
return nil
}
func format(o io.Writer, root string, fi os.FileInfo, long bool) {
if !long {
fmt.Fprintf(o, "%s\n", path.Join(root, fi.Name()))
return
}
name := fi.Name()
if name == "" {
name = root
}
fmt.Fprintf(o, "%v\t %d\t %s\t %s\n", fi.Mode(), fi.Size(), fi.ModTime().Format("Jan 02 15:04"), name)
}