Skip to content

Commit 25118d0

Browse files
TIL-23 Update readme for new configuration options
Signed-off-by: Chris Cummer <[email protected]>
1 parent c421c37 commit 25118d0

File tree

3 files changed

+86
-14
lines changed

3 files changed

+86
-14
lines changed

README.md

+36-5
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,17 @@ Open `~/.config/til/config.yml`, change the following entries, and save it:
7070
* committerEmail
7171
* committerName
7272
* editor
73-
* targetDirectory
73+
* targetDirectories
7474

7575
`committerEmail` and `committerName` are the values `til` will use to commit changes with when you run `til -save`.
7676

7777
`editor` is the text editor `til` will open your file in when you run `til [some title here]`.
7878

79-
`targetDirectory` is where `til` will write your files to. If the target directory does not exist, `til` will try to create it.
79+
`targetDirectories` defines the locations that `til` will write your files to. If a specified target directory does not exist, `til` will try to create it. This is a map of key/value pairs, where the "key" defines the value to pass in using the `-target` flag, and the "value" is the path to the directory.
80+
81+
If only one target directory is defined in the configuration, the `-target` flag can be ommitted from all commands.
82+
If multiple target diretories are defined in the configuration, all commands must include the `-target` flag specifying
83+
which target directory to operate against.
8084

8185
### Config Example
8286

@@ -86,7 +90,9 @@ commitMessage: "build, save, push"
8690
committerEmail: [email protected]
8791
committerName: "TIL Autobot"
8892
editor: "mvim"
89-
targetDirectory: "~/Documents/til"
93+
targetDirectories:
94+
a: ~/Documents/notes
95+
b: ~/Documents/blog
9096
```
9197

9298
## Usage
@@ -95,34 +101,59 @@ targetDirectory: "~/Documents/til"
95101

96102
### Creating a new page
97103

104+
With one target directory defined in the configuration:
105+
98106
```bash
99107
❯ til New title here
100108
2020-04-20T14-52-57-new-title-here.md
101109
```
102110

111+
With multiple target directories defined:
112+
113+
```bash
114+
❯ til -target a New title here
115+
2020-04-20T14-52-57-new-title-here.md
116+
```
117+
103118
That new page will open in whichever editor you've defined in your config.
104119

105120
### Building static pages
106121

122+
With one target directory defined in the configuration:
123+
107124
```bash
108125
❯ til -build
109126
```
110127

128+
With multiple target directories defined:
129+
130+
```bash
131+
❯ til -target a -build
132+
```
133+
111134
Builds the index and tag pages, and leaves them uncommitted.
112135

113136
<p align="center"><img src="images/til_build.png" width="600" height="213" alt="image of the build process" title="til -build" /></p>
114137

115138
### Building, saving, committing, and pushing
116139

140+
With one target directory defined in the configuration:
141+
117142
```bash
118143
❯ til -save [optional commit message]
119144
```
120145

146+
With multiple target directories defined:
147+
148+
```bash
149+
❯ til -target a -save [optional commit message]
150+
```
151+
121152
Builds the index and tag pages, commits everything to the git repo with the commit message you've defined in your config, and pushes it all up to the remote repo.
122153

123-
`-save` makes a hard assumption that your `targetDirectory` is under version control, controlled by `git`. It is highly recommended that you do this.
154+
`-save` makes a hard assumption that your target directory is under version control, controlled by `git`. It is recommended that you do this.
124155

125-
`-save` also makes a soft assumption that your `targetDirectory` has `remote` set to GitHub (but it should work with `remote` set to anywhere).
156+
`-save` also makes a soft assumption that your target directory has `remote` set to GitHub (but it should work with `remote` set to anywhere).
126157

127158
`-save` takes an optional commit message. If that message is supplied, it will be used as the commit message. If that message is not supplied, the `commitMessage` value in the config file will be used. If that value is not supplied, an error will be raised.
128159

til.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func readConfigFile() {
257257
// the config file, exists and contains a /docs folder for writing pages to.
258258
// If these directories don't exist, it tries to create them
259259
func buildTargetDirectory() {
260-
tDir := getTargetDir(true)
260+
tDir := getTargetDir(globalConfig, true)
261261

262262
if _, err := os.Stat(tDir); os.IsNotExist(err) {
263263
err := os.MkdirAll(tDir, os.ModePerm)
@@ -269,7 +269,7 @@ func buildTargetDirectory() {
269269

270270
// getTargetDir returns the absolute string path to the directory that the
271271
// content will be written to
272-
func getTargetDir(withDocsDir bool) string {
272+
func getTargetDir(cfg *config.Config, withDocsDir bool) string {
273273
docsBit := ""
274274
if withDocsDir {
275275
docsBit = "/docs"
@@ -281,7 +281,7 @@ func getTargetDir(withDocsDir bool) string {
281281
// targetDirectories:
282282
// a: ~/Documents/blog
283283
// b: ~/Documents/notes
284-
uDirs, err := globalConfig.Map("targetDirectories")
284+
uDirs, err := cfg.Map("targetDirectories")
285285
if err != nil {
286286
Defeat(err)
287287
}
@@ -366,7 +366,7 @@ func buildIndexPage(pages []*Page, tagMap *TagMap) {
366366
// And write the file to disk
367367
filePath := fmt.Sprintf(
368368
"%s/index.%s",
369-
getTargetDir(true),
369+
getTargetDir(globalConfig, true),
370370
fileExtension,
371371
)
372372

@@ -404,7 +404,7 @@ func buildTagPages(pages []*Page) *TagMap {
404404
// And write the file to disk
405405
filePath := fmt.Sprintf(
406406
"%s/%s.%s",
407-
getTargetDir(true),
407+
getTargetDir(globalConfig, true),
408408
tagName,
409409
fileExtension,
410410
)
@@ -441,7 +441,7 @@ func createNewPage(title string) string {
441441
// Write out the stub file, explode if we can't do that
442442
filePath := fmt.Sprintf(
443443
"%s/%s-%s.%s",
444-
getTargetDir(true),
444+
getTargetDir(globalConfig, true),
445445
pathDate,
446446
strings.ReplaceAll(strings.ToLower(title), " ", "-"),
447447
fileExtension,
@@ -476,7 +476,7 @@ func loadPages() []*Page {
476476
filePaths, _ := filepath.Glob(
477477
fmt.Sprintf(
478478
"%s/*.%s",
479-
getTargetDir(true),
479+
getTargetDir(globalConfig, true),
480480
fileExtension,
481481
),
482482
)
@@ -526,7 +526,7 @@ func parseTitle(targetFlag string, args []string) string {
526526
func push() {
527527
Info(statusRepoPush)
528528

529-
tDir := getTargetDir(false)
529+
tDir := getTargetDir(globalConfig, false)
530530

531531
r, err := git.PlainOpen(tDir)
532532
if err != nil {
@@ -563,7 +563,7 @@ func readPage(filePath string) *Page {
563563
func save(commitMsg string) {
564564
Info(statusRepoSave)
565565

566-
tDir := getTargetDir(false)
566+
tDir := getTargetDir(globalConfig, false)
567567

568568
r, err := git.PlainOpen(tDir)
569569
if err != nil {

til_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,50 @@ package main
33
import (
44
"testing"
55

6+
"github.com/olebedev/config"
67
"github.com/stretchr/testify/assert"
78
)
89

10+
/* -------------------- Target Directory -------------------- */
11+
12+
func Test_getTargetDir(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
config *config.Config
16+
withDocsDir bool
17+
expectedPath string
18+
expectedErr error
19+
}{
20+
{
21+
name: "with no targetDirectories config key",
22+
},
23+
{
24+
name: "with no target directories defined",
25+
},
26+
{
27+
name: "with one target directory defined",
28+
},
29+
{
30+
name: "with multiple target directories defined and invalid key",
31+
},
32+
{
33+
name: "with multiple target directories defined and valid key",
34+
},
35+
{
36+
name: "with a home directory-relative path",
37+
},
38+
{
39+
name: "with an absolute path",
40+
},
41+
}
42+
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
46+
})
47+
}
48+
}
49+
950
/* -------------------- More Helper Functions -------------------- */
1051

1152
func Test_Colour(t *testing.T) {

0 commit comments

Comments
 (0)