mirror of
https://github.com/kovetskiy/mark.git
synced 2025-04-24 05:42:40 +08:00
remove options mode
This commit is contained in:
parent
9d91b6cb7a
commit
7fe4542c0a
@ -16,14 +16,6 @@ password = "matrixishere"
|
|||||||
base_url = "http://confluence.local"
|
base_url = "http://confluence.local"
|
||||||
```
|
```
|
||||||
|
|
||||||
Mark can read Confluence page URL and markdown file path from another specified
|
|
||||||
configuration file, which you can specify using -c <file> flag. It is very
|
|
||||||
usable for git hooks. That file should have following format:
|
|
||||||
```toml
|
|
||||||
url = "http://confluence.local/pages/viewpage.action?pageId=123456"
|
|
||||||
file = "docs/README.md"
|
|
||||||
```
|
|
||||||
|
|
||||||
Mark understands extended file format, which, still being valid markdown,
|
Mark understands extended file format, which, still being valid markdown,
|
||||||
contains several metadata headers, which can be used to locate page inside
|
contains several metadata headers, which can be used to locate page inside
|
||||||
Confluence instance and update it accordingly.
|
Confluence instance and update it accordingly.
|
||||||
|
146
main.go
146
main.go
@ -1,8 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -81,8 +79,6 @@ Options:
|
|||||||
-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 for converting to html.
|
||||||
-c <file> Specify configuration file which should be used for reading
|
|
||||||
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
|
||||||
manual edits over Confluence Web UI.
|
manual edits over Confluence Web UI.
|
||||||
--dry-run Show resulting HTML and don't update Confluence page content.
|
--dry-run Show resulting HTML and don't update Confluence page content.
|
||||||
@ -110,24 +106,27 @@ type PageInfo struct {
|
|||||||
} `json:"_links"`
|
} `json:"_links"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
HeaderParent string = `Parent`
|
|
||||||
HeaderSpace = `Space`
|
|
||||||
HeaderTitle = `Title`
|
|
||||||
HeaderLayout = `Layout`
|
|
||||||
)
|
|
||||||
|
|
||||||
type Meta struct {
|
|
||||||
Parents []string
|
|
||||||
Space string
|
|
||||||
Title string
|
|
||||||
Layout string
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logger = lorg.NewLog()
|
logger = lorg.NewLog()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func initLogger(trace bool) {
|
||||||
|
if trace {
|
||||||
|
logger.SetLevel(lorg.LevelTrace)
|
||||||
|
}
|
||||||
|
|
||||||
|
logFormat := `${time} ${level:[%s]:right:true} %s`
|
||||||
|
|
||||||
|
if format := os.Getenv("LOG_FORMAT"); format != "" {
|
||||||
|
logFormat = format
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.SetFormat(colorgful.MustApplyDefaultTheme(
|
||||||
|
logFormat,
|
||||||
|
colorgful.Default,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
args, err := godocs.Parse(usage, "mark 1.0", godocs.UsePager)
|
args, err := godocs.Parse(usage, "mark 1.0", godocs.UsePager)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -142,55 +141,15 @@ func main() {
|
|||||||
dryRun = args["--dry-run"].(bool)
|
dryRun = args["--dry-run"].(bool)
|
||||||
editLock = args["-k"].(bool)
|
editLock = args["-k"].(bool)
|
||||||
trace = args["--trace"].(bool)
|
trace = args["--trace"].(bool)
|
||||||
|
|
||||||
optionsFile, shouldReadOptions = args["-c"].(string)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if trace {
|
initLogger(trace)
|
||||||
logger.SetLevel(lorg.LevelTrace)
|
|
||||||
}
|
|
||||||
|
|
||||||
logFormat := `${time} ${level:[%s]:right:true} %s`
|
|
||||||
|
|
||||||
if format := os.Getenv("LOG_FORMAT"); format != "" {
|
|
||||||
logFormat = format
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.SetFormat(colorgful.MustApplyDefaultTheme(
|
|
||||||
logFormat,
|
|
||||||
colorgful.Default,
|
|
||||||
))
|
|
||||||
|
|
||||||
config, err := getConfig(filepath.Join(os.Getenv("HOME"), ".config/mark"))
|
config, err := getConfig(filepath.Join(os.Getenv("HOME"), ".config/mark"))
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if shouldReadOptions {
|
|
||||||
optionsConfig, err := getConfig(optionsFile)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatalf(
|
|
||||||
"can't read options config '%s': %s", optionsFile, err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
targetURL, err = optionsConfig.GetString("url")
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal(
|
|
||||||
"can't read `url` value from options file (%s): %s",
|
|
||||||
optionsFile, err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
targetFile, err = optionsConfig.GetString("file")
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal(
|
|
||||||
"can't read `file` value from options file (%s): %s",
|
|
||||||
optionsFile, err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
markdownData, err := ioutil.ReadFile(targetFile)
|
markdownData, err := ioutil.ReadFile(targetFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
@ -597,72 +556,3 @@ func getConfig(path string) (zhash.Hash, error) {
|
|||||||
|
|
||||||
return zhash.HashFromMap(configData), nil
|
return zhash.HashFromMap(configData), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractMeta(data []byte) (*Meta, error) {
|
|
||||||
headerPattern := regexp.MustCompile(`\[\]:\s*#\s*\(([^:]+):\s*(.*)\)`)
|
|
||||||
|
|
||||||
var meta *Meta
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(bytes.NewBuffer(data))
|
|
||||||
for scanner.Scan() {
|
|
||||||
line := scanner.Text()
|
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
matches := headerPattern.FindStringSubmatch(line)
|
|
||||||
if matches == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if meta == nil {
|
|
||||||
meta = &Meta{}
|
|
||||||
}
|
|
||||||
|
|
||||||
header := strings.Title(matches[1])
|
|
||||||
|
|
||||||
switch header {
|
|
||||||
case HeaderParent:
|
|
||||||
meta.Parents = append(meta.Parents, matches[2])
|
|
||||||
|
|
||||||
case HeaderSpace:
|
|
||||||
meta.Space = strings.ToUpper(matches[2])
|
|
||||||
|
|
||||||
case HeaderTitle:
|
|
||||||
meta.Title = strings.TrimSpace(matches[2])
|
|
||||||
|
|
||||||
case HeaderLayout:
|
|
||||||
meta.Layout = strings.TrimSpace(matches[2])
|
|
||||||
|
|
||||||
default:
|
|
||||||
logger.Errorf(
|
|
||||||
`encountered unknown header '%s' line: %#v`,
|
|
||||||
header,
|
|
||||||
line,
|
|
||||||
)
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if meta == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if meta.Space == "" {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"space key is not set (%s header is not set)",
|
|
||||||
HeaderSpace,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if meta.Title == "" {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"page title is not set (%s header is not set)",
|
|
||||||
HeaderTitle,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return meta, nil
|
|
||||||
}
|
|
||||||
|
92
meta.go
Normal file
92
meta.go
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
HeaderParent string = `Parent`
|
||||||
|
HeaderSpace = `Space`
|
||||||
|
HeaderTitle = `Title`
|
||||||
|
HeaderLayout = `Layout`
|
||||||
|
)
|
||||||
|
|
||||||
|
type Meta struct {
|
||||||
|
Parents []string
|
||||||
|
Space string
|
||||||
|
Title string
|
||||||
|
Layout string
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractMeta(data []byte) (*Meta, error) {
|
||||||
|
headerPattern := regexp.MustCompile(`\[\]:\s*#\s*\(([^:]+):\s*(.*)\)`)
|
||||||
|
|
||||||
|
var meta *Meta
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(bytes.NewBuffer(data))
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
matches := headerPattern.FindStringSubmatch(line)
|
||||||
|
if matches == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if meta == nil {
|
||||||
|
meta = &Meta{}
|
||||||
|
}
|
||||||
|
|
||||||
|
header := strings.Title(matches[1])
|
||||||
|
|
||||||
|
switch header {
|
||||||
|
case HeaderParent:
|
||||||
|
meta.Parents = append(meta.Parents, matches[2])
|
||||||
|
|
||||||
|
case HeaderSpace:
|
||||||
|
meta.Space = strings.ToUpper(matches[2])
|
||||||
|
|
||||||
|
case HeaderTitle:
|
||||||
|
meta.Title = strings.TrimSpace(matches[2])
|
||||||
|
|
||||||
|
case HeaderLayout:
|
||||||
|
meta.Layout = strings.TrimSpace(matches[2])
|
||||||
|
|
||||||
|
default:
|
||||||
|
logger.Errorf(
|
||||||
|
`encountered unknown header '%s' line: %#v`,
|
||||||
|
header,
|
||||||
|
line,
|
||||||
|
)
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if meta == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if meta.Space == "" {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"space key is not set (%s header is not set)",
|
||||||
|
HeaderSpace,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if meta.Title == "" {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"page title is not set (%s header is not set)",
|
||||||
|
HeaderTitle,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return meta, nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user