Skip to content

Commit c385f0f

Browse files
committed
Improve .env loading and first-time setup docs
- Search for .env in priority order (./.env, ~/.config/brain/.env, ~/.brain/.env), resolve symlinks and only load regular readable files; fall back to default dotenv behavior if none found. - Clarify DIARY_PATH error with recommended locations and link to SETUP_CHECKLIST.md. - Add comprehensive SETUP_CHECKLIST.md and update README Quick Start/Installation to show first-time setup steps (create ~/.config/brain/.env, fetch .env.example, usage notes and .env search priority).
1 parent 934e5db commit c385f0f

File tree

3 files changed

+338
-18
lines changed

3 files changed

+338
-18
lines changed

README.md

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,16 @@ AI-powered personal knowledge system with daily planning, reflective journaling,
1212
Install directly from GitHub:
1313

1414
```bash
15-
# Using uv (recommended - fastest)
1615
uv tool install git+https://github.com/madebygps/second-brain.git
17-
18-
# Or using pipx (isolated environment)
19-
pipx install git+https://github.com/madebygps/second-brain.git
20-
21-
# Or using pip (system-wide)
22-
pip install git+https://github.com/madebygps/second-brain.git
2316
```
2417

2518
After installation, the `brain` command will be available globally:
2619
```bash
2720
brain --help
28-
brain plan create today
29-
brain diary create today
3021
```
3122

23+
**Next:** Configure your `.env` file (see [Configuration](#configuration) below).
24+
3225
### For Development
3326

3427
```bash
@@ -74,13 +67,23 @@ uv run brain --help
7467

7568
## Quick Start
7669

77-
After installing (see [Installation](#installation)):
70+
### First-Time Setup
71+
72+
1. **Install** (see [Installation](#installation) above)
73+
74+
2. **Configure** - Create `~/.config/brain/.env` with your settings:
7875

7976
```bash
80-
# Configure (first time only)
81-
cp .env.example .env
82-
# Edit .env with your paths and Azure credentials
77+
mkdir -p ~/.config/brain
78+
curl -o ~/.config/brain/.env https://raw.githubusercontent.com/madebygps/second-brain/main/.env.example
79+
nano ~/.config/brain/.env # Edit with your paths and Azure credentials
80+
```
81+
82+
See [SETUP_CHECKLIST.md](SETUP_CHECKLIST.md) for detailed setup instructions.
83+
84+
3. **Use**:
8385

86+
```bash
8487
# Morning: Create your planning entry (saves to PLANNER_PATH)
8588
brain plan create today
8689

@@ -91,7 +94,9 @@ brain diary create today
9194
brain notes search "discipline"
9295
```
9396

94-
> **Note:** If developing locally, prefix commands with `uv run` (e.g., `uv run brain plan create today`)
97+
> **Note:** The `.env` file is automatically searched in `~/.config/brain/.env`, `~/.brain/.env`, or current directory.
98+
>
99+
> **For developers:** When running locally, prefix commands with `uv run` (e.g., `uv run brain plan create today`)
95100
96101
## Usage
97102

@@ -128,12 +133,19 @@ brain --verbose <command> # Show key operations
128133
brain --debug <command> # Full diagnostics with LLM details
129134
```
130135

131-
> **Clean UX:** By default, logs are suppressed during operations to avoid interrupting spinners. Use `--verbose` or `--debug` for detailed information with beautiful Rich formatting.
136+
> **Logs:** By default, logs are suppressed during operations to avoid interrupting spinners. Use `--verbose` or `--debug` for detailed information with beautiful Rich formatting.
132137
133138
> **Cost Tracking:** All Azure OpenAI usage is automatically tracked in a local SQLite database (`~/.brain/costs.db`). Data stays private and grows ~10-50 MB/year for typical use.
134139
135140
## Configuration
136141

142+
The `brain` CLI automatically searches for `.env` in these locations (in priority order):
143+
1. Current directory (`./.env`)
144+
2. User config directory (`~/.config/brain/.env`) ⭐ **Recommended**
145+
3. Home directory (`~/.brain/.env`)
146+
147+
Create `~/.config/brain/.env` with these settings:
148+
137149
**Required Paths:**
138150
- `DIARY_PATH` - Path to Obsidian vault or markdown directory for reflection entries
139151
- `PLANNER_PATH` - Path to directory for daily plan files (separate from diary)

SETUP_CHECKLIST.md

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
# Setup Checklist for second-brain
2+
3+
Complete guide for first-time setup after installation.
4+
5+
## Installation
6+
7+
8+
```bash
9+
uv tool install git+https://github.com/madebygps/second-brain.git
10+
```
11+
12+
Verify installation:
13+
```bash
14+
brain --version
15+
# Output: Brain CLI v0.1.0
16+
```
17+
18+
## Configuration
19+
20+
### Step 1: Create Directories
21+
22+
Create directories for your diary and planner:
23+
24+
```bash
25+
# Create diary directory (for reflection entries)
26+
mkdir -p ~/Documents/second-brain/diary
27+
28+
# Create planner directory (for daily plans)
29+
mkdir -p ~/Documents/second-brain/planner
30+
```
31+
32+
> **Tip:** Best if used with Obsidian. Point `DIARY_PATH` to your vault directory.
33+
34+
### Step 2: Create .env File
35+
36+
The `brain` CLI automatically searches for `.env` in these locations (in order):
37+
38+
1. **Current directory** (`./.env`)
39+
2. **User config directory** (`~/.config/brain/.env`) ⭐ **Recommended**
40+
3. **Home directory** (`~/.brain/.env`)
41+
42+
**Recommended Setup (works from anywhere):**
43+
44+
```bash
45+
# Create config directory
46+
mkdir -p ~/.config/brain
47+
48+
# Download the example .env template
49+
curl -o ~/.config/brain/.env https://raw.githubusercontent.com/madebygps/second-brain/main/.env.example
50+
51+
# Set secure permissions (readable by you only)
52+
chmod 600 ~/.config/brain/.env
53+
54+
# Edit with your settings
55+
nano ~/.config/brain/.env # or use your preferred editor
56+
```
57+
58+
That's it! No need for shell aliases or cd-ing around. The `brain` command will automatically find your config.
59+
60+
> **Security Note:** Your `.env` file contains API keys. The `chmod 600` command ensures only you can read it.
61+
62+
63+
The current directory's `.env` takes priority over the global config.
64+
65+
### Step 3: Configure .env
66+
67+
Edit your `.env` file with the following **required** variables:
68+
69+
```bash
70+
# Required: Paths (created in Step 1)
71+
DIARY_PATH=/Users/yourname/Documents/second-brain/diary
72+
PLANNER_PATH=/Users/yourname/Documents/second-brain/planner
73+
74+
# Required: Azure OpenAI (get from Azure Portal)
75+
AZURE_OPENAI_API_KEY=your-api-key-here
76+
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/
77+
AZURE_OPENAI_DEPLOYMENT=gpt-4o
78+
AZURE_OPENAI_API_VERSION=2024-02-15-preview
79+
80+
# Required: Azure AI Search (for book notes search)
81+
AZURE_SEARCH_ENDPOINT=https://your-search-service.search.windows.net
82+
AZURE_SEARCH_API_KEY=your-search-api-key-here
83+
AZURE_SEARCH_INDEX_NAME=second-brain-notes
84+
```
85+
86+
**Optional variables** (with defaults):
87+
88+
```bash
89+
# Cost tracking database location
90+
BRAIN_COST_DB_PATH=~/.brain/costs.db
91+
92+
# Logging
93+
BRAIN_LOG_LEVEL=INFO
94+
BRAIN_LOG_FILE=~/.brain/logs/brain.log
95+
```
96+
97+
### Step 4: Get Azure Credentials
98+
99+
#### Azure OpenAI (Required)
100+
101+
1. Go to [Azure Portal](https://portal.azure.com)
102+
2. Navigate to your Azure OpenAI resource
103+
3. Go to **Keys and Endpoint**
104+
4. Copy:
105+
- **KEY 1**`AZURE_OPENAI_API_KEY`
106+
- **Endpoint**`AZURE_OPENAI_ENDPOINT`
107+
5. Go to **Model deployments**
108+
6. Copy your deployment name (e.g., "gpt-4o") → `AZURE_OPENAI_DEPLOYMENT`
109+
110+
#### Azure AI Search (Required for notes search)
111+
112+
1. Go to [Azure Portal](https://portal.azure.com)
113+
2. Navigate to your Azure AI Search resource
114+
3. Go to **Keys**
115+
4. Copy:
116+
- **Primary admin key**`AZURE_SEARCH_API_KEY`
117+
5. Copy the URL from Overview page → `AZURE_SEARCH_ENDPOINT`
118+
6. Go to **Indexes** and copy your index name → `AZURE_SEARCH_INDEX_NAME`
119+
120+
### Step 5: Test Configuration
121+
122+
Test that everything is configured correctly:
123+
124+
```bash
125+
# Test diary commands (uses DIARY_PATH)
126+
brain diary list
127+
128+
# Test plan commands (uses PLANNER_PATH)
129+
brain plan create today
130+
131+
# Test notes search (uses Azure AI Search)
132+
brain notes status
133+
134+
# Test cost tracking (uses BRAIN_COST_DB_PATH)
135+
brain cost summary
136+
```
137+
138+
## Troubleshooting
139+
140+
### "DIARY_PATH must be set in .env"
141+
142+
The `brain` command cannot find your `.env` file. The error message will show where it's looking.
143+
144+
Create `.env` in the recommended location:
145+
```bash
146+
mkdir -p ~/.config/brain
147+
nano ~/.config/brain/.env
148+
```
149+
150+
### "DIARY_PATH does not exist: /path/to/dir"
151+
152+
The directory doesn't exist. Create it:
153+
```bash
154+
mkdir -p /path/to/dir
155+
```
156+
157+
### "Azure OpenAI credentials required"
158+
159+
Your Azure OpenAI credentials are missing or incorrect. Double-check:
160+
1. `AZURE_OPENAI_API_KEY` is set correctly
161+
2. `AZURE_OPENAI_ENDPOINT` ends with a `/`
162+
3. `AZURE_OPENAI_DEPLOYMENT` matches your deployment name in Azure Portal
163+
164+
### .env File Not Loading
165+
166+
The `brain` CLI searches for `.env` in multiple locations automatically. If it's not loading:
167+
168+
1. **Check file exists in a searched location:**
169+
```bash
170+
ls -la ~/.config/brain/.env # Recommended location
171+
ls -la ~/.brain/.env # Alternative location
172+
ls -la .env # Current directory
173+
```
174+
175+
2. **Check file permissions (should be 600):**
176+
```bash
177+
ls -l ~/.config/brain/.env
178+
# Should show: -rw------- (readable/writable by you only)
179+
180+
# Fix if needed:
181+
chmod 600 ~/.config/brain/.env
182+
```
183+
184+
3. **Verify file has correct format:**
185+
```bash
186+
cat ~/.config/brain/.env | grep DIARY_PATH
187+
# Should show: DIARY_PATH=/your/path
188+
```
189+
190+
## Security Best Practices
191+
192+
### Protect Your API Keys
193+
194+
Your `.env` file contains sensitive Azure API keys. Follow these practices:
195+
196+
1. **File Permissions:**
197+
```bash
198+
chmod 600 ~/.config/brain/.env # Only you can read/write
199+
```
200+
201+
2. **Never Commit to Git:**
202+
```bash
203+
# If you accidentally staged it:
204+
git rm --cached .env
205+
echo ".env" >> .gitignore
206+
```
207+
208+
3. **Regular Key Rotation:**
209+
- Rotate Azure API keys periodically (every 90 days)
210+
- Update `.env` with new keys
211+
- Revoke old keys in Azure Portal
212+
213+
4. **Config Location Safety:**
214+
- `~/.config/brain/.env` is in your home directory (safe)
215+
- Current directory `.env` takes priority (be aware in shared directories)
216+
- Only load from trusted locations (the tool only searches these 3 paths)
217+
218+
### What the Tool Does
219+
220+
The `brain` CLI is designed with security in mind:
221+
222+
✅ Only searches 3 specific paths (no arbitrary file loading)
223+
✅ Validates files are regular files (not directories or special files)
224+
✅ Resolves symlinks to prevent path traversal
225+
✅ Only reads text configuration (no code execution)
226+
✅ Uses `python-dotenv` (standard, audited library)
227+
228+
### Data Privacy
229+
230+
All your data stays local:
231+
232+
- **Diary/Plan entries:** Stored in paths you specify (`DIARY_PATH`, `PLANNER_PATH`)
233+
- **Cost tracking:** SQLite database in `~/.brain/costs.db` (your machine only)
234+
- **No telemetry:** Nothing is sent except Azure OpenAI API calls (for LLM features)
235+
- **No cloud sync:** You control where files are stored (use Obsidian Sync, etc. if desired)
236+
237+
## Next Steps
238+
239+
Once configured:
240+
241+
1. **Morning routine:**
242+
```bash
243+
brain plan create today
244+
```
245+
246+
2. **Evening routine:**
247+
```bash
248+
brain diary create today
249+
# Write your reflection
250+
brain diary link today # Generate backlinks
251+
```
252+
253+
3. **Weekly review:**
254+
```bash
255+
brain diary report 7
256+
brain diary patterns 7
257+
```
258+
259+
4. **Search your notes:**
260+
```bash
261+
brain notes search "topic"
262+
```
263+
264+
5. **Track costs:**
265+
```bash
266+
brain cost summary
267+
brain cost trends 30
268+
```
269+
270+
## Updating
271+
272+
To update to the latest version:
273+
274+
```bash
275+
uv tool install --force git+https://github.com/madebygps/second-brain.git
276+
```
277+
278+
Your `.env` configuration file is not affected by updates.

0 commit comments

Comments
 (0)