Browse Source

Modify ls to use our internal structure

Ian Adam Naval 5 years ago
parent
commit
af72e66f8b
3 changed files with 112 additions and 112 deletions
  1. 57
    57
      Makefile
  2. 14
    0
      files.go
  3. 41
    55
      ls/main.go

+ 57
- 57
Makefile View File

@@ -1,57 +1,57 @@
1
-# Go parameters
2
-GOCMD=go
3
-GOBUILD=$(GOCMD) build
4
-GOCLEAN=$(GOCMD) clean
5
-GOINSTALL=$(GOCMD) install
6
-GOTEST=$(GOCMD) test
7
-GODEP=$(GOTEST) -i
8
-GOFMT=gofmt -w
9
-GOCOVER=$(GOCMD) tool cover
10
-COVER_DIR=cover
11
-
12
-# Package lists
13
-TOPLEVEL_PKG := jsh
14
-CMD_LIST := jsh/ps jsh/free jsh/ls
15
-
16
-# List building
17
-ALL_LIST = $(TOPLEVEL_PKG) $(CMD_LIST)
18
-
19
-BUILD_LIST = $(foreach int, $(ALL_LIST), $(int)_build)
20
-CLEAN_LIST = $(foreach int, $(ALL_LIST), $(int)_clean)
21
-INSTALL_LIST = $(foreach int, $(ALL_LIST), $(int)_install)
22
-IREF_LIST = $(foreach int, $(ALL_LIST), $(int)_iref)
23
-TEST_LIST = $(foreach int, $(ALL_LIST), $(int)_test)
24
-COVER_LIST = $(foreach int, $(ALL_LIST), $(int)_cover)
25
-FMT_TEST = $(foreach int, $(ALL_LIST), $(int)_fmt)
26
-
27
-# All are .PHONY for now because dependencyness is hard
28
-.PHONY: $(CLEAN_LIST) $(TEST_LIST) $(FMT_LIST) $(INSTALL_LIST) $(BUILD_LIST) $(IREF_LIST)
29
-
30
-all: install
31
-build: $(BUILD_LIST)
32
-clean: $(CLEAN_LIST)
33
-	rm -rf $(COVER_DIR)
34
-install: $(INSTALL_LIST)
35
-test: $(TEST_LIST)
36
-cover: $(COVER_LIST)
37
-	echo "Output in directory: $(COVER_DIR)"
38
-iref: $(IREF_LIST)
39
-fmt: $(FMT_TEST)
40
-
41
-$(BUILD_LIST): %_build: %_fmt %_iref
42
-	$(GOBUILD) $*
43
-$(CLEAN_LIST): %_clean:
44
-	$(GOCLEAN) $*
45
-$(INSTALL_LIST): %_install: %_fmt %_iref
46
-	$(GOINSTALL) $*
47
-$(IREF_LIST): %_iref:
48
-	$(GODEP) $*
49
-$(TEST_LIST): %_test:
50
-	$(GOTEST) $*
51
-$(COVER_LIST): %_cover:
52
-	mkdir -p $(COVER_DIR)/$*
53
-	$(GOTEST) -coverprofile=$(COVER_DIR)/$*/c.out $*
54
-	$(GOCOVER) -html=$(COVER_DIR)/$*/c.out -o $(COVER_DIR)/$*/index.html
55
-	rm -f $(COVER_DIR)/$*/c.out
56
-$(FMT_TEST): %_fmt:
57
-	[ -d ./$* ] && $(GOFMT) ./$* || $(GOFMT) .
1
+# Go parameters
2
+GOCMD=go
3
+GOBUILD=$(GOCMD) build
4
+GOCLEAN=$(GOCMD) clean
5
+GOINSTALL=$(GOCMD) install
6
+GOTEST=$(GOCMD) test
7
+GODEP=$(GOTEST) -i
8
+GOFMT=gofmt -w
9
+GOCOVER=$(GOCMD) tool cover
10
+COVER_DIR=cover
11
+
12
+# Package lists
13
+TOPLEVEL_PKG := jsh
14
+CMD_LIST := jsh/ps jsh/free jsh/ls jsh/cat jsh/df
15
+
16
+# List building
17
+ALL_LIST = $(TOPLEVEL_PKG) $(CMD_LIST)
18
+
19
+BUILD_LIST = $(foreach int, $(ALL_LIST), $(int)_build)
20
+CLEAN_LIST = $(foreach int, $(ALL_LIST), $(int)_clean)
21
+INSTALL_LIST = $(foreach int, $(ALL_LIST), $(int)_install)
22
+IREF_LIST = $(foreach int, $(ALL_LIST), $(int)_iref)
23
+TEST_LIST = $(foreach int, $(ALL_LIST), $(int)_test)
24
+COVER_LIST = $(foreach int, $(ALL_LIST), $(int)_cover)
25
+FMT_TEST = $(foreach int, $(ALL_LIST), $(int)_fmt)
26
+
27
+# All are .PHONY for now because dependencyness is hard
28
+.PHONY: $(CLEAN_LIST) $(TEST_LIST) $(FMT_LIST) $(INSTALL_LIST) $(BUILD_LIST) $(IREF_LIST)
29
+
30
+all: install
31
+build: $(BUILD_LIST)
32
+clean: $(CLEAN_LIST)
33
+	rm -rf $(COVER_DIR)
34
+install: $(INSTALL_LIST)
35
+test: $(TEST_LIST)
36
+cover: $(COVER_LIST)
37
+	echo "Output in directory: $(COVER_DIR)"
38
+iref: $(IREF_LIST)
39
+fmt: $(FMT_TEST)
40
+
41
+$(BUILD_LIST): %_build: %_fmt %_iref
42
+	$(GOBUILD) $*
43
+$(CLEAN_LIST): %_clean:
44
+	$(GOCLEAN) $*
45
+$(INSTALL_LIST): %_install: %_fmt %_iref
46
+	$(GOINSTALL) $*
47
+$(IREF_LIST): %_iref:
48
+	$(GODEP) $*
49
+$(TEST_LIST): %_test:
50
+	$(GOTEST) $*
51
+$(COVER_LIST): %_cover:
52
+	mkdir -p $(COVER_DIR)/$*
53
+	$(GOTEST) -coverprofile=$(COVER_DIR)/$*/c.out $*
54
+	$(GOCOVER) -html=$(COVER_DIR)/$*/c.out -o $(COVER_DIR)/$*/index.html
55
+	rm -f $(COVER_DIR)/$*/c.out
56
+$(FMT_TEST): %_fmt:
57
+	[ -d ./$* ] && $(GOFMT) ./$* || $(GOFMT) .

+ 14
- 0
files.go View File

@@ -0,0 +1,14 @@
1
+package jsh
2
+
3
+type File struct {
4
+	Name  string
5
+	Size  int
6
+	Mode  int
7
+	Inode uint64
8
+}
9
+
10
+type Permission struct {
11
+	Read    bool
12
+	Write   bool
13
+	Execute bool
14
+}

+ 41
- 55
ls/main.go View File

@@ -4,72 +4,58 @@ import (
4 4
 	"flag"
5 5
 	"fmt"
6 6
 	"io/ioutil"
7
+	"jsh"
7 8
 	"log"
8
-	"strconv"
9 9
 	"syscall"
10 10
 )
11 11
 
12
-func get_fileinfo(f string, size bool, mode bool, inode bool) string {
13
-	var stat syscall.Stat_t
14
-	var ret string
15
-	ret = "{\"files\": [{\"name\":\"" + f + "\""
16
-	if err := syscall.Stat(f, &stat); err != nil {
17
-		log.Fatal(err)
18
-	}
19
-	if size {
20
-		ret = ret + ", \"size\":" + strconv.FormatInt(stat.Size, 10)
21
-	}
22
-	if mode {
23
-		ret = ret + ", \"mode\":" + strconv.Itoa(int(stat.Mode))
24
-	}
25
-	if inode {
26
-		ret = ret + ", \"inode\":" + strconv.FormatUint(stat.Ino, 10)
27
-	}
28
-	ret = ret + "}]}"
29
-	return ret
30
-}
12
+// func get_fileinfo(f string) string {
31 13
 
32
-func main() {
33
-	// here be the ls flags
34
-
35
-	var a_flag bool     // all files, even ones starting with .
36
-	var mode_flag bool  // flags
37
-	var inode_flag bool // inode
38
-	var size_flag bool  // size
39
-	var first = true
40
-
41
-	flag.BoolVar(&a_flag, "a", false, "lists all files in directory, even hidden ones")
42
-	flag.BoolVar(&mode_flag, "f", false, "include flags for file")
43
-	flag.BoolVar(&inode_flag, "i", false, "include flags for file")
44
-	flag.BoolVar(&size_flag, "s", false, "include flags for file")
45
-	// end ls flag
14
+// 	ret = ret + ", \"size\":" + strconv.FormatInt(stat.Size, 10)
15
+// 	ret = ret + ", \"mode\":" + strconv.Itoa(int(stat.Mode))
16
+// 	ret = ret + ", \"inode\":" + strconv.FormatUint(stat.Ino, 10)
17
+// 	ret = ret + "}]}"
18
+// 	return ret
19
+// }
46 20
 
21
+func listFiles(queue chan *jsh.JshFrame) {
47 22
 	flag.Parse()
48 23
 	root := "." //flag.Arg(0)
49 24
 	dir, _ := ioutil.ReadDir(root)
25
+	for _, entry := range dir {
26
+		var stat syscall.Stat_t
27
+		if err := syscall.Stat(entry.Name(), &stat); err != nil {
28
+			log.Fatal(err)
29
+		}
30
+		if entry.Name()[0] != '.' {
31
+			output := jsh.File{}
32
+			output.Name = entry.Name()
33
+			output.Size = int(stat.Size)
34
+			output.Mode = int(stat.Mode)
35
+			output.Inode = stat.Ino
36
+			frame := jsh.JshFrame{output, ""}
37
+			queue <- &frame
38
+		}
39
+	}
40
+}
50 41
 
51
-	fmt.Printf("[{\"StdOut\": [\n")
42
+func runJsonMode() {
43
+	queue := make(chan *jsh.JshFrame)
44
+	done := make(chan bool)
45
+	go jsh.OutputFrames(queue, done)
46
+	listFiles(queue)
47
+	close(queue)
48
+	<-done
49
+}
52 50
 
53
-	if !a_flag {
54
-		for _, entry := range dir {
55
-			if entry.Name()[0] != '.' {
56
-				if !first {
57
-					fmt.Printf(",")
58
-				} else {
59
-					first = false
60
-				}
61
-				fmt.Printf("%s\n", get_fileinfo(entry.Name(), size_flag, mode_flag, inode_flag))
62
-			}
63
-		}
51
+func main() {
52
+	// TODO: Support more flags
53
+	jsonModePtr := flag.Bool("json", false, "whether to use json mode for input and output")
54
+	flag.Parse()
55
+
56
+	if !*jsonModePtr {
57
+		fmt.Printf("%s", jsh.FallbackWithArgs("ls", flag.Args()))
64 58
 	} else {
65
-		for _, entry := range dir {
66
-			if !first {
67
-				fmt.Printf(",")
68
-			} else {
69
-				first = false
70
-			}
71
-			fmt.Printf("%s\n", get_fileinfo(entry.Name(), size_flag, mode_flag, inode_flag))
72
-		}
59
+		runJsonMode()
73 60
 	}
74
-	fmt.Printf("], \"StdErr\": []}]\n")
75 61
 }

Loading…
Cancel
Save