2020-03-08 03:24:41 +03:00
|
|
|
package ls
|
|
|
|
import(
|
|
|
|
"os"
|
|
|
|
"fmt"
|
|
|
|
"flag"
|
|
|
|
"strings"
|
2020-05-06 03:57:54 +03:00
|
|
|
"regexp"
|
2020-03-08 03:24:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func IsDir(p string) (bool, error) {
|
|
|
|
finfo, e := os.Stat(p)
|
|
|
|
if e != nil {
|
|
|
|
return false, e
|
|
|
|
}
|
|
|
|
return finfo.IsDir(), e
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadDir(p string) ([]os.FileInfo, error) {
|
|
|
|
f, e := os.Open(p)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
l, e := f.Readdir(-1)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Stat(p string) (os.FileInfo, error) {
|
|
|
|
f, e := os.Open(p)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
s, e := f.Stat()
|
|
|
|
f.Close()
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ls(p string, fold int) error {
|
|
|
|
isDir, e := IsDir(p)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2020-05-06 03:57:54 +03:00
|
|
|
// Delete repeating slashes.
|
|
|
|
slash := regexp.MustCompile("/+")
|
|
|
|
p = slash.ReplaceAllString(p, "/")
|
|
|
|
if p != "/" { // Do not trim if it is root dir.
|
|
|
|
p = strings.TrimRight(p, "/")
|
|
|
|
}
|
|
|
|
|
2020-03-08 03:24:41 +03:00
|
|
|
|
2020-05-06 03:36:41 +03:00
|
|
|
if !isDir {
|
2020-05-06 03:57:54 +03:00
|
|
|
fmt.Println(p)
|
2020-05-06 03:36:41 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if fold>0 {
|
|
|
|
/* It's a directory and it can be ls-ed. */
|
2020-05-06 03:57:54 +03:00
|
|
|
l, e := ReadDir(p)
|
2020-04-23 10:41:19 +03:00
|
|
|
if e!=nil {
|
2020-03-08 03:24:41 +03:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
for _, f := range l {
|
2020-05-06 03:57:54 +03:00
|
|
|
s := p+"/"+f.Name()
|
2020-05-06 03:36:41 +03:00
|
|
|
if b, _ := IsDir(s) ; b && fold!=1 {
|
|
|
|
fmt.Println(s)
|
2020-04-23 10:41:19 +03:00
|
|
|
}
|
2020-05-06 03:36:41 +03:00
|
|
|
ls(s, fold-1)
|
2020-03-08 03:24:41 +03:00
|
|
|
}
|
2020-05-05 09:04:29 +03:00
|
|
|
} else {
|
2020-05-06 03:36:41 +03:00
|
|
|
/* It's finish directory. Fold==0 or fold<0. */
|
2020-05-06 03:57:54 +03:00
|
|
|
fmt.Println(p)
|
2020-03-08 03:24:41 +03:00
|
|
|
}
|
2020-04-23 10:41:19 +03:00
|
|
|
|
2020-03-08 03:24:41 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Run(args []string) int {
|
|
|
|
status := 0
|
|
|
|
arg0 := args[0]
|
|
|
|
args = args[1:]
|
|
|
|
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
|
2020-05-05 09:04:29 +03:00
|
|
|
var foldLvl int
|
2020-04-01 22:25:52 +03:00
|
|
|
flagSet.IntVar(&foldLvl, "r", 1, "List recursively with choosing deepness, can't be negative or zero.")
|
2020-03-08 03:24:41 +03:00
|
|
|
flagSet.Usage = func() {
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage of %s: %s [files]\n", arg0, arg0)
|
|
|
|
flagSet.PrintDefaults()
|
|
|
|
}
|
|
|
|
flagSet.Parse(args)
|
2020-05-05 09:04:29 +03:00
|
|
|
args = flagSet.Args()
|
|
|
|
|
|
|
|
if foldLvl<0 {
|
2020-04-01 22:25:52 +03:00
|
|
|
flagSet.Usage()
|
|
|
|
return 1
|
|
|
|
}
|
2020-05-05 09:04:29 +03:00
|
|
|
|
|
|
|
if foldLvl==0 && len(args)==0 {
|
2020-04-01 22:25:52 +03:00
|
|
|
flagSet.Usage()
|
|
|
|
return 1
|
|
|
|
}
|
2020-05-05 09:04:29 +03:00
|
|
|
|
2020-03-08 03:24:41 +03:00
|
|
|
if len(args) == 0 {
|
2020-05-05 09:04:29 +03:00
|
|
|
foldLvl -= 1
|
|
|
|
if l, e := ReadDir(".") ; e != nil {
|
2020-03-08 03:24:41 +03:00
|
|
|
status = 1
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
|
|
|
|
} else {
|
|
|
|
for _, f := range l {
|
2020-05-05 09:04:29 +03:00
|
|
|
isDir, _ := IsDir(f.Name())
|
2020-05-06 03:36:41 +03:00
|
|
|
|
|
|
|
fmt.Println(f.Name())
|
2020-05-05 09:04:29 +03:00
|
|
|
if isDir && foldLvl>0 {
|
|
|
|
e := ls(f.Name(), foldLvl)
|
|
|
|
if e!=nil {
|
|
|
|
status = 1
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
|
|
|
|
}
|
2020-04-22 19:44:39 +03:00
|
|
|
}
|
2020-03-08 03:24:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, p := range args {
|
|
|
|
e := ls(p, foldLvl)
|
|
|
|
if e != nil {
|
|
|
|
status = 1
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status
|
|
|
|
}
|