Example #1
0
func main() {
	cmd.UnixIO("err")
	c := cmd.AppCtx()
	opts.NewFlag("D", "debug", &c.Debug)
	opts.NewFlag("u", "unix IO", &ux)
	opts.NewFlag("g", "get contents", &gflag)
	if cmd.Args()[0] == "gf" {
		gflag = true
	}
	args := opts.Parse()
	if ux {
		cmd.UnixIO()
	}
	if len(args) == 0 {
		args = append(args, ".,1")
	}

	var dc <-chan face{}
	if !gflag {
		dc = cmd.Dirs(args...)
	} else {
		dc = cmd.Files(args...)
	}

	out := cmd.Out("out")
	var err error
	for m := range dc {
		cmd.Dprintf("got %T\n", m)
		switch m := m.(type) {
		case error:
			err = m
			cmd.Warn("%s", m)
			if !ux {
				m := fmt.Errorf("%s: %s", cmd.Args()[0], m)
				if ok := out <- m; !ok {
					close(dc, cerror(out))
				}
			}
		case zx.Dir:
			if !ux {
				if ok := out <- m; !ok {
					close(dc, cerror(out))
				}
			} else {
				printf("%s\n", m.Fmt())
			}
		case []byte:
			if ok := out <- m; !ok {
				close(dc, cerror(out))
			}
		}
	}
	if err := cerror(dc); err != nil {
		if !ux {
			out <- fmt.Errorf("%s: %s", cmd.Args()[0], err)
		}
		cmd.Exit(err)
	}
	cmd.Exit(err)
}
Example #2
0
func main() {
	cmd.UnixIO()
	nc := cmd.New(func() {
		cmd.Printf("hi from here\n")
		cmd.Fatal("oops!")
		println("XXX")
	})
	args := cmd.Args()
	cmd.Printf("args %v\n", args)
	wc := nc.Waitc()
	cmd.Printf("Hi there!\n")
	cmd.Printf("Hi there!\n")
	cmd.Printf("Hi there!\n")
	if len(args) > 1 {
		n, _ := strconv.Atoi(args[1])
		ns := time.Duration(n)
		time.Sleep(ns * time.Second)
	} else {
		time.Sleep(time.Second)
	}
	<-wc
	cmd.Printf("done\n")
}