diff --git a/action.yml b/action.yml index cda5f6d..6d5de17 100644 --- a/action.yml +++ b/action.yml @@ -23,6 +23,10 @@ inputs: description: no output, only validation required: false + verbose: + description: verbose logging + required: false + loose: description: skip JSON schema validation required: false diff --git a/cmd/cmd.go b/cmd/cmd.go index b2ef102..831851e 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -6,6 +6,7 @@ import ( _ "embed" "encoding/json" "io/fs" + "log/slog" "os" "path/filepath" @@ -22,13 +23,14 @@ type options struct { compact bool catalog bool quiet bool + verbose bool loose bool lint bool api string } // New creates new cobra command for exec command. -func New() (*cobra.Command, error) { +func New(levelVar *slog.LevelVar) (*cobra.Command, error) { opts := new(options) legacy := false @@ -43,6 +45,10 @@ func New() (*cobra.Command, error) { Args: cobra.MaximumNArgs(1), CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true}, RunE: func(cmd *cobra.Command, args []string) error { + if opts.verbose && levelVar != nil { + levelVar.Set(slog.LevelDebug) + } + if legacy { return legacyConvert(cmd.Context()) } @@ -72,6 +78,7 @@ func New() (*cobra.Command, error) { flags.BoolVar(&opts.lint, "lint", false, "enable built-in linter") flags.BoolVarP(&opts.compact, "compact", "c", false, "compact instead of pretty-printed output") flags.BoolVar(&opts.catalog, "catalog", false, "generate catalog instead of registry") + flags.BoolVarP(&opts.verbose, "verbose", "v", false, "verbose logging") root.MarkFlagsMutuallyExclusive("compact", "quiet") flags.BoolP("version", "V", false, "print version") diff --git a/cmd/k6registry/action.go b/cmd/k6registry/action.go index 39a45ff..3f258eb 100644 --- a/cmd/k6registry/action.go +++ b/cmd/k6registry/action.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "io" + "log/slog" "net/http" "os" "path/filepath" @@ -33,7 +34,11 @@ func emitOutput() error { return err } - _, err = fmt.Fprintf(file, "changed=%t\n", isChanged(ref, out)) + changed := isChanged(ref, out) + + slog.Debug("Detect change", "changed", changed, "ref", ref) + + _, err = fmt.Fprintf(file, "changed=%t\n", changed) if err != nil { return err } diff --git a/cmd/k6registry/main.go b/cmd/k6registry/main.go index 32b5b52..97780be 100644 --- a/cmd/k6registry/main.go +++ b/cmd/k6registry/main.go @@ -3,23 +3,38 @@ package main import ( "log" + "log/slog" "os" "github.com/grafana/k6registry/cmd" + sloglogrus "github.com/samber/slog-logrus/v2" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) var version = "dev" +func initLogging() *slog.LevelVar { + levelVar := new(slog.LevelVar) + + logrus.SetLevel(logrus.DebugLevel) + + logger := slog.New(sloglogrus.Option{Level: levelVar}.NewLogrusHandler()) + + slog.SetDefault(logger) + + return levelVar +} + func main() { log.SetFlags(0) log.Writer() - runCmd(newCmd(getArgs())) + runCmd(newCmd(getArgs(), initLogging())) } -func newCmd(args []string) *cobra.Command { - cmd, err := cmd.New() +func newCmd(args []string, levelVar *slog.LevelVar) *cobra.Command { + cmd, err := cmd.New(levelVar) if err != nil { log.Fatal(formatError(err)) } @@ -59,6 +74,10 @@ func getArgs() []string { args = append(args, "--quiet") } + if getenv("INPUT_VERBOSE", "false") == "true" { + args = append(args, "--verbose") + } + if getenv("INPUT_LOOSE", "false") == "true" { args = append(args, "--loose") } diff --git a/cmd/lint.go b/cmd/lint.go index 45c9830..6dd252f 100644 --- a/cmd/lint.go +++ b/cmd/lint.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "io/fs" + "log/slog" "os" "path/filepath" @@ -73,6 +74,8 @@ func updateWorkdir(ctx context.Context, dir string, cloneURL string) error { } if notfound { + slog.Debug("Clone", "url", cloneURL) + _, err = git.PlainCloneContext(ctx, dir, false, &git.CloneOptions{URL: cloneURL}) return err } @@ -87,6 +90,8 @@ func updateWorkdir(ctx context.Context, dir string, cloneURL string) error { return err } + slog.Debug("Pull", "url", cloneURL) + err = wtree.Pull(&git.PullOptions{Force: true}) if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) { return err @@ -98,6 +103,8 @@ func updateWorkdir(ctx context.Context, dir string, cloneURL string) error { func checkCompliance(ctx context.Context, module string, cloneURL string, tstamp float64) (*k6lint.Compliance, error) { com, found, err := loadCompliance(ctx, module, tstamp) if found { + slog.Debug("Compliance from cache", "module", module) + return com, nil } @@ -116,6 +123,8 @@ func checkCompliance(ctx context.Context, module string, cloneURL string, tstamp return nil, err } + slog.Debug("Check compliance", "module", module) + compliance, err := k6lint.Lint(ctx, dir, &k6lint.Options{ Passed: []k6lint.Checker{k6lint.CheckerLicense, k6lint.CheckerVersions, k6lint.CheckerGit}, }) diff --git a/cmd/load.go b/cmd/load.go index 473c7cd..4c98450 100644 --- a/cmd/load.go +++ b/cmd/load.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "log/slog" "strings" "github.com/Masterminds/semver/v3" @@ -34,8 +35,11 @@ func load(ctx context.Context, in io.Reader, loose bool, lint bool) (k6registry. ) if loose { + slog.Debug("Read source") raw, err = io.ReadAll(in) } else { + slog.Debug("Validate source") + raw, err = validateWithSchema(in) } @@ -45,6 +49,8 @@ func load(ctx context.Context, in io.Reader, loose bool, lint bool) (k6registry. var registry k6registry.Registry + slog.Debug("Unmarshal source") + if err := yaml.Unmarshal(raw, ®istry); err != nil { return nil, err } @@ -52,6 +58,7 @@ func load(ctx context.Context, in io.Reader, loose bool, lint bool) (k6registry. registry = append(registry, k6AsExtension()) for idx, ext := range registry { + slog.Debug("Process extension", "module", ext.Module) if len(ext.Tier) == 0 { registry[idx].Tier = k6registry.TierCommunity } @@ -99,6 +106,7 @@ func load(ctx context.Context, in io.Reader, loose bool, lint bool) (k6registry. } func loadRepository(ctx context.Context, module string) (*k6registry.Repository, []string, error) { + slog.Debug("Loading repository", "module", module) if strings.HasPrefix(module, k6Module) || strings.HasPrefix(module, ghModulePrefix) { repo, tags, err := loadGitHub(ctx, module) if err != nil { diff --git a/go.mod b/go.mod index 8003b15..0805794 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,8 @@ require ( github.com/grafana/clireadme v0.1.0 github.com/grafana/k6lint v0.1.0 github.com/narqo/go-badge v0.0.0-20230821190521-c9a75c019a59 + github.com/samber/slog-logrus/v2 v2.5.0 + github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.1 github.com/xanzy/go-gitlab v0.109.0 github.com/xeipuuv/gojsonschema v1.2.0 @@ -54,6 +56,8 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/samber/lo v1.44.0 // indirect + github.com/samber/slog-common v0.17.0 // indirect github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect github.com/shogo82148/go-shuffle v1.0.1 // indirect github.com/skeema/knownhosts v1.2.2 // indirect diff --git a/go.sum b/go.sum index 4d62278..9982647 100644 --- a/go.sum +++ b/go.sum @@ -139,12 +139,20 @@ github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDN github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/samber/lo v1.44.0 h1:5il56KxRE+GHsm1IR+sZ/6J42NODigFiqCWpSc2dybA= +github.com/samber/lo v1.44.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU= +github.com/samber/slog-common v0.17.0 h1:HdRnk7QQTa9ByHlLPK3llCBo8ZSX3F/ZyeqVI5dfMtI= +github.com/samber/slog-common v0.17.0/go.mod h1:mZSJhinB4aqHziR0SKPqpVZjJ0JO35JfH+dDIWqaCBk= +github.com/samber/slog-logrus/v2 v2.5.0 h1:0R1QlxBApEX6GOdCulqvDbeOK09LCSUDSEjVvm5ZeD4= +github.com/samber/slog-logrus/v2 v2.5.0/go.mod h1:xN6h40pDGXSJDgZsttF9KtaIV7dtpjeoBDpw8TpvRr8= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/shogo82148/go-shuffle v0.0.0-20180218125048-27e6095f230d/go.mod h1:2htx6lmL0NGLHlO8ZCf+lQBGBHIbEujyywxJArf+2Yc= github.com/shogo82148/go-shuffle v1.0.1 h1:4swIpHXLMAz14DE4YTgakgadpRN0n1wE1dieGnOTVFU= github.com/shogo82148/go-shuffle v1.0.1/go.mod h1:HQPjVgUUZ9TNgm4/K/iXRuAdhPsQrXnAGgtk/9kqbBY= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A= github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= @@ -156,6 +164,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e h1:BuzhfgfWQbX0dWzYzT1zsORLnHRv3bcRcsaUk0VmXA8= diff --git a/tools/gendoc/main.go b/tools/gendoc/main.go index c1f8b3e..364ded8 100644 --- a/tools/gendoc/main.go +++ b/tools/gendoc/main.go @@ -7,6 +7,6 @@ import ( ) func main() { - root, _ := cmd.New() + root, _ := cmd.New(nil) clireadme.Main(root, 1) }