main.go (3647B)
1 package main 2 3 import ( 4 "errors" 5 "flag" 6 "fmt" 7 "io" 8 "os" 9 "path/filepath" 10 "strings" 11 12 "manus/bib" 13 "manus/store" 14 ) 15 16 const usage = `usage: manus <command> [args] 17 18 Manuscripts 19 ls [-a] [-s stage] overview with task counts and health flags 20 init [-i id] [dir ...] register manuscripts (default: current dir) 21 discover list unregistered dirs under MANUS_ROOT 22 show [ms] details, tasks and previews 23 set [ms] key=value ... edit fields (stage, journal, deadline, ...) 24 revise [ms] [-mode auto|worktree|copy|none] [-c comments.txt] [-notag] 25 26 Tasks (ms may be omitted inside a manuscript dir; refs are ms#n or #n) 27 add [ms] "title" [-d due] [-s status] 28 tasks [ms] [-a] [-s status] 29 mv <ref> todo|doing|blocked|done 30 note <ref> [text] no text: open $EDITOR 31 rel [-d] <ref> blocks|relates|dup|parent <ref> 32 todos [ms] [-a] TODO/FIXME/\todo in tex; -a imports as tasks 33 34 References (shared BIBnew.bib) 35 ref add <doi|query|pdf> 36 ref check [ms] 37 ref import <file.bib> 38 ref export [ms] [-f bib|csl|ris] [-k key,...] [-o file] 39 40 Web 41 serve [-a 127.0.0.1:8090] 42 43 Environment: MANUS_ROOT (default ~/manus), MANUS_NOGIT=1 disables commits. 44 ` 45 46 type app struct { 47 root string 48 cwd string 49 out io.Writer 50 bib *bib.Client 51 } 52 53 func main() { 54 root := os.Getenv("MANUS_ROOT") 55 if root == "" { 56 home, err := os.UserHomeDir() 57 if err != nil { 58 fatal(err) 59 } 60 root = filepath.Join(home, "manus") 61 } 62 root, err := filepath.Abs(root) 63 if err != nil { 64 fatal(err) 65 } 66 cwd, err := os.Getwd() 67 if err != nil { 68 fatal(err) 69 } 70 a := &app{root: root, cwd: cwd, out: os.Stdout, bib: bib.NewClient()} 71 if err := a.run(os.Args[1:]); err != nil { 72 fatal(err) 73 } 74 } 75 76 func fatal(err error) { 77 fmt.Fprintln(os.Stderr, "manus:", err) 78 os.Exit(1) 79 } 80 81 var errUsage = errors.New("see 'manus help'") 82 83 func (a *app) run(args []string) error { 84 if len(args) == 0 { 85 args = []string{"ls"} 86 } 87 cmds := map[string]func([]string) error{ 88 "ls": a.ls, "init": a.init, "discover": a.discover, "show": a.show, "set": a.set, 89 "revise": a.revise, "add": a.add, "tasks": a.tasks, "mv": a.mv, "note": a.note, 90 "rel": a.rel, "todos": a.todos, "ref": a.ref, "serve": a.serve, 91 } 92 switch args[0] { 93 case "help", "-h", "--help": 94 fmt.Fprint(a.out, usage) 95 return nil 96 } 97 cmd, ok := cmds[args[0]] 98 if !ok { 99 return fmt.Errorf("unknown command %q, %w", args[0], errUsage) 100 } 101 return cmd(args[1:]) 102 } 103 104 // parse parses flags anywhere among the positional arguments. 105 func parse(fs *flag.FlagSet, args []string) ([]string, error) { 106 fs.SetOutput(io.Discard) 107 var pos []string 108 for { 109 if err := fs.Parse(args); err != nil { 110 return nil, fmt.Errorf("%s: %v", fs.Name(), err) 111 } 112 if fs.NArg() == 0 { 113 return pos, nil 114 } 115 pos = append(pos, fs.Arg(0)) 116 args = fs.Args()[1:] 117 } 118 } 119 120 // here returns the id of the manuscript containing the working directory. 121 func (a *app) here(s *store.Store) string { 122 if m := s.At(a.cwd); m != nil { 123 return m.ID 124 } 125 return "" 126 } 127 128 // pick resolves an optional manuscript argument against the cwd. 129 func (a *app) pick(s *store.Store, args []string) (*store.Manuscript, error) { 130 if len(args) > 0 { 131 return s.Get(args[0]) 132 } 133 if m := s.At(a.cwd); m != nil { 134 return m, nil 135 } 136 return nil, fmt.Errorf("no manuscript given and %s is not inside one", a.cwd) 137 } 138 139 func (a *app) printf(format string, v ...any) { fmt.Fprintf(a.out, format, v...) } 140 141 func (a *app) edit(fn func(s *store.Store) (string, error)) error { 142 return store.Edit(a.root, fn) 143 } 144 145 func oneLine(s string) string { return strings.Join(strings.Fields(s), " ") }