colinmarc-hdfs/cmd/hdfs/ls.go

147 строки
2.7 KiB
Go
Исходник Обычный вид История

2014-10-23 05:59:21 +04:00
package main
import (
"fmt"
"github.com/colinmarc/hdfs"
"io"
2014-10-23 05:59:21 +04:00
"os"
2014-10-23 17:27:00 +04:00
"path"
2014-10-23 16:13:42 +04:00
"strings"
2014-10-23 17:27:00 +04:00
"text/tabwriter"
"time"
2014-10-23 05:59:21 +04:00
)
2014-10-23 18:57:41 +04:00
func ls(paths []string, long, all bool) int {
paths, client, err := getClientAndExpandedPaths(paths)
2014-10-23 05:59:21 +04:00
if err != nil {
fatal(err)
}
if len(paths) == 0 {
paths = []string{userDir()}
}
files := make([]os.FileInfo, 0, len(paths))
dirs := make([]string, 0, len(paths))
for _, p := range paths {
fi, err := client.Stat(p)
2014-10-23 05:59:21 +04:00
if err != nil {
2014-10-28 15:49:30 +03:00
fatal(err)
2014-10-23 05:59:21 +04:00
}
if fi.IsDir() {
dirs = append(dirs, p)
} else {
files = append(files, fi)
}
}
if len(files) == 0 && len(dirs) == 1 {
2014-10-23 16:13:42 +04:00
printDir(client, dirs[0], long, all)
2014-10-23 05:59:21 +04:00
} else {
2014-10-23 17:27:00 +04:00
var tw *tabwriter.Writer
if long {
tw = defaultTabWriter()
printFiles(tw, files, true, all)
tw.Flush()
} else {
printFiles(nil, files, false, all)
2014-10-23 17:27:00 +04:00
}
2014-10-31 20:46:27 +03:00
for i, dir := range dirs {
if i > 0 {
fmt.Println()
}
fmt.Printf("%s/:\n", dir)
2014-10-23 16:13:42 +04:00
printDir(client, dir, long, all)
2014-10-23 05:59:21 +04:00
}
}
2014-10-23 18:57:41 +04:00
return 0
2014-10-23 05:59:21 +04:00
}
2014-10-23 16:13:42 +04:00
func printDir(client *hdfs.Client, dir string, long, all bool) {
dirReader, err := client.Open(dir)
2014-10-23 05:59:21 +04:00
if err != nil {
2014-10-28 15:49:30 +03:00
fatal(err)
2014-10-23 05:59:21 +04:00
}
2014-10-23 17:27:00 +04:00
var tw *tabwriter.Writer
if long {
tw = defaultTabWriter()
defer tw.Flush()
}
2014-10-23 16:13:42 +04:00
if all {
2014-10-23 17:27:00 +04:00
if long {
dirInfo, err := client.Stat(dir)
2014-10-23 17:27:00 +04:00
if err != nil {
2014-10-28 15:49:30 +03:00
fatal(err)
2014-10-23 17:27:00 +04:00
}
2014-10-23 19:08:49 +04:00
parentPath := path.Join(dir, "..")
parentInfo, err := client.Stat(parentPath)
2014-10-23 17:27:00 +04:00
if err != nil {
2014-10-28 15:49:30 +03:00
fatal(err)
2014-10-23 17:27:00 +04:00
}
2014-10-23 19:08:49 +04:00
printLong(tw, ".", dirInfo)
printLong(tw, "..", parentInfo)
2014-10-23 17:27:00 +04:00
} else {
fmt.Println(".")
fmt.Println("..")
}
2014-10-23 16:13:42 +04:00
}
var partial []os.FileInfo
for ; err != io.EOF; partial, err = dirReader.Readdir(100) {
if err != nil {
fatal(err)
}
printFiles(tw, partial, long, all)
if long {
tw.Flush()
}
}
2014-10-23 05:59:21 +04:00
}
2014-10-23 17:27:00 +04:00
func printFiles(tw *tabwriter.Writer, files []os.FileInfo, long, all bool) {
2014-10-23 05:59:21 +04:00
for _, file := range files {
2014-10-23 16:13:42 +04:00
if all || !strings.HasPrefix(file.Name(), ".") {
2014-10-23 17:27:00 +04:00
if long {
printLong(tw, file.Name(), file)
} else {
fmt.Println(file.Name())
}
2014-10-23 16:13:42 +04:00
}
2014-10-23 05:59:21 +04:00
}
}
2014-10-23 17:27:00 +04:00
func printLong(tw *tabwriter.Writer, name string, info os.FileInfo) {
fi := info.(*hdfs.FileInfo)
// mode owner group size date(\w tab) time/year name
mode := fi.Mode().String()
owner := fi.Owner()
group := fi.OwnerGroup()
size := fi.Size()
modtime := fi.ModTime()
date := modtime.Format("Jan\t2")
var timeOrYear string
if modtime.Year() == time.Now().Year() {
timeOrYear = modtime.Format("15:04")
} else {
timeOrYear = string(modtime.Year())
}
fmt.Fprintf(tw, "%s \t%s \t %s \t %d \t%s \t%s \t%s\n",
mode, owner, group, size, date, timeOrYear, name)
}
func defaultTabWriter() *tabwriter.Writer {
return tabwriter.NewWriter(os.Stdout, 3, 0, 0, ' ', tabwriter.AlignRight|tabwriter.TabIndent)
}