Skip to content

Commit

Permalink
Merge pull request #16 from kovetskiy/skip-ancestry-improve-dry-run
Browse files Browse the repository at this point in the history
do not require full ancestry, improve dry-run mode
  • Loading branch information
kovetskiy authored Jan 8, 2020
2 parents bc647bf + 6a66bd3 commit fae91ce
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 122 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/mark
/docker
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
FROM golang:latest
ENV GOPATH="/go/"
WORKDIR /go/src/mark
ENV GOPATH="/go"
WORKDIR /go/src/github.com/kovetskiy/mark
COPY / .
RUN make get
RUN make build

FROM alpine:latest
RUN apk --no-cache add ca-certificates bash
COPY --from=0 /go/src/mark/mark /bin/
COPY --from=0 /go/src/github.com/kovetskiy/mark/mark /bin/
RUN mkdir -p /docs
WORKDIR /docs
ENTRYPOINT ["/bin/mark"]
14 changes: 14 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '2'

vars:
pwd:
sh: pwd

tasks:
confluence:
cmds:
- docker run -v {{ .pwd }}/docker:/var/atlassian/application-data/confluence
--name="confluence"
-p 8090:8090
-p 8091:8091
atlassian/confluence-server
117 changes: 24 additions & 93 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/kovetskiy/godocs"
"github.com/kovetskiy/mark/pkg/confluence"
Expand Down Expand Up @@ -123,7 +122,8 @@ Options:
-f <file> Use specified markdown file for converting to html.
-k Lock page editing to current user only to prevent accidental
manual edits over Confluence Web UI.
--dry-run Show resulting HTML and don't update Confluence page content.
--dry-run Resolve page and ancestry, show resulting HTML and exit.
--compile-only Show resulting HTML and don't update Confluence page content.
--debug Enable debug logs.
--trace Enable trace logs.
-h --help Show this screen and call 911.
Expand All @@ -139,6 +139,7 @@ func main() {

var (
targetFile, _ = args["-f"].(string)
compileOnly = args["--compile-only"].(bool)
dryRun = args["--dry-run"].(bool)
editLock = args["-k"].(bool)
)
Expand Down Expand Up @@ -205,6 +206,15 @@ func main() {
}

if dryRun {
compileOnly = true

_, _, err := mark.ResolvePage(dryRun, api, meta)
if err != nil {
log.Fatalf(err, "unable to resolve page location")
}
}

if compileOnly {
fmt.Println(mark.CompileMarkdown(markdown, stdlib))
os.Exit(0)
}
Expand All @@ -229,14 +239,25 @@ func main() {
var target *confluence.PageInfo

if meta != nil {
page, err := resolvePage(api, meta)
parent, page, err := mark.ResolvePage(dryRun, api, meta)
if err != nil {
log.Fatalf(
karma.Describe("title", meta.Title).Reason(err),
"unable to resolve page",
)
}

if page == nil {
page, err = api.CreatePage(meta.Space, parent, meta.Title, ``)
if err != nil {
log.Fatalf(
err,
"can't create page %q",
meta.Title,
)
}
}

target = page
} else {
if creds.PageID == "" {
Expand Down Expand Up @@ -307,94 +328,4 @@ func main() {
"page successfully updated: %s\n",
creds.BaseURL+target.Links.Full,
)

}

func resolvePage(
api *confluence.API,
meta *mark.Meta,
) (*confluence.PageInfo, error) {
page, err := api.FindPage(meta.Space, meta.Title)
if err != nil {
return nil, karma.Format(
err,
"error during finding page %q",
meta.Title,
)
}

ancestry := meta.Parents
if page != nil {
ancestry = append(ancestry, page.Title)
}

if len(ancestry) > 0 {
page, err := mark.ValidateAncestry(
api,
meta.Space,
ancestry,
)
if err != nil {
return nil, err
}

if page == nil {
log.Warningf(
nil,
"page %q is not found ",
meta.Parents[len(ancestry)-1],
)
}

path := meta.Parents
path = append(path, meta.Title)

log.Debugf(
nil,
"resolving page path: ??? > %s",
strings.Join(path, ` > `),
)
}

parent, err := mark.EnsureAncestry(
api,
meta.Space,
meta.Parents,
)
if err != nil {
return nil, karma.Format(
err,
"can't create ancestry tree: %s",
strings.Join(meta.Parents, ` > `),
)
}

titles := []string{}
for _, page := range parent.Ancestors {
titles = append(titles, page.Title)
}

titles = append(titles, parent.Title)

log.Infof(
nil,
"page will be stored under path: %s > %s",
strings.Join(titles, ` > `),
meta.Title,
)

if page == nil {
page, err := api.CreatePage(meta.Space, parent, meta.Title, ``)
if err != nil {
return nil, karma.Format(
err,
"can't create page %q",
meta.Title,
)
}

return page, nil
}

return page, nil
}
67 changes: 46 additions & 21 deletions pkg/mark/ancestry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func EnsureAncestry(
dryRun bool,
api *confluence.API,
space string,
ancestry []string,
Expand Down Expand Up @@ -57,23 +58,33 @@ func EnsureAncestry(
}

log.Debugf(
nil,
nil,
"empty pages under %q to be created: %s",
parent.Title,
strings.Join(rest, ` > `),
)

for _, title := range rest {
page, err := api.CreatePage(space, parent, title, ``)
if err != nil {
return nil, karma.Format(
err,
`error during creating parent page with title %q`,
title,
)
if !dryRun {
for _, title := range rest {
page, err := api.CreatePage(space, parent, title, ``)
if err != nil {
return nil, karma.Format(
err,
`error during creating parent page with title %q`,
title,
)
}

parent = page
}

parent = page
} else {
log.Infof(
nil,
"skipping page creation due to enabled dry-run mode, "+
"need to create %d pages: %v",
len(rest),
rest,
)
}

return parent, nil
Expand Down Expand Up @@ -105,16 +116,30 @@ func ValidateAncestry(
)
}

// skipping root article title
for i, ancestor := range page.Ancestors[1:len(ancestry)] {
if ancestor.Title != ancestry[i] {
return nil, fmt.Errorf(
"broken ancestry tree; expected tree: %s; "+
"encountered %q at position of %q",
strings.Join(ancestry, ` > `),
ancestor.Title,
ancestry[i],
)
for _, parent := range ancestry[:len(ancestry)-1] {
found := false

// skipping root article title
for _, ancestor := range page.Ancestors[1:] {
if ancestor.Title == parent {
found = true
break
}
}

if !found {
list := []string{}

for _, ancestor := range page.Ancestors[1:] {
list = append(list, ancestor.Title)
}

return nil, karma.Describe("expected parent", parent).
Describe("list", strings.Join(list, "; ")).
Format(
nil,
"unexpected ancestry tree, did not find expected parent page in the tree",
)
}
}

Expand Down
12 changes: 7 additions & 5 deletions pkg/mark/mark.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
)

func ResolvePage(
dryRun bool,
api *confluence.API,
meta *Meta,
) (*confluence.PageInfo, error) {
) (*confluence.PageInfo, *confluence.PageInfo, error) {
page, err := api.FindPage(meta.Space, meta.Title)
if err != nil {
return nil, karma.Format(
return nil, nil, karma.Format(
err,
"error while finding page %q",
meta.Title,
Expand All @@ -33,7 +34,7 @@ func ResolvePage(
ancestry,
)
if err != nil {
return nil, err
return nil, nil, err
}

if page == nil {
Expand All @@ -55,12 +56,13 @@ func ResolvePage(
}

parent, err := EnsureAncestry(
dryRun,
api,
meta.Space,
meta.Parents,
)
if err != nil {
return nil, karma.Format(
return nil, nil, karma.Format(
err,
"can't create ancestry tree: %s",
strings.Join(meta.Parents, ` > `),
Expand All @@ -81,5 +83,5 @@ func ResolvePage(
meta.Title,
)

return page, nil
return parent, page, nil
}

0 comments on commit fae91ce

Please sign in to comment.