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", ¬ux) 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) }
func main() { opts := opt.New("[file]") c := cmd.AppCtx() opts.NewFlag("D", "debug", &c.Debug) rdonly := false opts.NewFlag("r", "read only", &rdonly) opts.NewFlag("e", "do edits for testing", &doedits) cmd.UnixIO("err") args := opts.Parse() var t *ink.Txt inkout := cmd.Out("ink") if inkout == nil { cmd.UnixIO() } if len(args) == 0 { t = ink.NewTxt("1234", "abc") } else { dat, err := zx.GetAll(cmd.NS(), cmd.AbsPath(args[0])) if err != nil { cmd.Fatal(err) } t = ink.NewTxt(args[0]+" Del", string(dat)) } go edit(t) if rdonly { t.NoEdits() } ink.UsePort("8182") bs := ink.NewButtonSet(&ink.Button{Tag: "One", Name: "one"}, &ink.Button{Tag: "Two", Name: "two"}, &ink.Button{Tag: "B", Name: "b", Value: &bold}, &ink.Button{Tag: "I", Name: "i", Value: &italic}) rs := ink.NewRadioSet(&style, &ink.Button{Tag: "R", Name: "r"}, &ink.Button{Tag: "T", Name: "t"}) go buttons(bs, rs, t) pg := ink.NewPg("/", "Example text editing:", bs, rs, t) pg.Tag = "Clive's iedit" if doedits { go edits(t) } go ink.Serve() if inkout != nil { // TODO: ctlrs must use unique ids sytem-wide, or // controls written won't work because of their ids. inkout <- []byte(`<tt>Hi there, this is HTML</tt>`) var buf bytes.Buffer bs.WriteTo(&buf) inkout <- buf.Bytes() inkout <- []byte("https://localhost:8182") } t.Wait() for rs := range t.Get(0, -1) { cmd.Printf("%s", string(rs)) } }
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", ¬ux) 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) }
// 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) }
func list(name string) error { if !strings.ContainsRune(name, '/') { name = "/u/lib/repl/" + name } tr, err := repl.Load(name) if err != nil { return err } defer tr.Close() c := cmd.AppCtx() switch { case c.Verb: tr.Ldb.DumpTo(os.Stderr) tr.Rdb.DumpTo(os.Stderr) default: xs := strings.Join(tr.Ldb.Excl, " ") cmd.Printf("%s %s %s %s\n", tr.Ldb, tr.Ldb.Addr, tr.Rdb.Addr, xs) } return nil }
func bytes(m []byte) { if len(m) == 0 { cmd.Printf("[]byte{},\n") return } cmd.Printf("[]byte{\n\t\t") defer cmd.Printf("\t\t},\n") for i, b := range m { cmd.Printf("%d,", b) if i%16 == 0 && i > 0 && i < len(m)-1 { cmd.Printf("\n\t\t") } } if len(m)%16 != 0 { cmd.Printf("\n") } }
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") }
func lns(nm string, in chan []byte, donec chan bool) { last := []string{} nln := 0 cmd.Dprintf("nhd %d ntl %d nfrom %d\n", nhd, ntl, nfrom) var err error for m := range in { s := string(m) lout := false nln++ if all { if pflag { _, err = cmd.Printf("%s:%-5d %s", nm, nln, s) } else if nflag { _, err = cmd.Printf("%-5d %s", nln, s) } else { _, err = cmd.Printf("%s", s) } if err != nil { close(in, err) } continue } if ntl == 0 && nfrom == 0 && nhd > 0 && nln > nhd { close(in) close(donec) return } for _, a := range addrs { cmd.Dprintf("tl match %d of ? in %s\n", nln, a) if a.Matches(nln, 0) { lout = true if pflag { _, err = cmd.Printf("%s:%-5d %s", nm, nln, s) } else if nflag { _, err = cmd.Printf("%-5d %s", nln, s) } else { _, err = cmd.Printf("%s", s) } if err != nil { close(in, err) } break } } if nln >= nfrom || ntl > 0 { if lout { s = "" /*already there */ } if nln >= nfrom || ntl > 0 && len(last) < ntl { last = append(last, s) } else { copy(last, last[1:]) last[len(last)-1] = s } } } if !all && (ntl > 0 || nfrom > 0) { // if len(last) == 3 and nln is 10 // last[0] is -3 or 10-2 // last[1] is -2 or 10-1 // last[2] is -1 or 10 for i := 0; i < len(last); i++ { for _, a := range addrs { if a.P0 > 0 && a.P1 > 0 { // done already continue } cmd.Dprintf("tl match %d of %d in %s\n", nln-len(last)+1+i, nln, a) if a.Matches(nln-len(last)+1+i, nln) && last[i] != "" { if pflag { _, err = cmd.Printf("%s:%-5d %s", nm, nln-len(last)+1+i, last[i]) } else if nflag { _, err = cmd.Printf("%-5d %s", nln-len(last)+1+i, last[i]) } else { _, err = cmd.Printf("%s", last[i]) } if err != nil { close(donec, err) return } last[i] = "" /* because if empty it still contains \n */ break } } } } close(donec) }
func pull1(name string) error { if !strings.ContainsRune(name, '/') { name = "/u/lib/repl/" + name } tr, err := repl.Load(name) if err != nil { cmd.Warn("load %s: %s", name, err) return err } defer tr.Close() c := cmd.AppCtx() if c.Debug { tr.Ldb.DumpTo(os.Stderr) tr.Rdb.DumpTo(os.Stderr) } if nflag { var cc <-chan repl.Chg switch { case aflag: cc, err = tr.AllPullChanges() default: cc, err = tr.PullChanges() } if err != nil { cmd.Warn("pull changes %s: %s", name, err) return err } for c := range cc { cmd.Printf("%s\n", c) } return nil } var cc chan repl.Chg dc := make(chan bool) if c.Verb { cc = make(chan repl.Chg) go func() { for c := range cc { cmd.Printf("%s\n", c) } close(dc) }() } else { close(dc) } switch { case aflag: err = tr.PullAll(cc) case bflag: err = tr.BlindPull(cc) default: err = tr.Pull(cc) } <-dc if err != nil { cmd.Warn("pull %s: %s", name, err) } if err2 := tr.Save(name); err2 != nil { cmd.Warn("save %s: %s", name, err2) if err == nil { err = err2 } } return err }
func find(dump, dpref, rel string, dc chan<- zx.Dir, ufile zx.Dir) { droot := fpath.Join(dump, dpref) years, err := cmd.GetDir(droot) if err != nil { cmd.Warn("%s", err) return } for i := len(years) - 1; i >= 0; i-- { year := years[i]["name"] if ignored(year, "") { continue } ypath := years[i]["path"] days, err := cmd.GetDir(ypath) if err != nil { cmd.Warn("%s: %s", ypath, err) continue } lastsz, lastmt, lastm := "", "", "" for j := len(days) - 1; j >= 0; j-- { day := days[j]["name"] if ignored(year, day) { continue } fpath := fpath.Join(days[j]["path"], rel) d, err := cmd.Stat(fpath) if err != nil { if !force { cmd.Dprintf("find: %s", err) return } continue } newm, newsz, newmt := d["mode"], d["size"], d["mtime"] if newsz == lastsz && newmt == lastmt && newm == lastm { continue } lastm, lastsz, lastmt = newm, newsz, newmt d["upath"] = ufile["path"] d["uupath"] = ufile["upath"] if ok := dc <- d; !ok { return } if !all { return } } } } func report(dc chan zx.Dir, donec chan bool) { last := "" for d := range dc { if last == "" { last = d["Upath"] if last == "" { last = d["path"] } } p := d["path"] cmd.Dprintf("found '%s'\n", p) var err error switch { case xcmd != "": _, err = cmd.Printf("%s %s %s\n", xcmd, p, last) case dflag: dcmd := fmt.Sprintf(`9 diff -n %s %s`, p, last) _, err = cmd.Printf("%s\n", dcmd) if err != nil { cmd.Warn("diff: %s", err) continue } case lflag: _, err = cmd.Printf("%s\n", d.Fmt()) case cflag: _, err = cmd.Printf("cp %s %s\n", p, d["Upath"]) default: _, err = cmd.Printf("%s\n", d["path"]) } if err != nil { close(dc, err) } last = p } close(donec, cerror(dc)) } func hist(in <-chan face{}) error { dc := make(chan zx.Dir) ec := make(chan bool) go report(dc, ec) var sts error for m := range in { switch m := m.(type) { case zx.Dir: cmd.Dprintf("got %T %s\n", m, m["path"]) file := m["path"] if m["upath"] == "" { m["upath"] = m["path"] } ddir := dump dpref := "" rel := "" switch { case zx.HasPrefix(file, "/zx"): if ddir == "" { ddir = "/dump" } dpref = "/zx" rel = zx.Suffix(file, "/zx") case zx.HasPrefix(file, "/u/gosrc/src/clive"): if ddir == "" { ddir = "/u/dump" } dpref = "clive" rel = zx.Suffix(file, "/u/gosrc/src/clive") case zx.HasPrefix(file, "/u"): if ddir == "" { ddir = "/u/dump" } els := zx.Elems(file) if len(els) < 3 { cmd.Warn("%s: too few path elements", m["upath"]) sts = errNoDump continue } dpref = els[1] rel = zx.Path(els[2:]...) default: cmd.Warn("%s: %s", m["upath"], errNoDump) sts = errNoDump continue } find(ddir, dpref, rel, dc, m.Dup()) default: cmd.Dprintf("got %T\n", m) } } close(dc, cerror(in)) <-ec if sts == nil { sts = cerror(ec) } if sts == nil { sts = cerror(in) } return sts } // Run cnt in the current app context. func main() { c := cmd.AppCtx() cmd.UnixIO("err") opts.NewFlag("D", "debug", &c.Debug) opts.NewFlag("f", "force search past file removals", &force) opts.NewFlag("l", "produce a long listing (or print just the name)", &lflag) opts.NewFlag("c", "copy the file from the dump", &cflag) opts.NewFlag("d", "print file differences", &dflag) opts.NewFlag("x", "cmd: print lines to execute this command between versions", &xcmd) opts.NewFlag("a", "list all copies that differ, not just the last one.", &all) opts.NewFlag("p", "dumpdir: path to dump (default is /dump or /u/dump)", &dump) t := time.Now() when := t opts.NewFlag("w", "date: backward search start time (default is now)", &when) ux := false opts.NewFlag("u", "unix IO", &ux) args := opts.Parse() if (all && cflag) || (force && !all) { cmd.Warn("incompatible flags") opts.Usage() } if ux { cmd.UnixIO("out") } lastyear = "" lastday = "" if !t.Equal(when) { y := when.Year() m := when.Month() d := when.Day() if y == 0 { y = t.Year() } lastyear = fmt.Sprintf("%04d", y) lastday = fmt.Sprintf("%02d%02d", m, d) } if len(args) != 0 { cmd.SetIn("in", cmd.Dirs(args...)) } in := cmd.In("in") if err := hist(in); err != nil { cmd.Fatal(err) } }