Skip to content

Commit 2009d8e

Browse files
authored
feat: add --content flag to daily command (#122)
* feat: add --content flag to daily command * docs: add --content flag examples to daily command in README
1 parent 5ffcc9b commit 2009d8e

4 files changed

Lines changed: 110 additions & 6 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ notesmd-cli daily --vault "{vault-name}"
238238

239239
# Creates / opens daily note in your default editor
240240
notesmd-cli daily --editor
241+
242+
# Adds content to daily note (appends if note already exists)
243+
notesmd-cli daily --content "abcde"
244+
245+
# Adds content and opens in editor
246+
notesmd-cli daily --content "abcde" --editor
241247
```
242248

243249
### Search Note

cmd/daily.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var DailyCmd = &cobra.Command{
1818
uri := obsidian.Uri{}
1919

2020
err := actions.DailyNote(&vault, &uri, actions.DailyParams{
21+
Content: dailyContent,
2122
UseEditor: resolveUseEditor(cmd, &vault),
2223
})
2324
if err != nil {
@@ -26,8 +27,11 @@ var DailyCmd = &cobra.Command{
2627
},
2728
}
2829

30+
var dailyContent string
31+
2932
func init() {
3033
DailyCmd.Flags().StringVarP(&vaultName, "vault", "v", "", "vault name (not required if default is set)")
34+
DailyCmd.Flags().StringVarP(&dailyContent, "content", "c", "", "text to add to daily note (appends if note exists)")
3135
DailyCmd.Flags().BoolP("editor", "e", false, "open in editor instead of Obsidian")
3236
rootCmd.AddCommand(DailyCmd)
3337
}

pkg/actions/daily.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
)
1111

1212
type DailyParams struct {
13+
Content string
1314
UseEditor bool
1415
}
1516

@@ -48,17 +49,30 @@ func DailyNote(vault obsidian.VaultManager, uri obsidian.UriManager, params Dail
4849
}
4950

5051
// Read template content if configured.
51-
content := ""
52+
templateContent := ""
5253
if config.Template != "" {
5354
templatePath := filepath.Join(vaultPath, obsidian.AddMdSuffix(config.Template))
54-
if templateContent, readErr := os.ReadFile(templatePath); readErr == nil {
55-
content = string(templateContent)
55+
if data, readErr := os.ReadFile(templatePath); readErr == nil {
56+
templateContent = string(data)
5657
}
5758
}
5859

59-
// WriteNoteFile leaves existing files unchanged (no append/overwrite).
60-
if err := WriteNoteFile(notePath, content, false, false); err != nil {
61-
return err
60+
normalizedContent := NormalizeContent(params.Content)
61+
62+
_, statErr := os.Stat(notePath)
63+
fileExists := statErr == nil
64+
65+
if fileExists && normalizedContent != "" {
66+
// Append user content to existing daily note.
67+
if err := WriteNoteFile(notePath, normalizedContent, true, false); err != nil {
68+
return err
69+
}
70+
} else if !fileExists {
71+
// Create new daily note with template + content.
72+
newContent := templateContent + normalizedContent
73+
if err := WriteNoteFile(notePath, newContent, false, false); err != nil {
74+
return err
75+
}
6276
}
6377

6478
// Open the note.

pkg/actions/daily_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,86 @@ func TestDailyNote(t *testing.T) {
147147
assert.Equal(t, uri.ExecuteErr, err)
148148
})
149149

150+
t.Run("Creates daily note with content", func(t *testing.T) {
151+
tmpDir := t.TempDir()
152+
vault := mocks.MockVaultOperator{Name: "myVault", PathValue: tmpDir}
153+
uri := mocks.MockUriManager{}
154+
155+
err := actions.DailyNote(&vault, &uri, actions.DailyParams{
156+
Content: "hello world",
157+
})
158+
assert.NoError(t, err)
159+
160+
data, _ := os.ReadFile(filepath.Join(tmpDir, today+".md"))
161+
assert.Equal(t, "hello world", string(data))
162+
})
163+
164+
t.Run("Creates daily note with template and content", func(t *testing.T) {
165+
tmpDir := t.TempDir()
166+
obsDir := filepath.Join(tmpDir, ".obsidian")
167+
if err := os.MkdirAll(obsDir, 0755); err != nil {
168+
t.Fatal(err)
169+
}
170+
if err := os.WriteFile(filepath.Join(obsDir, "daily-notes.json"), []byte(`{
171+
"template": "Templates/Daily"
172+
}`), 0644); err != nil {
173+
t.Fatal(err)
174+
}
175+
if err := os.MkdirAll(filepath.Join(tmpDir, "Templates"), 0755); err != nil {
176+
t.Fatal(err)
177+
}
178+
if err := os.WriteFile(filepath.Join(tmpDir, "Templates", "Daily.md"), []byte("# Daily Note\n"), 0644); err != nil {
179+
t.Fatal(err)
180+
}
181+
182+
vault := mocks.MockVaultOperator{Name: "myVault", PathValue: tmpDir}
183+
uri := mocks.MockUriManager{}
184+
185+
err := actions.DailyNote(&vault, &uri, actions.DailyParams{
186+
Content: "- task 1",
187+
})
188+
assert.NoError(t, err)
189+
190+
data, _ := os.ReadFile(filepath.Join(tmpDir, today+".md"))
191+
assert.Equal(t, "# Daily Note\n- task 1", string(data))
192+
})
193+
194+
t.Run("Appends content to existing daily note", func(t *testing.T) {
195+
tmpDir := t.TempDir()
196+
notePath := filepath.Join(tmpDir, today+".md")
197+
if err := os.WriteFile(notePath, []byte("existing content"), 0644); err != nil {
198+
t.Fatal(err)
199+
}
200+
201+
vault := mocks.MockVaultOperator{Name: "myVault", PathValue: tmpDir}
202+
uri := mocks.MockUriManager{}
203+
204+
err := actions.DailyNote(&vault, &uri, actions.DailyParams{
205+
Content: "\\nnew line",
206+
})
207+
assert.NoError(t, err)
208+
209+
data, _ := os.ReadFile(notePath)
210+
assert.Equal(t, "existing content\nnew line", string(data))
211+
})
212+
213+
t.Run("Does not modify existing daily note without content", func(t *testing.T) {
214+
tmpDir := t.TempDir()
215+
notePath := filepath.Join(tmpDir, today+".md")
216+
if err := os.WriteFile(notePath, []byte("existing content"), 0644); err != nil {
217+
t.Fatal(err)
218+
}
219+
220+
vault := mocks.MockVaultOperator{Name: "myVault", PathValue: tmpDir}
221+
uri := mocks.MockUriManager{}
222+
223+
err := actions.DailyNote(&vault, &uri, actions.DailyParams{})
224+
assert.NoError(t, err)
225+
226+
data, _ := os.ReadFile(notePath)
227+
assert.Equal(t, "existing content", string(data))
228+
})
229+
150230
t.Run("Creates daily note with custom format", func(t *testing.T) {
151231
tmpDir := t.TempDir()
152232
obsDir := filepath.Join(tmpDir, ".obsidian")

0 commit comments

Comments
 (0)