Selaa lähdekoodia

Add option to always list files

Lucas Stadler 9 vuotta sitten
vanhempi
commit
99697c5ed3
1 muutettua tiedostoa jossa 29 lisäystä ja 2 poistoa
  1. 29 2
      go/explore/explore.go

+ 29 - 2
go/explore/explore.go

@ -1,6 +1,7 @@
1 1
package main
2 2
3 3
import (
4
	"flag"
4 5
	"fmt"
5 6
	"os"
6 7
	"os/exec"
@ -8,10 +9,20 @@ import (
8 9
	"strings"
9 10
)
10 11
12
var options struct {
13
	listFiles bool
14
}
15
16
func init() {
17
	flag.BoolVar(&options.listFiles, "l", false, "Always list files")
18
}
19
11 20
func main() {
21
	flag.Parse()
22
12 23
	dir := "."
13
	if len(os.Args) > 1 {
14
		dir = os.Args[1]
24
	if flag.NArg() > 0 {
25
		dir = flag.Arg(0)
15 26
	}
16 27
17 28
	f, err := os.Open(dir)
@ -26,6 +37,14 @@ func main() {
26 37
		os.Exit(1)
27 38
	}
28 39
40
	if options.listFiles {
41
		cmd := exec.Command("ls", dir)
42
		cmd.Stdin = os.Stdin
43
		cmd.Stdout = os.Stdout
44
		cmd.Stderr = os.Stderr
45
		cmd.Run()
46
	}
47
29 48
	var cmd *exec.Cmd
30 49
	if fi.IsDir() {
31 50
		fis, err := f.Readdir(-1)
@ -49,6 +68,10 @@ func main() {
49 68
		if len(rs) == 0 {
50 69
			cmd = exec.Command("ls", dir)
51 70
		} else {
71
			if options.listFiles {
72
				fmt.Println()
73
			}
74
52 75
			fmt.Printf("'%s':\n", rs[0])
53 76
			cmd = exec.Command("head", "-n3", path.Join(dir, rs[0]))
54 77
		}
@ -56,6 +79,10 @@ func main() {
56 79
		cmd = exec.Command("head", "-n3", path.Join(dir, f.Name()))
57 80
	}
58 81
82
	if options.listFiles && cmd.Path == "ls" {
83
		os.Exit(0)
84
	}
85
59 86
	cmd.Stdin = os.Stdin
60 87
	cmd.Stdout = os.Stdout
61 88
	cmd.Stderr = os.Stderr