Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert-to-default-checksum-parser #380

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
13 changes: 7 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ go 1.19

require (
github.com/aquaproj/aqua v1.38.0
github.com/aquaproj/aqua/v2 v2.4.0
github.com/goccy/go-yaml v1.11.0
github.com/mattn/go-colorable v0.1.13
github.com/sirupsen/logrus v1.9.0
github.com/suzuki-shunsuke/logrus-error v0.1.4
github.com/urfave/cli/v2 v2.25.1
github.com/urfave/cli/v2 v2.25.3
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -57,11 +58,11 @@ require (
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
24 changes: 14 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ github.com/antonmedv/expr v1.12.5 h1:Fq4okale9swwL3OeLLs9WD9H6GbgBLJyN/NUHRv+n0E
github.com/antonmedv/expr v1.12.5/go.mod h1:FPC8iWArxls7axbVLsW+kpg1mz29A1b2M6jt+hZfDkU=
github.com/aquaproj/aqua v1.38.0 h1:mAU1Fj/HVMI1U30ofOijYeNRdGJxxo10Q4hdK+MiNQk=
github.com/aquaproj/aqua v1.38.0/go.mod h1:XXVugIyzQrfLiAPStVLFUPLWkF54LELTj1dIH1zwZZk=
github.com/aquaproj/aqua/v2 v2.4.0 h1:VfB8kszbIxbrjNDJMTa4otiexKJGY9SLkpidnpbKt5A=
github.com/aquaproj/aqua/v2 v2.4.0/go.mod h1:Yx6BHPCMuBJHHlIU3gus+miB6JiX/xsWadkhQzA/7nE=
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
Expand Down Expand Up @@ -262,8 +264,8 @@ github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oW
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/urfave/cli/v2 v2.25.1 h1:zw8dSP7ghX0Gmm8vugrs6q9Ku0wzweqPyshy+syu9Gw=
github.com/urfave/cli/v2 v2.25.1/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/urfave/cli/v2 v2.25.3 h1:VJkt6wvEBOoSjPFQvOkv6iWIrsJyCrKGtCtxXWwmGeY=
github.com/urfave/cli/v2 v2.25.3/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
Expand Down Expand Up @@ -358,8 +360,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand All @@ -369,8 +371,8 @@ golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.6.0 h1:Lh8GPgSKBfWSwFvtuWOfeI3aAAnbXTSutYxJiOJFgIw=
golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw=
golang.org/x/oauth2 v0.7.0 h1:qe6s0zUXlPX80/dITx3440hWZ7GwMwgDDyrSGTPJG/g=
golang.org/x/oauth2 v0.7.0/go.mod h1:hPLQkd9LyjfXTiRohC/41GhcFqxisoUQ99sCUOHO9x4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -426,13 +428,15 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -442,8 +446,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
25 changes: 25 additions & 0 deletions pkg/cli/conv_to_default_checksum_parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cli

import (
"github.com/aquaproj/registry-tool/pkg/convtodefaultchecksumparser"
"github.com/urfave/cli/v2"
)

func (runner *Runner) newConvToDefaultChecksumParserCommand() *cli.Command {
return &cli.Command{
Name: "conv-to-default-checksum-parser",
Usage: `Convert the checksum parser to the default checksum parser`,
UsageText: `$ aqua-registry conv-to-default-checksum-parser <registry file path>`,
Description: `Convert the checksum parser to the default checksum parser

e.g.

$ aqua-registry conv-to-default-checksum-parser cli/cli
`,
Action: runner.convToDefaultChecksumParserAction,
}
}

func (runner *Runner) convToDefaultChecksumParserAction(c *cli.Context) error {
return convtodefaultchecksumparser.Convert(c.Args().Slice()...) //nolint:wrapcheck
}
1 change: 1 addition & 0 deletions pkg/cli/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (runner *Runner) Run(ctx context.Context, args ...string) error {
runner.newVersionCommand(),
runner.newInitCommand(),
runner.newPatchChecksumCommand(),
runner.newConvToDefaultChecksumParserCommand(),
},
}

Expand Down
221 changes: 221 additions & 0 deletions pkg/convtodefaultchecksumparser/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package convtodefaultchecksumparser

import (
"errors"
"fmt"
"os"

"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/parser"
"github.com/sirupsen/logrus"
"github.com/suzuki-shunsuke/logrus-error/logerr"
"gopkg.in/yaml.v2"
)

func Convert(filePaths ...string) error {
for _, filePath := range filePaths {
if err := convert(filePath); err != nil {
return logerr.WithFields(err, logrus.Fields{ //nolint:wrapcheck
"file_path": filePath,
})
}
}
return nil
}

func convert(filePath string) error { //nolint:cyclop
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci-lint] reported by reviewdog 🐶
Function 'convert' is too long (64 > 60)

cfg := &registry.Config{}
b, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("open a configuration file %s: %w", filePath, err)
}

if err := yaml.Unmarshal(b, cfg); err != nil {
return fmt.Errorf("parse a configuration file %s as YAML: %w", filePath, err)
}

file, err := parser.ParseBytes(b, parser.ParseComments)
if err != nil {
return fmt.Errorf("parse configuration file as YAML: %w", err)
}

visitor := &Visitor{}
ast.Walk(visitor, file.Docs[0])

// changed := false

// // if f, err := removeWithPath(file); err != nil {
// // return err
// // } else if f {
// // changed = true
// // }

// pkgsAST, err := patchchecksum.GetPackagesAST(file)
// if err != nil {
// return err //nolint:wrapcheck
// }

// for i, pkgInfo := range cfg.PackageInfos {
// pkgInfoNode, j := patchchecksum.FindFirstMappingNode(pkgsAST, i)
// if j == -1 {
// return nil
// }

// if f, err := convChecksumParserOfVersionOverrides(pkgInfoNode, pkgInfo); err != nil {
// return fmt.Errorf("convert to the default checksum parser: %w", err)
// } else if f {
// changed = true
// }

// if pkgInfo.Checksum != nil && pkgInfo.Checksum.FileFormat != "" {
// if f, err := removeFromChecksumParent(pkgInfoNode); err != nil {
// return fmt.Errorf("convert to the default checksum parser: %w", err)
// } else if f {
// changed = true
// }
// }
// }

// if !changed {
// return nil
// }

if !visitor.changed {
return nil
}

if err := os.WriteFile(filePath, []byte(file.String()), 0o644); err != nil { //nolint:gosec,gomnd
return fmt.Errorf("write the configuration file: %w", err)
}
return nil
}

// func removeWithPath(file *ast.File) (bool, error) {
// ypath, err := goccyYAML.PathString("$.packages[*].version_constraints[*].checksum")
// if err != nil {
// return false, fmt.Errorf("create a yaml path from string: %w", err)
// }
// node, err := ypath.FilterFile(file)
// if err != nil {
// return false, fmt.Errorf("filter a file with yaml path: %w", err)
// }
// return removeFromChecksum(node)
// }

func getMappingValueNode(mappingNode *ast.MappingNode, key string) *ast.MappingValueNode {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci-lint] reported by reviewdog 🐶
func getMappingValueNode is unused

for _, mvNode := range mappingNode.Values {
if mvNode.Key.String() == key {
return mvNode
}
}
return nil
}

// func removeFromChecksum(node ast.Node) (bool, error) {
// checksumNode, ok := node.(*ast.MappingNode)
// if !ok {
// return false, logerr.WithFields(errors.New("checksum must be *ast.MappingNode"), logrus.Fields{
// "node_path": node.GetPath(),
// })
// }
// values := make([]*ast.MappingValueNode, 0, len(checksumNode.Values))
// for _, mvNode := range checksumNode.Values {
// switch mvNode.Key.String() {
// case "file_format", "pattern":
// continue
// }
// values = append(values, mvNode)
// }
// if len(checksumNode.Values) == len(values) {
// return false, nil
// }
// checksumNode.Values = values
// return true, nil
// }

func removeFromChecksumParent(parentNode *ast.MappingNode) (bool, error) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci-lint] reported by reviewdog 🐶
removeFromChecksumParent - result 1 (error) is always nil

mvNode := getMappingValueNode(parentNode, "checksum")
if mvNode == nil {
return false, nil
}

switch t := mvNode.Value.(type) {
case *ast.MappingNode:
values := make([]*ast.MappingValueNode, 0, len(t.Values))
for _, mvNode := range t.Values {
switch mvNode.Key.String() {
case "file_format", "pattern":
continue
}
values = append(values, mvNode)
}
if len(t.Values) == len(values) {
return false, nil
}
t.Values = values
return true, nil
case *ast.MappingValueNode:
return false, nil
}
return false, nil
}

func convChecksumParserOfVersionOverrides(pkgInfoNode *ast.MappingNode, pkgInfo *registry.PackageInfo) (bool, error) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci-lint] reported by reviewdog 🐶
func convChecksumParserOfVersionOverrides is unused

if pkgInfo.VersionOverrides == nil {
return false, nil
}

mvNode := getMappingValueNode(pkgInfoNode, "version_overrides")
if mvNode == nil {
return false, nil
}

seq, ok := mvNode.Value.(*ast.SequenceNode)
if !ok {
return false, errors.New("version_overrides must be *ast.SequenceNode")
}
changed := false
for _, val := range seq.Values {
val := val
m, ok := val.(*ast.MappingNode)
if !ok {
continue
}
if f, err := removeFromChecksumParent(m); err != nil {
return false, err
} else if f {
changed = true
}
}
return changed, nil
}

type Visitor struct {
changed bool
}

func (visitor *Visitor) Visit(node ast.Node) ast.Visitor {
a, ok := node.(*ast.MappingValueNode)
if !ok {
return visitor
}
t, ok := a.Value.(*ast.MappingNode)
if !ok {
return visitor
}
values := make([]*ast.MappingValueNode, 0, len(t.Values))
for _, mvNode := range t.Values {
switch mvNode.Key.String() {
case "file_format", "pattern":
continue
}
values = append(values, mvNode)
}
if len(t.Values) == len(values) {
return visitor
}
t.Values = values
visitor.changed = true
return visitor
}