Skip to content

Commit

Permalink
Merge pull request #58 from grafana/57-add-verbose-logging
Browse files Browse the repository at this point in the history
feat: add verbose logging
  • Loading branch information
szkiba authored Sep 9, 2024
2 parents c622759 + 7ba61eb commit ad1f747
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 6 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
_ "embed"
"encoding/json"
"io/fs"
"log/slog"
"os"
"path/filepath"

Expand All @@ -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
Expand All @@ -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())
}
Expand Down Expand Up @@ -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")
Expand Down
7 changes: 6 additions & 1 deletion cmd/k6registry/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -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
}
Expand Down
25 changes: 22 additions & 3 deletions cmd/k6registry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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")
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"io/fs"
"log/slog"
"os"
"path/filepath"

Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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},
})
Expand Down
8 changes: 8 additions & 0 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"strings"

"github.com/Masterminds/semver/v3"
Expand Down Expand Up @@ -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)
}

Expand All @@ -45,13 +49,16 @@ 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, &registry); err != nil {
return nil, err
}

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
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion tools/gendoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (
)

func main() {
root, _ := cmd.New()
root, _ := cmd.New(nil)
clireadme.Main(root, 1)
}

0 comments on commit ad1f747

Please sign in to comment.