Пример #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)
}
Пример #2
0
// Run print lines in the current app context.
func main() {
	c := cmd.AppCtx()
	cmd.UnixIO("err")
	opts.NewFlag("D", "debug", &c.Debug)
	opts.NewFlag("1", "collect all files (not one msg per file)", &one)
	ux := false
	opts.NewFlag("u", "use unix out", &ux)
	args := opts.Parse()
	if ux {
		cmd.UnixIO("out")
	}
	if len(args) != 0 {
		cmd.SetIn("in", cmd.Files(args...))
	}
	buf := &bytes.Buffer{}
	in := cmd.In("in")
	out := cmd.Out("out")
	for m := range in {
		switch m := m.(type) {
		case []byte:
			buf.Write(m)
		case zx.Dir:
			if !one && buf.Len() > 0 {
				if ok := out <- buf.Bytes(); !ok {
					close(in, cerror(out))
					break
				}
				buf = &bytes.Buffer{}
			}
			if !one && !ux {
				if ok := out <- m; !ok {
					close(in, cerror(out))
					break
				}
			}
		case error:
			cmd.Warn("%s", m)
		default:
			cmd.Dprintf("ignored %T\n", m)
			if !ux {
				if ok := out <- m; !ok {
					close(in, cerror(out))
					break
				}
			}
		}
	}
	if buf.Len() > 0 {
		out <- buf.Bytes()
	}
	if err := cerror(in); err != nil {
		cmd.Fatal(err)
	}
}
Пример #3
0
func main() {
	cmd.UnixIO("err")
	cmd.UnixIO("in")
	args := opts.Parse()
	if len(args) != 0 {
		in := cmd.Files(args...)
		cmd.SetIn("in", in)
	}
	out := cmd.Out("out")
	in := cmd.In("in")
	for m := range in {
		if ok := out <- m; !ok {
			close(in, cerror(out))
		}
	}
	if err := cerror(in); err != nil {
		cmd.Fatal(err)
	}
}
Пример #4
0
// Run cols in the current app context.
func main() {
	c := cmd.AppCtx()
	opts.NewFlag("D", "debug", &c.Debug)
	opts.NewFlag("w", "wid: set max line width", &wid)
	opts.NewFlag("n", "ncols: set number of columns", &ncols)
	opts.NewFlag("u", "use unix output", &ux)
	cmd.UnixIO("err")
	args := opts.Parse()
	if ux {
		cmd.UnixIO("out")
	}
	if len(args) != 0 {
		cmd.SetIn("in", cmd.Files(args...))
	}
	in := cmd.In("in")
	for m := range in {
		switch m := m.(type) {
		default:
			// ignored & forwarded
			cmd.Dprintf("got %T\n", m)
			continue
		case zx.Dir:
			cmd.Dprintf("got %T %s\n", m, m["Upath"])
			add(strings.TrimSpace(m["name"]))
		case error:
			if m != nil {
				cmd.Warn("%s", m)
			}
		case []byte:
			cmd.Dprintf("got %T [%d]\n", m, len(m))
			words := strings.Fields(strings.TrimSpace(string(m)))
			add(words...)
		}
	}
	col()
	if err := cerror(in); err != nil {
		cmd.Fatal("in %s", err)
	}
}
Пример #5
0
// Run print lines in the current app context.
func main() {
	c := cmd.AppCtx()
	cmd.UnixIO("err")
	opts.NewFlag("D", "debug", &c.Debug)
	opts.NewFlag("r", "range: print this range", &ranges)
	opts.NewFlag("n", "print line numbers", &nflag)
	opts.NewFlag("p", "print file names and line numbers", &pflag)
	ux := false
	opts.NewFlag("u", "use unix out", &ux)
	args := opts.Parse()
	if ux {
		cmd.UnixIO("out")
	}
	if len(args) != 0 {
		cmd.SetIn("in", cmd.Files(args...))
	}
	if len(ranges) == 0 {
		ranges = append(ranges, ",")
	}
	if err := parseRanges(); err != nil {
		cmd.Fatal(err)
	}
	runFiles(lns)
}