add support for fileglobs

This commit is contained in:
wbhegedus 2021-04-02 13:15:54 -04:00
parent 12510a1208
commit 403efe3acc
2 changed files with 77 additions and 37 deletions

View File

@ -299,7 +299,7 @@ mark -h | --help
If -l is not specified, file should contain metadata (see above). If -l is not specified, file should contain metadata (see above).
- `-b <url>` or `--base-url <url>` Base URL for Confluence. - `-b <url>` or `--base-url <url>` Base URL for Confluence.
Alternative option for base_url config field. Alternative option for base_url config field.
- `-f <file>` — Use specified markdown file for converting to html. - `-f <file>` — Use specified markdown file(s) for converting to html. Supports file globbing patterns (needs to be quoted).
- `-c <file>` — Specify configuration file which should be used for reading - `-c <file>` — Specify configuration file which should be used for reading
Confluence page URL and markdown file path. Confluence page URL and markdown file path.
- `-k` — Lock page editing to current user only to prevent accidental - `-k` — Lock page editing to current user only to prevent accidental
@ -369,3 +369,10 @@ only:
branches: branches:
- main - main
``` ```
## File Globbing
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:
```bash
mark -f "helpful_cmds/*.md"
```

105
main.go
View File

@ -18,6 +18,16 @@ import (
"github.com/reconquest/pkg/log" "github.com/reconquest/pkg/log"
) )
type markFlags struct {
fileGlobPatten string
compileOnly bool
dryRun bool
editLock bool
dropH1 bool
minorEdit bool
color string
}
const ( const (
usage = `mark - a tool for updating Atlassian Confluence pages from markdown. usage = `mark - a tool for updating Atlassian Confluence pages from markdown.
@ -38,7 +48,7 @@ Options:
above). above).
-b --base-url <url> Base URL for Confluence. -b --base-url <url> Base URL for Confluence.
Alternative option for base_url config field. Alternative option for base_url config field.
-f <file> Use specified markdown file for converting to html. -f <file> Use specified markdown file(s) for converting to html. Supports file globbing patterns (needs to be quoted).
-k Lock page editing to current user only to prevent accidental -k Lock page editing to current user only to prevent accidental
manual edits over Confluence Web UI. manual edits over Confluence Web UI.
--drop-h1 Don't include H1 headings in Confluence output. --drop-h1 Don't include H1 headings in Confluence output.
@ -55,20 +65,20 @@ Options:
) )
func main() { func main() {
args, err := docopt.Parse(usage, nil, true, "5.5", false) args, err := docopt.ParseArgs(usage, nil, "5.6")
if err != nil { if err != nil {
panic(err) panic(err)
} }
var ( flags := &markFlags{
targetFile, _ = args["-f"].(string) fileGlobPatten: args["-f"].(string),
compileOnly = args["--compile-only"].(bool) compileOnly: args["--compile-only"].(bool),
dryRun = args["--dry-run"].(bool) dryRun: args["--dry-run"].(bool),
editLock = args["-k"].(bool) editLock: args["-k"].(bool),
dropH1 = args["--drop-h1"].(bool) dropH1: args["--drop-h1"].(bool),
minorEdit = args["--minor-edit"].(bool) minorEdit: args["--minor-edit"].(bool),
color = args["--color"].(string) color: args["--color"].(string),
) }
if args["--debug"].(bool) { if args["--debug"].(bool) {
log.SetLevel(lorg.LevelDebug) log.SetLevel(lorg.LevelDebug)
@ -78,7 +88,7 @@ func main() {
log.SetLevel(lorg.LevelTrace) log.SetLevel(lorg.LevelTrace)
} }
if color == "never" { if flags.color == "never" {
log.GetLogger().SetFormat( log.GetLogger().SetFormat(
lorg.NewFormat( lorg.NewFormat(
`${time:2006-01-02 15:04:05.000} ${level:%s:left:true} ${prefix}%s`, `${time:2006-01-02 15:04:05.000} ${level:%s:left:true} ${prefix}%s`,
@ -99,7 +109,38 @@ func main() {
api := confluence.NewAPI(creds.BaseURL, creds.Username, creds.Password) api := confluence.NewAPI(creds.BaseURL, creds.Username, creds.Password)
markdown, err := ioutil.ReadFile(targetFile) files, err := filepath.Glob(flags.fileGlobPatten)
if err != nil {
log.Fatal(err)
}
if len(files) == 0 {
log.Fatal("No files matched.")
}
// Loop through files matched by glob pattern
for _, file := range files {
log.Infof(
nil,
"Processing %s...",
file,
)
target := processFile(file, api, flags, creds.PageID, creds.Username)
log.Infof(
nil,
"page successfully updated: %s",
creds.BaseURL+target.Links.Full,
)
fmt.Println(
"Page available at:", creds.BaseURL+target.Links.Full,
)
}
}
func processFile(file string, api *confluence.API, flags *markFlags, pageID string, username string) *confluence.PageInfo {
markdown, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -153,21 +194,21 @@ func main() {
markdown = mark.SubstituteLinks(markdown, links) markdown = mark.SubstituteLinks(markdown, links)
if dryRun { if flags.dryRun {
compileOnly = true flags.compileOnly = true
_, _, err := mark.ResolvePage(dryRun, api, meta) _, _, err := mark.ResolvePage(flags.dryRun, api, meta)
if err != nil { if err != nil {
log.Fatalf(err, "unable to resolve page location") log.Fatalf(err, "unable to resolve page location")
} }
} }
if compileOnly { if flags.compileOnly {
fmt.Println(mark.CompileMarkdown(markdown, stdlib)) fmt.Println(mark.CompileMarkdown(markdown, stdlib))
os.Exit(0) os.Exit(0)
} }
if creds.PageID != "" && meta != nil { if pageID != "" && meta != nil {
log.Warning( log.Warning(
`specified file contains metadata, ` + `specified file contains metadata, ` +
`but it will be ignored due specified command line URL`, `but it will be ignored due specified command line URL`,
@ -176,7 +217,7 @@ func main() {
meta = nil meta = nil
} }
if creds.PageID == "" && meta == nil { if pageID == "" && meta == nil {
log.Fatal( log.Fatal(
`specified file doesn't contain metadata ` + `specified file doesn't contain metadata ` +
`and URL is not specified via command line ` + `and URL is not specified via command line ` +
@ -187,7 +228,7 @@ func main() {
var target *confluence.PageInfo var target *confluence.PageInfo
if meta != nil { if meta != nil {
parent, page, err := mark.ResolvePage(dryRun, api, meta) parent, page, err := mark.ResolvePage(flags.dryRun, api, meta)
if err != nil { if err != nil {
log.Fatalf( log.Fatalf(
karma.Describe("title", meta.Title).Reason(err), karma.Describe("title", meta.Title).Reason(err),
@ -208,11 +249,11 @@ func main() {
target = page target = page
} else { } else {
if creds.PageID == "" { if pageID == "" {
log.Fatalf(nil, "URL should provide 'pageId' GET-parameter") log.Fatalf(nil, "URL should provide 'pageId' GET-parameter")
} }
page, err := api.GetPageByID(creds.PageID) page, err := api.GetPageByID(pageID)
if err != nil { if err != nil {
log.Fatalf(err, "unable to retrieve page by id") log.Fatalf(err, "unable to retrieve page by id")
} }
@ -227,7 +268,7 @@ func main() {
markdown = mark.CompileAttachmentLinks(markdown, attaches) markdown = mark.CompileAttachmentLinks(markdown, attaches)
if dropH1 { if flags.dropH1 {
log.Info( log.Info(
"Leading H1 heading will be excluded from the Confluence output", "Leading H1 heading will be excluded from the Confluence output",
) )
@ -257,35 +298,27 @@ func main() {
html = buffer.String() html = buffer.String()
} }
err = api.UpdatePage(target, html, minorEdit, meta.Labels) err = api.UpdatePage(target, html, flags.minorEdit, meta.Labels)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if editLock { if flags.editLock {
log.Infof( log.Infof(
nil, nil,
`edit locked on page %q by user %q to prevent manual edits`, `edit locked on page %q by user %q to prevent manual edits`,
target.Title, target.Title,
creds.Username, username,
) )
err := api.RestrictPageUpdates( err := api.RestrictPageUpdates(
target, target,
creds.Username, username,
) )
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
log.Infof( return target
nil,
"page successfully updated: %s",
creds.BaseURL+target.Links.Full,
)
fmt.Println(
creds.BaseURL + target.Links.Full,
)
} }