Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/testdata
.idea/
/mark.test
.vscode/*
54 changes: 48 additions & 6 deletions main.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Flags struct {
Username string `docopt:"-u"`
Password string `docopt:"-p"`
TargetURL string `docopt:"-l"`
FilesList []string `docopt:"-L"`
BaseURL string `docopt:"--base-url"`
Config string `docopt:"--config"`
Ci bool `docopt:"--ci"`
Expand All @@ -48,6 +49,7 @@ Docs: https://github.com/kovetskiy/mark
Usage:
mark [options] [-u <username>] [-p <token>] [-k] [-l <url>] -f <file>
mark [options] [-u <username>] [-p <password>] [-k] [-b <url>] -f <file>
mark [options] [-u <username>] [-p <password>] [-k] [-b <url>] [-L <file>...]
mark -v | --version
mark -h | --help

Expand All @@ -60,6 +62,7 @@ Options:
-l <url> Edit specified Confluence page.
If -l is not specified, file should contain metadata (see
above).
-L <fileslist> List of files. Use this option multiple times to pass files to Confluence.
-b --base-url <url> Base URL for Confluence.
Alternative option for base_url config field.
-f <file> Use specified markdown file(s) for converting to html.
Expand Down Expand Up @@ -87,6 +90,12 @@ Options:
)

func main() {
// args := []string{"-u","",
// "-p", "",
// "-k",
// "-b", "",
// "-L", "/home/xxxxx/file",
// "-L", "/home/xxxxx/file"}
cmd, err := docopt.ParseArgs(os.ExpandEnv(usage), nil, version)
if err != nil {
panic(err)
Expand Down Expand Up @@ -127,7 +136,14 @@ func main() {

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

files, err := filepath.Glob(flags.FileGlobPatten)
var files []string
if len(flags.FilesList) != 0 {
files = flags.FilesList
err = nil
} else {
files, err = filepath.Glob(flags.FileGlobPatten)
}

if err != nil {
log.Fatal(err)
}
Expand All @@ -141,14 +157,16 @@ func main() {
}

// Loop through files matched by glob pattern
var fails []string
for _, file := range files {
log.Infof(
nil,
"processing %s",
file,
)

target := processFile(file, api, flags, creds.PageID, creds.Username)
target, relative_falis := processFile(file, api, flags, creds.PageID, creds.Username)
fails = append(fails, relative_falis...)

log.Infof(
nil,
Expand All @@ -158,6 +176,27 @@ func main() {

fmt.Println(creds.BaseURL + target.Links.Full)
}

// Try again with relative resolved fails
if len(fails) > 0 {
for _, file := range fails {
log.Infof(
nil,
"try again to resolve relative links of %s",
file,
)
target, _ := processFile(file, api, flags, creds.PageID, creds.Username)

log.Infof(
nil,
"done retry relative resolve for: %s",
creds.BaseURL+target.Links.Full,
)

fmt.Println(creds.BaseURL + target.Links.Full)

}
}
}

func processFile(
Expand All @@ -166,7 +205,8 @@ func processFile(
flags Flags,
pageID string,
username string,
) *confluence.PageInfo {
) (*confluence.PageInfo, []string) {
var rfails []string
markdown, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -258,9 +298,11 @@ func processFile(
}
}

links, err := mark.ResolveRelativeLinks(api, meta, markdown, ".")
base, _ := filepath.Split(file)
links, err := mark.ResolveRelativeLinks(api, meta, markdown, base)
if err != nil {
log.Fatalf(err, "unable to resolve relative links")
log.Warningf(err, "unable to resolve relative links in %s", file)
rfails = append(rfails, file)
}

markdown = mark.SubstituteLinks(markdown, links)
Expand Down Expand Up @@ -390,5 +432,5 @@ func processFile(
}
}

return target
return target, rfails
}
21 changes: 13 additions & 8 deletions pkg/mark/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"bytes"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"regexp"
"errors"

"github.com/kovetskiy/mark/pkg/confluence"
"github.com/reconquest/karma-go"
Expand All @@ -34,6 +34,7 @@ func ResolveRelativeLinks(
matches := parseLinks(string(markdown))

links := []LinkSubstitution{}
var partial_fail error = nil
for _, match := range matches {
log.Tracef(
nil,
Expand All @@ -49,6 +50,7 @@ func ResolveRelativeLinks(
}

if resolved == "" {
partial_fail = errors.New("resolved relative link partial fails")
continue
}

Expand All @@ -58,7 +60,7 @@ func ResolveRelativeLinks(
})
}

return links, nil
return links, partial_fail
}

func resolveLink(
Expand Down Expand Up @@ -171,12 +173,13 @@ func getConfluenceLink(
api *confluence.API,
space, title string,
) (string, error) {
link := fmt.Sprintf(
"%s/display/%s/%s",
api.BaseURL,
space,
url.QueryEscape(title),
)
// link := fmt.Sprintf(
// "%s/display/%s/%s",
// api.BaseURL,
// space,
// url.QueryEscape(title),
// )
var link string

page, err := api.FindPage(space, title, "page")
if err != nil {
Expand All @@ -187,6 +190,8 @@ func getConfluenceLink(
// Needs baseURL, as REST api response URL doesn't contain subpath ir
// confluence is server from that
link = api.BaseURL + page.Links.Full
} else {
link = ""
}

return link, nil
Expand Down