ls: Simplified. No "-d" flag. Use "-r 0" when with args and "-r 1" when without.
This commit is contained in:
parent
48e278bad9
commit
3ac9732a26
1 changed files with 26 additions and 25 deletions
51
ls/ls.go
51
ls/ls.go
|
@ -6,11 +6,6 @@ import(
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var(
|
|
||||||
dirFlag bool
|
|
||||||
foldLvl int
|
|
||||||
)
|
|
||||||
|
|
||||||
func IsDir(p string) (bool, error) {
|
func IsDir(p string) (bool, error) {
|
||||||
finfo, e := os.Stat(p)
|
finfo, e := os.Stat(p)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
|
@ -51,22 +46,20 @@ func ls(p string, fold int) error {
|
||||||
|
|
||||||
pp := strings.TrimRight(p, "/")
|
pp := strings.TrimRight(p, "/")
|
||||||
|
|
||||||
if !isDir || dirFlag || fold<1 {
|
if isDir && fold>0 {
|
||||||
fmt.Println(pp);
|
|
||||||
}else{
|
|
||||||
l, e := ReadDir(pp)
|
l, e := ReadDir(pp)
|
||||||
if e!=nil {
|
if e!=nil {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
for _, f := range l {
|
for _, f := range l {
|
||||||
s := pp+"/"+f.Name()
|
s := pp+"/"+f.Name()
|
||||||
if b, _:=IsDir(s) ; b {
|
fmt.Println(s)
|
||||||
fmt.Println(s)
|
if b, _ :=IsDir(s) ; b {
|
||||||
}
|
|
||||||
if 0<fold {
|
|
||||||
ls(s, fold-1)
|
ls(s, fold-1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println(pp)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -77,34 +70,42 @@ func Run(args []string) int {
|
||||||
arg0 := args[0]
|
arg0 := args[0]
|
||||||
args = args[1:]
|
args = args[1:]
|
||||||
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
|
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
|
||||||
|
var foldLvl int
|
||||||
flagSet.IntVar(&foldLvl, "r", 1, "List recursively with choosing deepness, can't be negative or zero.")
|
flagSet.IntVar(&foldLvl, "r", 1, "List recursively with choosing deepness, can't be negative or zero.")
|
||||||
flagSet.BoolVar(&dirFlag, "d", false, "List directory as usual file, doesn't work with with recursive level not equal 1. ")
|
|
||||||
flagSet.Usage = func() {
|
flagSet.Usage = func() {
|
||||||
fmt.Fprintf(os.Stderr, "Usage of %s: %s [files]\n", arg0, arg0)
|
fmt.Fprintf(os.Stderr, "Usage of %s: %s [files]\n", arg0, arg0)
|
||||||
flagSet.PrintDefaults()
|
flagSet.PrintDefaults()
|
||||||
}
|
}
|
||||||
flagSet.Parse(args)
|
flagSet.Parse(args)
|
||||||
if foldLvl<1 {
|
args = flagSet.Args()
|
||||||
|
|
||||||
|
if foldLvl<0 {
|
||||||
|
flagSet.Usage()
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if foldLvl==0 && len(args)==0 {
|
||||||
flagSet.Usage()
|
flagSet.Usage()
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
if foldLvl!=1 && dirFlag {
|
|
||||||
flagSet.Usage()
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
args = flagSet.Args()
|
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
l, e := ReadDir(".")
|
foldLvl -= 1
|
||||||
if e != nil {
|
if l, e := ReadDir(".") ; e != nil {
|
||||||
status = 1
|
status = 1
|
||||||
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
|
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
|
||||||
} else {
|
} else {
|
||||||
for _, f := range l {
|
for _, f := range l {
|
||||||
e := ls(f.Name(), foldLvl-1)
|
isDir, _ := IsDir(f.Name())
|
||||||
if e!=nil {
|
if isDir && foldLvl>0 {
|
||||||
status = 1
|
fmt.Println(f.Name())
|
||||||
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
|
e := ls(f.Name(), foldLvl)
|
||||||
|
if e!=nil {
|
||||||
|
status = 1
|
||||||
|
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println(f.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue