Exemplo n.º 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)
}
Exemplo n.º 2
0
func main() {
	cmd.UnixIO("err")
	c := cmd.AppCtx()
	opts.NewFlag("D", "debug", &c.Debug)
	opts.NewFlag("v", "verbose", &c.Verb)
	opts.NewFlag("x", "exclude", &excl)
	opts.NewFlag("n", "print just replica names when used to list replicas", &nflag)
	opts.NewFlag("m", "move existing replica client/server paths to the given ones", &mflag)
	opts.NewFlag("u", "don't use unix out", &notux)
	args := opts.Parse()
	if !notux {
		cmd.UnixIO("out")
	}
	if mflag {
		if len(args) != 3 {
			cmd.Warn("-m needs all arguments")
			opts.Usage()
		}
		err := mv(args[0], args[1], args[2])
		if err != nil {
			cmd.Fatal(err)
		}
		cmd.Exit(nil)
	}
	var err error
	switch len(args) {
	case 0:
		for _, nm := range names() {
			if nflag {
				cmd.Printf("%s\n", nm)
				continue
			}
			if err2 := list(nm); err2 != nil {
				cmd.Warn("%s: %s", nm, err2)
				if err == nil {
					err = err2
				}
			}
		}
	case 1:
		err = list(args[0])
	case 3:
		err = mk(args[0], args[1], args[2])
	default:
		opts.Usage()
	}
	if err != nil {
		cmd.Fatal(err)
	}
	cmd.Exit(nil)
}
Exemplo n.º 3
0
func main() {
	cmd.UnixIO("err")
	c := cmd.AppCtx()
	opts.NewFlag("D", "debug", &c.Debug)
	opts.NewFlag("v", "verbose", &c.Verb)
	opts.NewFlag("a", "all", &aflag)
	opts.NewFlag("b", "blind", &bflag)
	opts.NewFlag("n", "dry run", &nflag)
	opts.NewFlag("u", "don't use unix out", &notux)
	args := opts.Parse()
	if !notux {
		cmd.UnixIO("out")
	}
	if aflag && bflag {
		opts.Usage()
	}
	var err error
	switch len(args) {
	case 0:
		for _, nm := range names() {
			cmd.Printf("pull %s...\n", nm)
			if err2 := pull1(nm); err == nil {
				err = err2
			}
		}
	case 1:
		err = pull1(args[0])
	default:
		opts.Usage()
	}
	cmd.Exit(err)
}
Exemplo n.º 4
0
func main() {
	c := cmd.AppCtx()
	opts.NewFlag("D", "debug", &c.Debug)
	cmd.UnixIO("err")
	args := opts.Parse()
	if len(args) == 1 && ismode(args[0]) {
		args = append([]string{"mode"}, args...)
	}
	if len(args) == 0 || len(args)%2 != 0 {
		cmd.Warn("wrong number of arguments")
		opts.Usage()
	}
	nd := chd(args...)

	in := cmd.In("in")
	var err error
	for m := range in {
		switch d := m.(type) {
		case zx.Dir:
			cmd.Dprintf("got %T %s\n", d, d["Upath"])
			if cerr := ch(d, nd); cerr != nil {
				cmd.Warn("%s", cerr)
				err = cerr
			}
		default:
			// ignored
			cmd.Dprintf("got %T\n", m)
		}
	}
	if err != nil {
		cmd.Exit(err)
	}
}
Exemplo n.º 5
0
func runFiles(fn func(nm string, c chan []byte, dc chan bool)) {
	var lnc chan []byte
	var dc chan bool
	in := cmd.Lines(cmd.In("in"))
	out := cmd.Out("out")
	nm := "in"
	for m := range in {
		cmd.Dprintf("got %T\n", m)
		switch m := m.(type) {
		case zx.Dir:
			if pflag {
				p := m["Upath"]
				if p == "" {
					p = m["path"]
				}
				nm = p
			}
			if dc != nil {
				close(lnc)
				<-dc
			}
			dc = nil
			lnc = nil
			if ok := out <- m; !ok {
				close(in, cerror(out))
			}
		case []byte:
			if dc == nil {
				lnc = make(chan []byte)
				dc = make(chan bool, 1)
				go fn(nm, lnc, dc)
			}
			if ok := lnc <- m; !ok {
				close(in, cerror(lnc))
				close(out, cerror(lnc))
			}
		default:
			if dc != nil {
				close(lnc)
				<-dc
			}
			dc = nil
			lnc = nil
			if ok := out <- m; !ok {
				close(lnc, cerror(out))
				close(in, cerror(out))
			}
		}
	}
	if dc != nil {
		close(lnc, cerror(in))
		<-dc
	}
	cmd.Exit(cerror(in))
}
Exemplo n.º 6
0
// Run echo in the current app context.
func main() {
	cmd.UnixIO("err")
	c := cmd.AppCtx()
	opts.NewFlag("D", "debug", &c.Debug)
	opts.NewFlag("n", "don't add a final newline", &nflag)
	opts.NewFlag("m", "issue one message per arg", &mflag)
	opts.NewFlag("u", "use unix out", &ux)
	opts.NewFlag("o", "chan: output to this chan (for testing other tools)", &oname)
	opts.NewFlag("i", "chan: echo input from this chan (for testing other tools)", &iname)
	args := opts.Parse()
	if ux {
		cmd.UnixIO(oname)
	}
	var b bytes.Buffer
	out := cmd.Out(oname)
	if out == nil {
		cmd.Fatal("no output chan '%s'", oname)
	}
	mflag = mflag || iname != ""
	for i, arg := range args {
		if mflag {
			ok := out <- []byte(arg)
			if !ok {
				cmd.Fatal("out: %s", cerror(out))
			}
		} else {
			b.WriteString(arg)
			if i < len(args)-1 {
				b.WriteString(" ")
			}
		}
	}
	if iname != "" {
		for x := range cmd.In(iname) {
			x := x
			if b, ok := x.([]byte); ok {
				ok := out <- []byte(b)
				if !ok {
					cmd.Fatal("out: %s", cerror(out))
				}
			}
		}
	}
	if mflag {
		cmd.Exit(nil)
	}
	if !nflag {
		b.WriteString("\n")
	}
	ok := out <- b.Bytes()
	if !ok {
		cmd.Fatal("out: %s", cerror(out))
	}
}
Exemplo n.º 7
0
// Run path in the current app context.
func main() {
	cmd.UnixIO("err")
	c := cmd.AppCtx()
	opts.NewFlag("D", "debug", &c.Debug)
	opts.NewFlag("r", "dir: print paths relative to dir", &dir)
	opts.NewFlag("d", "print parent directories", &dflag)
	opts.NewFlag("b", "print base names", &bflag)
	args := opts.Parse()
	if ux {
		cmd.UnixIO()
	}
	if dir != "" && (dflag || bflag) {
		cmd.Warn("incompatible flags")
		opts.Usage()
	}
	if dflag && bflag {
		cmd.Warn("incompatible flags")
		opts.Usage()
	}
	if dir != "" {
		dir = cmd.AbsPath(dir)
	}
	var sts error
	for _, n := range args {
		n = cmd.AbsPath(n)
		switch {
		case dir != "":
			r, err := filepath.Rel(dir, n)
			if err != nil {
				sts = err
				cmd.Warn("%s: %s", n, err)
			} else {
				n = r
			}
		case dflag:
			n = fpath.Dir(n)
		case bflag:
			n = fpath.Base(n)
		}
		cmd.Printf("%s\n", n)
	}
	cmd.Exit(sts)
}
Exemplo n.º 8
0
func kq(dir, wcmd string) {
	w, err := fswatch.New()
	if err != nil {
		cmd.Fatal("kqueue: %s", err)
	}

	cmd.Dprintf("(re)read %s\n", dir)
	if err := w.Add(dir); err != nil {
		cmd.Fatal("kqueue: %s", err)
	}
	if err != nil {
		cmd.Fatal("watch: %s", err)
	}
	var pc chan string
	if once {
		w.Once()
	}
	pc = w.Changes()
	for p := range pc {
		if wcmd == "" {
			cmd.Warn("%s", p)
			continue
		}
		cln := strings.Replace(wcmd, "%", p, -1)
		cmd.Dprintf("run %s\n", cln)
		out, err := exec.Command("sh", "-c", cln).CombinedOutput()
		cmd.Out("out") <- out
		if err != nil {
			cmd.Warn("run: %s", err)
		}
	}
	if err := cerror(pc); err != nil {
		cmd.Fatal(err)
	}
	cmd.Exit(nil)
}
Exemplo n.º 9
0
// Run rem in the current app context.
func main() {
	c := cmd.AppCtx()
	cmd.UnixIO("err")
	opts.NewFlag("D", "debug", &c.Debug)
	opts.NewFlag("v", "verbose; print the calls made in the order they are made.", &c.Verb)
	opts.NewFlag("a", "remove all", &aflag)
	opts.NewFlag("f", "quiet, called 'force' in unix", &fflag)
	opts.NewFlag("n", "dry run; report removes but do not do them", &dry)
	args := opts.Parse()
	if len(args) != 0 {
		cmd.SetIn("in", cmd.Dirs(args...))
	}
	c.Verb = c.Verb || dry
	in := cmd.In("in")
	var err error
	for m := range in {
		switch d := m.(type) {
		case zx.Dir:
			cmd.Dprintf("got %T %s\n", d, d["upath"])
			dirs = append(dirs, d)
		case error:
			cmd.Warn("%s", d)
		default:
			// ignored
			cmd.Dprintf("ignored %T\n", m)
		}
	}
	if aflag {
		for i := 0; i < len(dirs); i++ {
			if dirs[i] == nil {
				continue
			}
			pi := dirs[i]["path"]
			for j := 1; j < len(dirs); j++ {
				if i == j || dirs[j] == nil {
					continue
				}
				pj := dirs[j]["path"]
				if zx.HasPrefix(pj, pi) {
					dirs[j] = nil
				}
			}
		}
	}
	for i := len(dirs) - 1; i >= 0; i-- {
		if dirs[i] == nil {
			continue
		}
		if cerr := rmf(dirs[i]); cerr != nil {
			if !fflag {
				cmd.Warn("%s", cerr)
				err = cerr
			}
		}
	}
	if err == nil {
		err = cerror(in)
		if err != nil {
			cmd.Fatal(err)
		}
	}
	cmd.Exit(err)
}