2014-10-23 05:59:21 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-10-31 22:46:22 +03:00
|
|
|
"io"
|
2014-10-23 05:59:21 +04:00
|
|
|
"os"
|
2014-10-23 17:27:00 +04:00
|
|
|
"path"
|
2014-12-20 00:54:20 +03:00
|
|
|
"strconv"
|
2015-01-06 14:05:17 +03:00
|
|
|
"strings"
|
2014-10-23 17:27:00 +04:00
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
2015-11-24 03:19:53 +03:00
|
|
|
|
|
|
|
"github.com/colinmarc/hdfs"
|
2014-10-23 05:59:21 +04:00
|
|
|
)
|
|
|
|
|
2015-01-06 14:05:17 +03:00
|
|
|
func ls(paths []string, long, all, humanReadable bool) {
|
2014-10-26 20:39:10 +03:00
|
|
|
paths, client, err := getClientAndExpandedPaths(paths)
|
2014-10-23 05:59:21 +04:00
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-10-26 20:39:10 +03:00
|
|
|
if len(paths) == 0 {
|
|
|
|
paths = []string{userDir()}
|
|
|
|
}
|
|
|
|
|
2014-11-04 22:57:35 +03:00
|
|
|
files := make([]string, 0, len(paths))
|
|
|
|
fileInfos := make([]os.FileInfo, 0, len(paths))
|
2014-10-26 20:39:10 +03:00
|
|
|
dirs := make([]string, 0, len(paths))
|
|
|
|
for _, p := range paths {
|
2014-10-31 22:46:22 +03:00
|
|
|
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 {
|
2014-11-04 22:57:35 +03:00
|
|
|
files = append(files, p)
|
|
|
|
fileInfos = append(fileInfos, fi)
|
2014-10-23 05:59:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(files) == 0 && len(dirs) == 1 {
|
2014-12-20 00:54:20 +03:00
|
|
|
printDir(client, dirs[0], long, all, humanReadable)
|
2014-10-23 05:59:21 +04:00
|
|
|
} else {
|
2014-10-23 17:27:00 +04:00
|
|
|
if long {
|
2015-01-06 14:36:56 +03:00
|
|
|
tw := lsTabWriter()
|
2014-11-04 22:57:35 +03:00
|
|
|
for i, p := range files {
|
2014-12-20 00:54:20 +03:00
|
|
|
printLong(tw, p, fileInfos[i], humanReadable)
|
2014-11-04 22:57:35 +03:00
|
|
|
}
|
|
|
|
|
2014-10-23 17:58:00 +04:00
|
|
|
tw.Flush()
|
|
|
|
} else {
|
2014-11-04 22:57:35 +03:00
|
|
|
for _, p := range files {
|
|
|
|
fmt.Println(p)
|
|
|
|
}
|
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-12-20 00:54:20 +03:00
|
|
|
printDir(client, dir, long, all, humanReadable)
|
2014-10-23 05:59:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 00:54:20 +03:00
|
|
|
func printDir(client *hdfs.Client, dir string, long, all, humanReadable bool) {
|
2014-10-31 22:46:22 +03:00
|
|
|
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 {
|
2015-01-06 14:36:56 +03:00
|
|
|
tw = lsTabWriter()
|
2014-10-23 17:27:00 +04:00
|
|
|
defer tw.Flush()
|
|
|
|
}
|
|
|
|
|
2014-10-23 16:13:42 +04:00
|
|
|
if all {
|
2014-10-23 17:27:00 +04:00
|
|
|
if long {
|
2014-10-31 22:46:22 +03:00
|
|
|
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, "..")
|
2014-10-31 22:46:22 +03:00
|
|
|
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-12-20 00:54:20 +03:00
|
|
|
printLong(tw, ".", dirInfo, humanReadable)
|
|
|
|
printLong(tw, "..", parentInfo, humanReadable)
|
2014-10-23 17:27:00 +04:00
|
|
|
} else {
|
|
|
|
fmt.Println(".")
|
|
|
|
fmt.Println("..")
|
|
|
|
}
|
2014-10-23 16:13:42 +04:00
|
|
|
}
|
|
|
|
|
2014-10-31 22:46:22 +03:00
|
|
|
var partial []os.FileInfo
|
|
|
|
for ; err != io.EOF; partial, err = dirReader.Readdir(100) {
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-20 00:54:20 +03:00
|
|
|
printFiles(tw, partial, long, all, humanReadable)
|
2015-01-06 14:36:56 +03:00
|
|
|
}
|
2014-10-31 22:46:22 +03:00
|
|
|
|
2015-01-06 14:36:56 +03:00
|
|
|
if long {
|
|
|
|
tw.Flush()
|
2014-10-31 22:46:22 +03:00
|
|
|
}
|
2014-10-23 05:59:21 +04:00
|
|
|
}
|
|
|
|
|
2014-12-20 00:54:20 +03:00
|
|
|
func printFiles(tw *tabwriter.Writer, files []os.FileInfo, long, all, humanReadable bool) {
|
2014-10-23 05:59:21 +04:00
|
|
|
for _, file := range files {
|
2014-11-04 22:57:35 +03:00
|
|
|
if !all && strings.HasPrefix(file.Name(), ".") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if long {
|
2014-12-20 00:54:20 +03:00
|
|
|
printLong(tw, file.Name(), file, humanReadable)
|
2014-11-04 22:57:35 +03:00
|
|
|
} 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
|
|
|
|
2014-12-20 00:54:20 +03:00
|
|
|
func printLong(tw *tabwriter.Writer, name string, info os.FileInfo, humanReadable bool) {
|
2014-10-23 17:27:00 +04:00
|
|
|
fi := info.(*hdfs.FileInfo)
|
|
|
|
// mode owner group size date(\w tab) time/year name
|
|
|
|
mode := fi.Mode().String()
|
|
|
|
owner := fi.Owner()
|
|
|
|
group := fi.OwnerGroup()
|
2014-12-20 00:54:20 +03:00
|
|
|
size := strconv.FormatInt(fi.Size(), 10)
|
|
|
|
if humanReadable {
|
2017-01-31 05:28:00 +03:00
|
|
|
size = formatBytes(uint64(fi.Size()))
|
2014-12-20 00:54:20 +03:00
|
|
|
}
|
2014-10-23 17:27:00 +04:00
|
|
|
|
|
|
|
modtime := fi.ModTime()
|
2016-02-13 14:30:09 +03:00
|
|
|
date := modtime.Format("Jan _2")
|
2014-10-23 17:27:00 +04:00
|
|
|
var timeOrYear string
|
|
|
|
if modtime.Year() == time.Now().Year() {
|
|
|
|
timeOrYear = modtime.Format("15:04")
|
|
|
|
} else {
|
2016-04-04 02:49:58 +03:00
|
|
|
timeOrYear = modtime.Format("2006")
|
2014-10-23 17:27:00 +04:00
|
|
|
}
|
|
|
|
|
2014-12-20 00:54:20 +03:00
|
|
|
fmt.Fprintf(tw, "%s \t%s \t %s \t %s \t%s \t%s \t%s\n",
|
2014-10-23 17:27:00 +04:00
|
|
|
mode, owner, group, size, date, timeOrYear, name)
|
|
|
|
}
|
|
|
|
|
2015-01-06 14:36:56 +03:00
|
|
|
func lsTabWriter() *tabwriter.Writer {
|
|
|
|
return tabwriter.NewWriter(os.Stdout, 3, 8, 0, ' ', tabwriter.AlignRight|tabwriter.TabIndent)
|
2014-12-20 00:54:20 +03:00
|
|
|
}
|