cmdInit creates ~/.flow/ (or $FLOW_ROOT), initializes flow.db, installs the skill. Idempotent — re-running is safe.
(args []string)
| 39 | // cmdInit creates ~/.flow/ (or $FLOW_ROOT), initializes flow.db, installs |
| 40 | // the skill. Idempotent — re-running is safe. |
| 41 | func cmdInit(args []string) int { |
| 42 | fs := flagSet("init") |
| 43 | if err := fs.Parse(args); err != nil { |
| 44 | return 2 |
| 45 | } |
| 46 | if fs.NArg() > 0 { |
| 47 | fmt.Fprintln(os.Stderr, "error: init takes no positional arguments") |
| 48 | return 2 |
| 49 | } |
| 50 | |
| 51 | root, err := flowRoot() |
| 52 | if err != nil { |
| 53 | fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 54 | return 1 |
| 55 | } |
| 56 | |
| 57 | // Create the top-level tree. `projects/` and `tasks/` are parents for |
| 58 | // per-project and per-task subdirectories created later by `flow add`. |
| 59 | // `kb/` holds 5 knowledge-base files (user/org/products/processes/ |
| 60 | // business) that the skill appends to on the fly and that `flow show |
| 61 | // task`/`show project` lists for execution sessions to read. |
| 62 | for _, d := range []string{ |
| 63 | root, |
| 64 | filepath.Join(root, "projects"), |
| 65 | filepath.Join(root, "tasks"), |
| 66 | filepath.Join(root, "kb"), |
| 67 | } { |
| 68 | if err := os.MkdirAll(d, 0o755); err != nil { |
| 69 | fmt.Fprintf(os.Stderr, "error: mkdir %s: %v\n", d, err) |
| 70 | return 1 |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Seed the KB files. Idempotent: only create if missing; never |
| 75 | // overwrite existing content. |
| 76 | for _, kb := range kbSeeds() { |
| 77 | path := filepath.Join(root, "kb", kb.filename) |
| 78 | if _, err := os.Stat(path); os.IsNotExist(err) { |
| 79 | if err := os.WriteFile(path, []byte(kb.stub), 0o644); err != nil { |
| 80 | fmt.Fprintf(os.Stderr, "error: write %s: %v\n", path, err) |
| 81 | return 1 |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Open (and implicitly initialize) flow.db. |
| 87 | dbPath := filepath.Join(root, "flow.db") |
| 88 | db, err := flowdb.OpenDB(dbPath) |
| 89 | if err != nil { |
| 90 | fmt.Fprintf(os.Stderr, "error: open db: %v\n", err) |
| 91 | return 1 |
| 92 | } |
| 93 | db.Close() |
| 94 | |
| 95 | // Install the skill idempotently. Skip if already present; we never |
| 96 | // overwrite on init (use `flow skill update` for that). |
| 97 | h := defaultHarness() |
| 98 | skillPath, err := h.SkillInstallPath() |