Skip to content

Commit e725773

Browse files
authored
Merge pull request #85 from wbh1/add/fileglobbing
Add support for fileglobs
2 parents 1daedcd + 403efe3 commit e725773

2 files changed

Lines changed: 77 additions & 37 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ mark -h | --help
306306
If -l is not specified, file should contain metadata (see above).
307307
- `-b <url>` or `--base-url <url>` – Base URL for Confluence.
308308
Alternative option for base_url config field.
309-
- `-f <file>` — Use specified markdown file for converting to html.
309+
- `-f <file>` — Use specified markdown file(s) for converting to html. Supports file globbing patterns (needs to be quoted).
310310
- `-c <file>` — Specify configuration file which should be used for reading
311311
Confluence page URL and markdown file path.
312312
- `-k` — Lock page editing to current user only to prevent accidental
@@ -376,3 +376,10 @@ only:
376376
branches:
377377
- main
378378
```
379+
380+
## File Globbing
381+
382+
Rather than running `mark` multiple times, or looping through a list of files from `find`, you can use file globbing (i.e. wildcard patterns) to match files in subdirectories. For example:
383+
```bash
384+
mark -f "helpful_cmds/*.md"
385+
```

main.go

Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ import (
1818
"github.com/reconquest/pkg/log"
1919
)
2020

21+
type markFlags struct {
22+
fileGlobPatten string
23+
compileOnly bool
24+
dryRun bool
25+
editLock bool
26+
dropH1 bool
27+
minorEdit bool
28+
color string
29+
}
30+
2131
const (
2232
usage = `mark - a tool for updating Atlassian Confluence pages from markdown.
2333
@@ -38,7 +48,7 @@ Options:
3848
above).
3949
-b --base-url <url> Base URL for Confluence.
4050
Alternative option for base_url config field.
41-
-f <file> Use specified markdown file for converting to html.
51+
-f <file> Use specified markdown file(s) for converting to html. Supports file globbing patterns (needs to be quoted).
4252
-k Lock page editing to current user only to prevent accidental
4353
manual edits over Confluence Web UI.
4454
--drop-h1 Don't include H1 headings in Confluence output.
@@ -55,20 +65,20 @@ Options:
5565
)
5666

5767
func main() {
58-
args, err := docopt.Parse(usage, nil, true, "5.5", false)
68+
args, err := docopt.ParseArgs(usage, nil, "5.6")
5969
if err != nil {
6070
panic(err)
6171
}
6272

63-
var (
64-
targetFile, _ = args["-f"].(string)
65-
compileOnly = args["--compile-only"].(bool)
66-
dryRun = args["--dry-run"].(bool)
67-
editLock = args["-k"].(bool)
68-
dropH1 = args["--drop-h1"].(bool)
69-
minorEdit = args["--minor-edit"].(bool)
70-
color = args["--color"].(string)
71-
)
73+
flags := &markFlags{
74+
fileGlobPatten: args["-f"].(string),
75+
compileOnly: args["--compile-only"].(bool),
76+
dryRun: args["--dry-run"].(bool),
77+
editLock: args["-k"].(bool),
78+
dropH1: args["--drop-h1"].(bool),
79+
minorEdit: args["--minor-edit"].(bool),
80+
color: args["--color"].(string),
81+
}
7282

7383
if args["--debug"].(bool) {
7484
log.SetLevel(lorg.LevelDebug)
@@ -78,7 +88,7 @@ func main() {
7888
log.SetLevel(lorg.LevelTrace)
7989
}
8090

81-
if color == "never" {
91+
if flags.color == "never" {
8292
log.GetLogger().SetFormat(
8393
lorg.NewFormat(
8494
`${time:2006-01-02 15:04:05.000} ${level:%s:left:true} ${prefix}%s`,
@@ -99,7 +109,38 @@ func main() {
99109

100110
api := confluence.NewAPI(creds.BaseURL, creds.Username, creds.Password)
101111

102-
markdown, err := ioutil.ReadFile(targetFile)
112+
files, err := filepath.Glob(flags.fileGlobPatten)
113+
if err != nil {
114+
log.Fatal(err)
115+
}
116+
if len(files) == 0 {
117+
log.Fatal("No files matched.")
118+
}
119+
120+
// Loop through files matched by glob pattern
121+
for _, file := range files {
122+
log.Infof(
123+
nil,
124+
"Processing %s...",
125+
file,
126+
)
127+
128+
target := processFile(file, api, flags, creds.PageID, creds.Username)
129+
130+
log.Infof(
131+
nil,
132+
"page successfully updated: %s",
133+
creds.BaseURL+target.Links.Full,
134+
)
135+
136+
fmt.Println(
137+
"Page available at:", creds.BaseURL+target.Links.Full,
138+
)
139+
}
140+
}
141+
142+
func processFile(file string, api *confluence.API, flags *markFlags, pageID string, username string) *confluence.PageInfo {
143+
markdown, err := ioutil.ReadFile(file)
103144
if err != nil {
104145
log.Fatal(err)
105146
}
@@ -153,21 +194,21 @@ func main() {
153194

154195
markdown = mark.SubstituteLinks(markdown, links)
155196

156-
if dryRun {
157-
compileOnly = true
197+
if flags.dryRun {
198+
flags.compileOnly = true
158199

159-
_, _, err := mark.ResolvePage(dryRun, api, meta)
200+
_, _, err := mark.ResolvePage(flags.dryRun, api, meta)
160201
if err != nil {
161202
log.Fatalf(err, "unable to resolve page location")
162203
}
163204
}
164205

165-
if compileOnly {
206+
if flags.compileOnly {
166207
fmt.Println(mark.CompileMarkdown(markdown, stdlib))
167208
os.Exit(0)
168209
}
169210

170-
if creds.PageID != "" && meta != nil {
211+
if pageID != "" && meta != nil {
171212
log.Warning(
172213
`specified file contains metadata, ` +
173214
`but it will be ignored due specified command line URL`,
@@ -176,7 +217,7 @@ func main() {
176217
meta = nil
177218
}
178219

179-
if creds.PageID == "" && meta == nil {
220+
if pageID == "" && meta == nil {
180221
log.Fatal(
181222
`specified file doesn't contain metadata ` +
182223
`and URL is not specified via command line ` +
@@ -187,7 +228,7 @@ func main() {
187228
var target *confluence.PageInfo
188229

189230
if meta != nil {
190-
parent, page, err := mark.ResolvePage(dryRun, api, meta)
231+
parent, page, err := mark.ResolvePage(flags.dryRun, api, meta)
191232
if err != nil {
192233
log.Fatalf(
193234
karma.Describe("title", meta.Title).Reason(err),
@@ -210,11 +251,11 @@ func main() {
210251

211252
target = page
212253
} else {
213-
if creds.PageID == "" {
254+
if pageID == "" {
214255
log.Fatalf(nil, "URL should provide 'pageId' GET-parameter")
215256
}
216257

217-
page, err := api.GetPageByID(creds.PageID)
258+
page, err := api.GetPageByID(pageID)
218259
if err != nil {
219260
log.Fatalf(err, "unable to retrieve page by id")
220261
}
@@ -229,7 +270,7 @@ func main() {
229270

230271
markdown = mark.CompileAttachmentLinks(markdown, attaches)
231272

232-
if dropH1 {
273+
if flags.dropH1 {
233274
log.Info(
234275
"Leading H1 heading will be excluded from the Confluence output",
235276
)
@@ -259,35 +300,27 @@ func main() {
259300
html = buffer.String()
260301
}
261302

262-
err = api.UpdatePage(target, html, minorEdit, meta.Labels)
303+
err = api.UpdatePage(target, html, flags.minorEdit, meta.Labels)
263304
if err != nil {
264305
log.Fatal(err)
265306
}
266307

267-
if editLock {
308+
if flags.editLock {
268309
log.Infof(
269310
nil,
270311
`edit locked on page %q by user %q to prevent manual edits`,
271312
target.Title,
272-
creds.Username,
313+
username,
273314
)
274315

275316
err := api.RestrictPageUpdates(
276317
target,
277-
creds.Username,
318+
username,
278319
)
279320
if err != nil {
280321
log.Fatal(err)
281322
}
282323
}
283324

284-
log.Infof(
285-
nil,
286-
"page successfully updated: %s",
287-
creds.BaseURL+target.Links.Full,
288-
)
289-
290-
fmt.Println(
291-
creds.BaseURL + target.Links.Full,
292-
)
325+
return target
293326
}

0 commit comments

Comments
 (0)