package main import ( "flag" "fmt" "io/ioutil" "jsh" "log" "syscall" ) // func get_fileinfo(f string) string { // ret = ret + ", \"size\":" + strconv.FormatInt(stat.Size, 10) // ret = ret + ", \"mode\":" + strconv.Itoa(int(stat.Mode)) // ret = ret + ", \"inode\":" + strconv.FormatUint(stat.Ino, 10) // ret = ret + "}]}" // return ret // } func listFiles(queue chan *jsh.JshFrame) { flag.Parse() root := "." //flag.Arg(0) dir, _ := ioutil.ReadDir(root) for _, entry := range dir { var stat syscall.Stat_t if err := syscall.Stat(entry.Name(), &stat); err != nil { log.Fatal(err) } if entry.Name()[0] != '.' { output := jsh.File{} output.Name = entry.Name() output.Size = int(stat.Size) output.Mode = int(stat.Mode) output.Inode = stat.Ino frame := jsh.JshFrame{output, ""} queue <- &frame } } } func runJsonMode() { queue := make(chan *jsh.JshFrame) done := make(chan bool) go jsh.OutputFrames(queue, done) listFiles(queue) close(queue) <-done } func main() { // TODO: Support more flags jsonModePtr := flag.Bool("json", false, "whether to use json mode for input and output") flag.Parse() if !*jsonModePtr { fmt.Printf("%s", jsh.FallbackWithArgs("ls", flag.Args())) } else { runJsonMode() } }