mirror of
https://github.com/kovetskiy/mark.git
synced 2026-05-03 14:47:38 +08:00
refactor: modernize Go primitives
- Replace interface{} with any (Go 1.18) across confluence/api.go,
macro/macro.go, util/cli.go, util/error_handler.go, includes/templates.go
- Replace sort.SliceStable with slices.SortStableFunc + cmp.Compare (Go 1.21)
in attachment/attachment.go, consistent with existing slices usage
- Replace fmt.Errorf("%s", msg) with errors.New(msg) in mark.go
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
4a5f3798eb
commit
a43f5fec2e
@ -13,7 +13,8 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"cmp"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -235,8 +236,8 @@ func CompileAttachmentLinks(markdown []byte, attachments []Attachment) []byte {
|
|||||||
// attachments/a.jpg
|
// attachments/a.jpg
|
||||||
// attachments/a.jpg.jpg
|
// attachments/a.jpg.jpg
|
||||||
// so we replace longer and then shorter
|
// so we replace longer and then shorter
|
||||||
sort.SliceStable(replaces, func(i, j int) bool {
|
slices.SortStableFunc(replaces, func(a, b string) int {
|
||||||
return len(replaces[i]) > len(replaces[j])
|
return cmp.Compare(len(b), len(a))
|
||||||
})
|
})
|
||||||
|
|
||||||
for _, replace := range replaces {
|
for _, replace := range replaces {
|
||||||
|
|||||||
@ -94,7 +94,7 @@ type tracer struct {
|
|||||||
prefix string
|
prefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tracer *tracer) Printf(format string, args ...interface{}) {
|
func (tracer *tracer) Printf(format string, args ...any) {
|
||||||
log.Trace().Msgf(tracer.prefix+" "+format, args...)
|
log.Trace().Msgf(tracer.prefix+" "+format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -485,21 +485,21 @@ func (api *API) CreatePage(
|
|||||||
title string,
|
title string,
|
||||||
body string,
|
body string,
|
||||||
) (*PageInfo, error) {
|
) (*PageInfo, error) {
|
||||||
payload := map[string]interface{}{
|
payload := map[string]any{
|
||||||
"type": pageType,
|
"type": pageType,
|
||||||
"title": title,
|
"title": title,
|
||||||
"space": map[string]interface{}{
|
"space": map[string]any{
|
||||||
"key": space,
|
"key": space,
|
||||||
},
|
},
|
||||||
"body": map[string]interface{}{
|
"body": map[string]any{
|
||||||
"storage": map[string]interface{}{
|
"storage": map[string]any{
|
||||||
"representation": "storage",
|
"representation": "storage",
|
||||||
"value": body,
|
"value": body,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"metadata": map[string]interface{}{
|
"metadata": map[string]any{
|
||||||
"properties": map[string]interface{}{
|
"properties": map[string]any{
|
||||||
"editor": map[string]interface{}{
|
"editor": map[string]any{
|
||||||
"value": "v2",
|
"value": "v2",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -507,7 +507,7 @@ func (api *API) CreatePage(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if parent != nil {
|
if parent != nil {
|
||||||
payload["ancestors"] = []map[string]interface{}{
|
payload["ancestors"] = []map[string]any{
|
||||||
{"id": parent.ID},
|
{"id": parent.ID},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -528,20 +528,20 @@ func (api *API) CreatePage(
|
|||||||
|
|
||||||
func (api *API) UpdatePage(page *PageInfo, newContent string, minorEdit bool, versionMessage string, appearance string, emojiString string) error {
|
func (api *API) UpdatePage(page *PageInfo, newContent string, minorEdit bool, versionMessage string, appearance string, emojiString string) error {
|
||||||
nextPageVersion := page.Version.Number + 1
|
nextPageVersion := page.Version.Number + 1
|
||||||
oldAncestors := []map[string]interface{}{}
|
oldAncestors := []map[string]any{}
|
||||||
|
|
||||||
if page.Type != "blogpost" && len(page.Ancestors) > 0 {
|
if page.Type != "blogpost" && len(page.Ancestors) > 0 {
|
||||||
// picking only the last one, which is required by confluence
|
// picking only the last one, which is required by confluence
|
||||||
oldAncestors = []map[string]interface{}{
|
oldAncestors = []map[string]any{
|
||||||
{"id": page.Ancestors[len(page.Ancestors)-1].ID},
|
{"id": page.Ancestors[len(page.Ancestors)-1].ID},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
properties := map[string]interface{}{
|
properties := map[string]any{
|
||||||
// Fix to set full-width as has changed on Confluence APIs again.
|
// Fix to set full-width as has changed on Confluence APIs again.
|
||||||
// https://jira.atlassian.com/browse/CONFCLOUD-65447
|
// https://jira.atlassian.com/browse/CONFCLOUD-65447
|
||||||
//
|
//
|
||||||
"content-appearance-published": map[string]interface{}{
|
"content-appearance-published": map[string]any{
|
||||||
"value": appearance,
|
"value": appearance,
|
||||||
},
|
},
|
||||||
// content-appearance-draft should not be set as this is impacted by
|
// content-appearance-draft should not be set as this is impacted by
|
||||||
@ -555,37 +555,37 @@ func (api *API) UpdatePage(page *PageInfo, newContent string, minorEdit bool, ve
|
|||||||
}
|
}
|
||||||
unicodeHex := fmt.Sprintf("%x", r)
|
unicodeHex := fmt.Sprintf("%x", r)
|
||||||
|
|
||||||
properties["emoji-title-draft"] = map[string]interface{}{
|
properties["emoji-title-draft"] = map[string]any{
|
||||||
"value": unicodeHex,
|
"value": unicodeHex,
|
||||||
}
|
}
|
||||||
properties["emoji-title-published"] = map[string]interface{}{
|
properties["emoji-title-published"] = map[string]any{
|
||||||
"value": unicodeHex,
|
"value": unicodeHex,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
payload := map[string]interface{}{
|
payload := map[string]any{
|
||||||
"id": page.ID,
|
"id": page.ID,
|
||||||
"type": page.Type,
|
"type": page.Type,
|
||||||
"title": page.Title,
|
"title": page.Title,
|
||||||
"version": map[string]interface{}{
|
"version": map[string]any{
|
||||||
"number": nextPageVersion,
|
"number": nextPageVersion,
|
||||||
"minorEdit": minorEdit,
|
"minorEdit": minorEdit,
|
||||||
"message": versionMessage,
|
"message": versionMessage,
|
||||||
},
|
},
|
||||||
"ancestors": oldAncestors,
|
"ancestors": oldAncestors,
|
||||||
"body": map[string]interface{}{
|
"body": map[string]any{
|
||||||
"storage": map[string]interface{}{
|
"storage": map[string]any{
|
||||||
"value": newContent,
|
"value": newContent,
|
||||||
"representation": "storage",
|
"representation": "storage",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"metadata": map[string]interface{}{
|
"metadata": map[string]any{
|
||||||
"properties": properties,
|
"properties": properties,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
request, err := api.rest.Res(
|
request, err := api.rest.Res(
|
||||||
"content/"+page.ID, &map[string]interface{}{},
|
"content/"+page.ID, &map[string]any{},
|
||||||
).Put(payload)
|
).Put(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -600,10 +600,10 @@ func (api *API) UpdatePage(page *PageInfo, newContent string, minorEdit bool, ve
|
|||||||
|
|
||||||
func (api *API) AddPageLabels(page *PageInfo, newLabels []string) (*LabelInfo, error) {
|
func (api *API) AddPageLabels(page *PageInfo, newLabels []string) (*LabelInfo, error) {
|
||||||
|
|
||||||
labels := []map[string]interface{}{}
|
labels := []map[string]any{}
|
||||||
for _, label := range newLabels {
|
for _, label := range newLabels {
|
||||||
if label != "" {
|
if label != "" {
|
||||||
item := map[string]interface{}{
|
item := map[string]any{
|
||||||
"prefix": "global",
|
"prefix": "global",
|
||||||
"name": label,
|
"name": label,
|
||||||
}
|
}
|
||||||
@ -765,17 +765,17 @@ func (api *API) RestrictPageUpdatesCloud(
|
|||||||
user = currentUser
|
user = currentUser
|
||||||
}
|
}
|
||||||
|
|
||||||
var result interface{}
|
var result any
|
||||||
|
|
||||||
request, err := api.rest.
|
request, err := api.rest.
|
||||||
Res("content").
|
Res("content").
|
||||||
Id(page.ID).
|
Id(page.ID).
|
||||||
Res("restriction", &result).
|
Res("restriction", &result).
|
||||||
Post([]map[string]interface{}{
|
Post([]map[string]any{
|
||||||
{
|
{
|
||||||
"operation": "update",
|
"operation": "update",
|
||||||
"restrictions": map[string]interface{}{
|
"restrictions": map[string]any{
|
||||||
"user": []map[string]interface{}{
|
"user": []map[string]any{
|
||||||
{
|
{
|
||||||
"type": "known",
|
"type": "known",
|
||||||
"accountId": user.AccountID,
|
"accountId": user.AccountID,
|
||||||
@ -801,15 +801,15 @@ func (api *API) RestrictPageUpdatesServer(
|
|||||||
) error {
|
) error {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
result interface{}
|
result any
|
||||||
)
|
)
|
||||||
|
|
||||||
request, err := api.json.Res(
|
request, err := api.json.Res(
|
||||||
"setContentPermissions", &result,
|
"setContentPermissions", &result,
|
||||||
).Post([]interface{}{
|
).Post([]any{
|
||||||
page.ID,
|
page.ID,
|
||||||
"Edit",
|
"Edit",
|
||||||
[]map[string]interface{}{
|
[]map[string]any{
|
||||||
{
|
{
|
||||||
"userName": allowedUser,
|
"userName": allowedUser,
|
||||||
},
|
},
|
||||||
|
|||||||
@ -75,7 +75,7 @@ func ProcessIncludes(
|
|||||||
templates *template.Template,
|
templates *template.Template,
|
||||||
) (*template.Template, []byte, bool, error) {
|
) (*template.Template, []byte, bool, error) {
|
||||||
formatVardump := func(
|
formatVardump := func(
|
||||||
data map[string]interface{},
|
data map[string]any,
|
||||||
) string {
|
) string {
|
||||||
var parts []string
|
var parts []string
|
||||||
for key, value := range data {
|
for key, value := range data {
|
||||||
@ -105,7 +105,7 @@ func ProcessIncludes(
|
|||||||
left = string(groups[3])
|
left = string(groups[3])
|
||||||
right = string(groups[4])
|
right = string(groups[4])
|
||||||
config = groups[5]
|
config = groups[5]
|
||||||
data = map[string]interface{}{}
|
data = map[string]any{}
|
||||||
)
|
)
|
||||||
|
|
||||||
if delimsNone == "none" {
|
if delimsNone == "none" {
|
||||||
|
|||||||
@ -37,7 +37,7 @@ func (macro *Macro) Apply(
|
|||||||
content = macro.Regexp.ReplaceAllFunc(
|
content = macro.Regexp.ReplaceAllFunc(
|
||||||
content,
|
content,
|
||||||
func(match []byte) []byte {
|
func(match []byte) []byte {
|
||||||
config := map[string]interface{}{}
|
config := map[string]any{}
|
||||||
|
|
||||||
err = yaml.Unmarshal([]byte(macro.Config), &config)
|
err = yaml.Unmarshal([]byte(macro.Config), &config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -63,21 +63,21 @@ func (macro *Macro) Apply(
|
|||||||
return content, err
|
return content, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (macro *Macro) configure(node interface{}, groups [][]byte) interface{} {
|
func (macro *Macro) configure(node any, groups [][]byte) any {
|
||||||
switch node := node.(type) {
|
switch node := node.(type) {
|
||||||
case map[interface{}]interface{}:
|
case map[any]any:
|
||||||
for key, value := range node {
|
for key, value := range node {
|
||||||
node[key] = macro.configure(value, groups)
|
node[key] = macro.configure(value, groups)
|
||||||
}
|
}
|
||||||
|
|
||||||
return node
|
return node
|
||||||
case map[string]interface{}:
|
case map[string]any:
|
||||||
for key, value := range node {
|
for key, value := range node {
|
||||||
node[key] = macro.configure(value, groups)
|
node[key] = macro.configure(value, groups)
|
||||||
}
|
}
|
||||||
|
|
||||||
return node
|
return node
|
||||||
case []interface{}:
|
case []any:
|
||||||
for key, value := range node {
|
for key, value := range node {
|
||||||
node[key] = macro.configure(value, groups)
|
node[key] = macro.configure(value, groups)
|
||||||
}
|
}
|
||||||
@ -126,7 +126,7 @@ func ExtractMacros(
|
|||||||
var macro Macro
|
var macro Macro
|
||||||
|
|
||||||
if strings.HasPrefix(template, "#") {
|
if strings.HasPrefix(template, "#") {
|
||||||
cfg := map[string]interface{}{}
|
cfg := map[string]any{}
|
||||||
|
|
||||||
err = yaml.Unmarshal([]byte(config), &cfg)
|
err = yaml.Unmarshal([]byte(config), &cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -170,7 +170,7 @@ func ExtractMacros(
|
|||||||
macro.Config = config
|
macro.Config = config
|
||||||
|
|
||||||
log.Trace().
|
log.Trace().
|
||||||
Interface("vardump", map[string]interface{}{
|
Interface("vardump", map[string]any{
|
||||||
"expr": expr,
|
"expr": expr,
|
||||||
"template": template,
|
"template": template,
|
||||||
"config": macro.Config,
|
"config": macro.Config,
|
||||||
|
|||||||
3
mark.go
3
mark.go
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@ -97,7 +98,7 @@ func Run(config Config) error {
|
|||||||
if config.CI {
|
if config.CI {
|
||||||
log.Warn().Msg(msg)
|
log.Warn().Msg(msg)
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("%s", msg)
|
return errors.New(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
10
util/cli.go
10
util/cli.go
@ -23,7 +23,7 @@ func RunMark(ctx context.Context, cmd *cli.Command) error {
|
|||||||
output := zerolog.ConsoleWriter{
|
output := zerolog.ConsoleWriter{
|
||||||
Out: os.Stderr,
|
Out: os.Stderr,
|
||||||
TimeFormat: "2006-01-02 15:04:05.000",
|
TimeFormat: "2006-01-02 15:04:05.000",
|
||||||
FormatLevel: func(i interface{}) string {
|
FormatLevel: func(i any) string {
|
||||||
var l string
|
var l string
|
||||||
if ll, ok := i.(string); ok {
|
if ll, ok := i.(string); ok {
|
||||||
switch ll {
|
switch ll {
|
||||||
@ -49,16 +49,16 @@ func RunMark(ctx context.Context, cmd *cli.Command) error {
|
|||||||
}
|
}
|
||||||
return l
|
return l
|
||||||
},
|
},
|
||||||
FormatFieldName: func(i interface{}) string {
|
FormatFieldName: func(i any) string {
|
||||||
return ""
|
return ""
|
||||||
},
|
},
|
||||||
FormatFieldValue: func(i interface{}) string {
|
FormatFieldValue: func(i any) string {
|
||||||
return fmt.Sprintf("%s", i)
|
return fmt.Sprintf("%s", i)
|
||||||
},
|
},
|
||||||
FormatErrFieldName: func(i interface{}) string {
|
FormatErrFieldName: func(i any) string {
|
||||||
return ""
|
return ""
|
||||||
},
|
},
|
||||||
FormatErrFieldValue: func(i interface{}) string {
|
FormatErrFieldValue: func(i any) string {
|
||||||
return fmt.Sprintf("%s", i)
|
return fmt.Sprintf("%s", i)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,7 +14,7 @@ func NewErrorHandler(continueOnError bool) *FatalErrorHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *FatalErrorHandler) Handle(err error, format string, args ...interface{}) {
|
func (h *FatalErrorHandler) Handle(err error, format string, args ...any) {
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if h.ContinueOnError {
|
if h.ContinueOnError {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user