Skip to content

Commit

Permalink
fix: use zap logger
Browse files Browse the repository at this point in the history
Signed-off-by: Valery Piashchynski <[email protected]>
  • Loading branch information
rustatian committed Oct 16, 2024
1 parent 49f2bed commit 9dde883
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 101 deletions.
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Documentation: <https://github.com/golangci/golangci-lint#config-file>

run:
go: '1.17'
timeout: 1m
allow-parallel-runners: true

Expand Down Expand Up @@ -36,7 +35,7 @@ linters: # All available linters list: <https://golangci-lint.run/usage/linters/
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
- errorlint # find code that will cause problems with the error wrapping scheme introduced in Go 1.13
- exhaustive # check exhaustiveness of enum switch statements
- exportloopref # checks for pointers to enclosing loop variables
- copyloopvar # checks for pointers to enclosing loop variables
- gochecknoglobals # Checks that no globals are present in Go code
- gochecknoinits # Checks that no init functions are present in Go code
- goconst # Finds repeated strings that could be replaced by a constant
Expand Down
30 changes: 15 additions & 15 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package builder
import (
"bytes"
"fmt"
"log/slog"
"math/rand"
"os"
"os/exec"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/hashicorp/go-version"
"github.com/roadrunner-server/velox/v2024"
"github.com/roadrunner-server/velox/v2024/builder/templates"
"go.uber.org/zap"
)

const (
Expand All @@ -38,12 +38,12 @@ type Builder struct {
rrTempPath string
out string
modules []*velox.ModulesInfo
log *slog.Logger
log *zap.Logger
debug bool
rrVersion string
}

func NewBuilder(rrTmpPath string, modules []*velox.ModulesInfo, out, rrVersion string, debug bool, log *slog.Logger) *Builder {
func NewBuilder(rrTmpPath string, modules []*velox.ModulesInfo, out, rrVersion string, debug bool, log *zap.Logger) *Builder {
return &Builder{
rrTempPath: rrTmpPath,
modules: modules,
Expand Down Expand Up @@ -99,7 +99,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return fmt.Errorf("unknown module version: %s", t.ModuleVersion)
}

b.log.Debug("template", slog.String("template", buf.String()))
b.log.Debug("template", zap.String("template", buf.String()))

f, err := os.Open(b.rrTempPath)
if err != nil {
Expand All @@ -113,7 +113,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
}

for i := 0; i < len(files); i++ {
b.log.Info("cleaning temporary folders", slog.String("file/folder", files[i]))
b.log.Info("cleaning temporary folders", zap.String("file/folder", files[i]))
_ = os.RemoveAll(files[i])
}
}()
Expand Down Expand Up @@ -163,7 +163,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return fmt.Errorf("unknown module version: %s", t.ModuleVersion)
}

b.log.Debug("template", slog.String("template", buf.String()))
b.log.Debug("template", zap.String("template", buf.String()))

_, err = goModFile.Write(buf.Bytes())
if err != nil {
Expand All @@ -173,7 +173,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
// reuse buffer
buf.Reset()

b.log.Info("switching working directory", slog.String("wd", b.rrTempPath))
b.log.Info("switching working directory", zap.String("wd", b.rrTempPath))
err = syscall.Chdir(b.rrTempPath)
if err != nil {
return err
Expand All @@ -189,7 +189,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return err
}

b.log.Info("creating output directory", slog.String("dir", b.out))
b.log.Info("creating output directory", zap.String("dir", b.out))
err = os.MkdirAll(b.out, os.ModeDir)
if err != nil {
return err
Expand All @@ -200,7 +200,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return err
}

b.log.Info("moving binary", slog.String("file", filepath.Join(b.rrTempPath, executableName)), slog.String("to", filepath.Join(b.out, executableName)))
b.log.Info("moving binary", zap.String("file", filepath.Join(b.rrTempPath, executableName)), zap.String("to", filepath.Join(b.out, executableName)))
err = moveFile(filepath.Join(b.rrTempPath, executableName), filepath.Join(b.out, executableName))
if err != nil {
return err
Expand All @@ -210,7 +210,7 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
}

func (b *Builder) Write(d []byte) (int, error) {
b.log.Debug("[STDERR OUTPUT]", slog.Any("log", d))
b.log.Debug("[STDERR OUTPUT]", zap.ByteString("log", d))
return len(d), nil
}

Expand Down Expand Up @@ -270,7 +270,7 @@ func (b *Builder) goBuildCmd(out string) error {

cmd = exec.Command("go", buildCmdArgs...)

b.log.Info("building RoadRunner", slog.String("cmd", cmd.String()))
b.log.Info("building RoadRunner", zap.String("cmd", cmd.String()))
cmd.Stderr = b
cmd.Stdout = b
err := cmd.Start()
Expand All @@ -285,7 +285,7 @@ func (b *Builder) goBuildCmd(out string) error {
}

func (b *Builder) goModDowloadCmd() error {
b.log.Info("downloading dependencies", slog.String("cmd", "go mod download"))
b.log.Info("downloading dependencies", zap.String("cmd", "go mod download"))
cmd := exec.Command("go", "mod", "download")
cmd.Stderr = b
err := cmd.Start()
Expand All @@ -300,7 +300,7 @@ func (b *Builder) goModDowloadCmd() error {
}

func (b *Builder) goModTidyCmd() error {
b.log.Info("updating dependencies", slog.String("cmd", "go mod tidy"))
b.log.Info("updating dependencies", zap.String("cmd", "go mod tidy"))
cmd := exec.Command("go", "mod", "tidy")
cmd.Stderr = b
err := cmd.Start()
Expand All @@ -315,7 +315,7 @@ func (b *Builder) goModTidyCmd() error {
}

func (b *Builder) getDepsReplace(repl string) []*templates.Entry {
b.log.Info("found replace, processing", slog.String("dependency", repl))
b.log.Info("found replace, processing", zap.String("dependency", repl))
modFile, err := os.ReadFile(path.Join(repl, goModStr))
if err != nil {
return nil
Expand All @@ -326,7 +326,7 @@ func (b *Builder) getDepsReplace(repl string) []*templates.Entry {
for i := 0; i < len(replaces); i++ {
split := strings.Split(strings.TrimSpace(replaces[i][0]), " => ")
if len(split) != 2 {
b.log.Error("not enough split args", slog.String("replace", replaces[i][0]))
b.log.Error("not enough split args", zap.String("replace", replaces[i][0]))
continue
}

Expand Down
5 changes: 3 additions & 2 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package builder

import (
"log/slog"
"os"
"path"
"testing"

"github.com/roadrunner-server/velox/v2024"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -81,7 +81,8 @@ func setup() *Builder {
"dummy_multiple_absolute_remote": []byte(replaceGoModMultipleRemote),
}

b := NewBuilder("/tmp", []*velox.ModulesInfo{}, "", "v2024.1.0", false, slog.Default())
l, _ := zap.NewDevelopment()
b := NewBuilder("/tmp", []*velox.ModulesInfo{}, "", "v2024.1.0", false, l)

b.modules = []*velox.ModulesInfo{
{
Expand Down
24 changes: 12 additions & 12 deletions github/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"path"
"strings"
Expand All @@ -14,6 +13,7 @@ import (

"github.com/google/go-github/v61/github"
"github.com/roadrunner-server/velox/v2024"
"go.uber.org/zap"
)

const (
Expand All @@ -26,7 +26,7 @@ type processor struct {
errs []error
wg sync.WaitGroup
mu sync.Mutex
log *slog.Logger
log *zap.Logger
queueCh chan *pcfg
modinfo []*velox.ModulesInfo
client *github.Client
Expand All @@ -37,7 +37,7 @@ type pcfg struct {
name string
}

func newPool(log *slog.Logger, client *github.Client) *processor {
func newPool(log *zap.Logger, client *github.Client) *processor {
p := &processor{
maxWorkers: 10,
log: log,
Expand All @@ -61,11 +61,11 @@ func (p *processor) run() {
for v := range p.queueCh {
modInfo := new(velox.ModulesInfo)
p.log.Debug("fetching plugin data",
slog.String("repository", v.pluginCfg.Repo),
slog.String("owner", v.pluginCfg.Owner),
slog.String("folder", v.pluginCfg.Folder),
slog.String("plugin", v.name),
slog.String("ref", v.pluginCfg.Ref),
zap.String("repository", v.pluginCfg.Repo),
zap.String("owner", v.pluginCfg.Owner),
zap.String("folder", v.pluginCfg.Folder),
zap.String("plugin", v.name),
zap.String("ref", v.pluginCfg.Ref),
)

if v.pluginCfg.Ref == "" {
Expand Down Expand Up @@ -93,7 +93,7 @@ func (p *processor) run() {
line := scanner.Text()
switch { //nolint:gocritic
case strings.HasPrefix(line, modLine):
p.log.Debug("reading module info", slog.String("plugin", v.name), slog.String("module", line))
p.log.Debug("reading module info", zap.String("plugin", v.name), zap.String("module", line))

// module github.com/roadrunner-server/logger/v2, we split and get the second part
retMod := strings.Split(line, " ")
Expand All @@ -115,10 +115,10 @@ func (p *processor) run() {

err = resp.Body.Close()
if err != nil {
p.log.Warn("failed to close response body, continuing", slog.Any("error", err))
p.log.Warn("failed to close response body, continuing", zap.Any("error", err))
}

p.log.Debug("requesting commit", slog.String("plugin", v.name), slog.String("ref", v.pluginCfg.Ref))
p.log.Debug("requesting commit", zap.String("plugin", v.name), zap.String("ref", v.pluginCfg.Ref))
commits, rsp, err := p.client.Repositories.ListCommits(context.Background(), v.pluginCfg.Owner, v.pluginCfg.Repo, &github.CommitsListOptions{
SHA: v.pluginCfg.Ref,
Until: time.Now(),
Expand Down Expand Up @@ -155,7 +155,7 @@ func (p *processor) run() {

if v.pluginCfg.Replace != "" {
modInfo.Replace = v.pluginCfg.Replace
p.log.Debug("found replace, applying", slog.String("plugin", v.name), slog.String("path", v.pluginCfg.Replace))
p.log.Debug("found replace, applying", zap.String("plugin", v.name), zap.String("path", v.pluginCfg.Replace))
}

p.mu.Lock()
Expand Down
22 changes: 11 additions & 11 deletions github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"path"
Expand All @@ -16,6 +15,7 @@ import (

"github.com/google/go-github/v61/github"
"github.com/roadrunner-server/velox/v2024"
"go.uber.org/zap"
"golang.org/x/oauth2"
)

Expand All @@ -31,10 +31,10 @@ GHRepo represents template repository
type GHRepo struct {
client *github.Client
config *velox.Config
log *slog.Logger
log *zap.Logger
}

func NewGHRepoInfo(cfg *velox.Config, log *slog.Logger) *GHRepo {
func NewGHRepoInfo(cfg *velox.Config, log *zap.Logger) *GHRepo {
var client *http.Client

// if a token exists, use it to increase rate limiter
Expand All @@ -53,7 +53,7 @@ func NewGHRepoInfo(cfg *velox.Config, log *slog.Logger) *GHRepo {

// DownloadTemplate downloads template repository ->
func (r *GHRepo) DownloadTemplate(tmp, version string) (string, error) { //nolint:gocyclo
r.log.Info("obtaining link", slog.String("owner", rrOwner), slog.String("repository", rrRepo), slog.String("encoding", "zip"), slog.String("ref", version))
r.log.Info("obtaining link", zap.String("owner", rrOwner), zap.String("repository", rrRepo), zap.String("encoding", "zip"), zap.String("ref", version))
url, resp, err := r.client.Repositories.GetArchiveLink(context.Background(), rrOwner, rrRepo, github.Zipball, &github.RepositoryContentGetOptions{Ref: version}, 10)
if err != nil {
return "", err
Expand All @@ -63,14 +63,14 @@ func (r *GHRepo) DownloadTemplate(tmp, version string) (string, error) { //nolin
return "", fmt.Errorf("wrong response status, got: %d", resp.StatusCode)
}

r.log.Info("seding download request", slog.String("url", url.String()))
r.log.Info("seding download request", zap.String("url", url.String()))
request, err := r.client.NewRequest(http.MethodGet, url.String(), nil)
if err != nil {
return "", err
}

buf := new(bytes.Buffer)
r.log.Info("downloading repository", slog.String("url", url.String()))
r.log.Info("downloading repository", zap.String("url", url.String()))
do, err := r.client.Do(context.Background(), request, buf)
if err != nil {
return "", err
Expand All @@ -85,7 +85,7 @@ func (r *GHRepo) DownloadTemplate(tmp, version string) (string, error) { //nolin
name := path.Join(tmp, "roadrunner-server-"+version)
_ = os.RemoveAll(name)

r.log.Debug("saving repository in temporary folder", slog.String("path", name+zipExt))
r.log.Debug("saving repository in temporary folder", zap.String("path", name+zipExt))
f, err := os.Create(name + zipExt)
if err != nil {
return "", err
Expand All @@ -100,7 +100,7 @@ func (r *GHRepo) DownloadTemplate(tmp, version string) (string, error) { //nolin
return "", err
}

r.log.Debug("repository saved", slog.Int("bytes written", n))
r.log.Debug("repository saved", zap.Int("bytes written", n))

rc, err := zip.OpenReader(name + zipExt)
if err != nil {
Expand Down Expand Up @@ -146,14 +146,14 @@ func (r *GHRepo) DownloadTemplate(tmp, version string) (string, error) { //nolin
}

for _, zf := range rc.File {
r.log.Debug("extracting repository", slog.String("file", zf.Name), slog.String("path", dest))
r.log.Debug("extracting repository", zap.String("file", zf.Name), zap.String("path", dest))
err = extract(dest, zf)
if err != nil {
return "", err
}
}

r.log.Info("repository saved", slog.String("path", filepath.Join(dest, outDir))) //nolint:gosec
r.log.Info("repository saved", zap.String("path", filepath.Join(dest, outDir))) //nolint:gosec
// first name is the output path
return filepath.Join(dest, outDir), nil //nolint:gosec
}
Expand Down Expand Up @@ -201,7 +201,7 @@ func extract(dest string, zf *zip.File) error {
// https://github.com/spiral/roadrunner-binary/archive/refs/tags/v2.7.0.zip

func (r *GHRepo) GetPluginsModData() ([]*velox.ModulesInfo, error) {
poolExecutor := newPool(r.log, r.client)
poolExecutor := newPool(r.log.Named("pool"), r.client)
for k, v := range r.config.GitHub.Plugins {
poolExecutor.add(&pcfg{
pluginCfg: v,
Expand Down
Loading

0 comments on commit 9dde883

Please sign in to comment.