mirror of
https://github.com/kovetskiy/mark.git
synced 2025-04-24 05:42:40 +08:00
add flag for updating on change
This commit is contained in:
parent
c63201159d
commit
5accce3b17
@ -49,6 +49,7 @@ type PageInfo struct {
|
|||||||
|
|
||||||
Version struct {
|
Version struct {
|
||||||
Number int64 `json:"number"`
|
Number int64 `json:"number"`
|
||||||
|
Message string `json:"message"`
|
||||||
} `json:"version"`
|
} `json:"version"`
|
||||||
|
|
||||||
Ancestors []struct {
|
Ancestors []struct {
|
||||||
|
63
main.go
63
main.go
@ -2,9 +2,12 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -190,6 +193,12 @@ var flags = []cli.Flag{
|
|||||||
TakesFile: true,
|
TakesFile: true,
|
||||||
EnvVars: []string{"MARK_INCLUDE_PATH"},
|
EnvVars: []string{"MARK_INCLUDE_PATH"},
|
||||||
}),
|
}),
|
||||||
|
altsrc.NewBoolFlag(&cli.BoolFlag{
|
||||||
|
Name: "changes-only",
|
||||||
|
Value: false,
|
||||||
|
Usage: "Avoids re-uploading pages that haven't changed since the last run.",
|
||||||
|
EnvVars: []string{"CHANGES_ONLY"},
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -515,9 +524,51 @@ func processFile(
|
|||||||
html = buffer.String()
|
html = buffer.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
err = api.UpdatePage(target, html, cCtx.Bool("minor-edit"), cCtx.String("version-message"), meta.Labels, meta.ContentAppearance)
|
var finalVersionMessage string
|
||||||
if err != nil {
|
var shouldUpdatePage bool = true
|
||||||
log.Fatal(err)
|
|
||||||
|
if cCtx.Bool("changes-only") {
|
||||||
|
contentHash := getSHA1Hash(html)
|
||||||
|
|
||||||
|
log.Debugf(
|
||||||
|
nil,
|
||||||
|
"content hash: %s",
|
||||||
|
contentHash,
|
||||||
|
)
|
||||||
|
|
||||||
|
versionPattern := `\[v([a-f0-9]{40})]$`
|
||||||
|
re := regexp.MustCompile(versionPattern)
|
||||||
|
|
||||||
|
matches := re.FindStringSubmatch(target.Version.Message)
|
||||||
|
|
||||||
|
if len(matches) > 1 {
|
||||||
|
log.Debugf(
|
||||||
|
nil,
|
||||||
|
"previous content hash: %s",
|
||||||
|
matches[1],
|
||||||
|
)
|
||||||
|
|
||||||
|
if matches[1] == contentHash {
|
||||||
|
log.Infof(
|
||||||
|
nil,
|
||||||
|
"page %q is already up to date",
|
||||||
|
target.Title,
|
||||||
|
)
|
||||||
|
shouldUpdatePage = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
finalVersionMessage = fmt.Sprintf("%s [v%s]", cCtx.String("version-message"), contentHash)
|
||||||
|
} else {
|
||||||
|
finalVersionMessage = cCtx.String("version-message")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if shouldUpdatePage {
|
||||||
|
err = api.UpdatePage(target, html, cCtx.Bool("minor-edit"), finalVersionMessage, meta.Labels, meta.ContentAppearance)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
updateLabels(api, target, meta)
|
updateLabels(api, target, meta)
|
||||||
@ -630,3 +681,9 @@ func setLogLevel(cCtx *cli.Context) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getSHA1Hash(input string) string {
|
||||||
|
hash := sha1.New()
|
||||||
|
hash.Write([]byte(input))
|
||||||
|
return hex.EncodeToString(hash.Sum(nil))
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user