Skip to content

Commit

Permalink
fix: output log of registry update
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Oct 10, 2023
1 parent a922c77 commit d88341b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
65 changes: 46 additions & 19 deletions pkg/controller/update/ast/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"errors"

"github.com/goccy/go-yaml/ast"
"github.com/sirupsen/logrus"
)

const typeStandard = "standard"

func findMappingValue(values []*ast.MappingValueNode, key string) *ast.MappingValueNode {
for _, value := range values {
if value.Key.String() == key {
Expand Down Expand Up @@ -33,40 +36,54 @@ func findMappingValueFromNode(body ast.Node, key string) (*ast.MappingValueNode,
return findMappingValue(values, key), nil
}

func parseRegistryNode(node ast.Node, newVersions map[string]string) error { //nolint:gocognit,cyclop
func updateRegistryVersion(logE *logrus.Entry, refNode *ast.StringNode, rgstName, newVersion string) {
if refNode.Value == newVersion {
return
}
logE.WithFields(logrus.Fields{
"old_version": refNode.Value,
"new_version": newVersion,
"registry_name": rgstName,
}).Info("updating a registry")
refNode.Value = newVersion
}

func parseRegistryNode(logE *logrus.Entry, node ast.Node, newVersions map[string]string) error { //nolint:gocognit,cyclop,funlen
mvs, err := normalizeMappingValueNodes(node)
if err != nil {
return err
}
var refNode *ast.StringNode
var newVersion string
var rgstName string
for _, mvn := range mvs {
switch mvn.Key.String() {
case "ref":
sn, ok := mvn.Value.(*ast.StringNode)
if !ok {
return errors.New("ref must be a string")
}
if newVersion != "" {
sn.Value = newVersion
return nil
if newVersion == "" {
refNode = sn
continue
}
refNode = sn
updateRegistryVersion(logE, sn, rgstName, newVersion)
return nil
case "type":
sn, ok := mvn.Value.(*ast.StringNode)
if !ok {
return errors.New("type must be a string")
}
if sn.Value == "standard" {
version, ok := newVersions["standard"]
if !ok {
return nil
}
newVersion = version
}
if sn.Value != "standard" && sn.Value != "github_content" {
if sn.Value != typeStandard && sn.Value != "github_content" {
break
}
if sn.Value != typeStandard {
continue
}
if rgstName == "" {
rgstName = typeStandard
}
continue
case "name":
sn, ok := mvn.Value.(*ast.StringNode)
if !ok {
Expand All @@ -76,19 +93,29 @@ func parseRegistryNode(node ast.Node, newVersions map[string]string) error { //n
if !ok {
return nil
}
if refNode != nil {
refNode.Value = version
return nil
if refNode == nil {
rgstName = sn.Value
newVersion = version
continue
}
newVersion = version
updateRegistryVersion(logE, refNode, sn.Value, version)
return nil
default:
continue // Ignore unknown fields
}
}
if refNode == nil || rgstName == "" {
return nil
}
version, ok := newVersions[rgstName]
if !ok {
return nil
}
updateRegistryVersion(logE, refNode, rgstName, version)
return nil
}

func UpdateRegistries(file *ast.File, newVersions map[string]string) error {
func UpdateRegistries(logE *logrus.Entry, file *ast.File, newVersions map[string]string) error {
body := file.Docs[0].Body // DocumentNode
mv, err := findMappingValueFromNode(body, "registries")
if err != nil {
Expand All @@ -100,7 +127,7 @@ func UpdateRegistries(file *ast.File, newVersions map[string]string) error {
return errors.New("the value must be a sequence node")
}
for _, value := range seq.Values {
if err := parseRegistryNode(value, newVersions); err != nil {
if err := parseRegistryNode(logE, value, newVersions); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (c *Controller) update(ctx context.Context, logE *logrus.Entry, cfgFilePath
return fmt.Errorf("parse configuration file as YAML: %w", err)
}

if err := ast.UpdateRegistries(file, newVersions); err != nil {
if err := ast.UpdateRegistries(logE, file, newVersions); err != nil {
return fmt.Errorf("parse a configuration as YAML to update registries: %w", err)
}

Expand Down

0 comments on commit d88341b

Please sign in to comment.