Skip to content

Commit

Permalink
use zap as logger
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Jan 2, 2023
1 parent 9a7a21a commit 56c3162
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"github.com/kobtea/gorgo/storage"
"github.com/open-policy-agent/conftest/output"
"github.com/open-policy-agent/conftest/runner"
"go.uber.org/zap"
)

func Check(ctx context.Context, cfg *config.Config) error {
logger := zap.S().Named("check")
logger.Info("check data")
var result []output.CheckResult
st, err := storage.NewStorage(cfg.WorkingDir)
if err != nil {
Expand Down Expand Up @@ -40,6 +43,7 @@ func Check(ctx context.Context, cfg *config.Config) error {
Combine: ConftestConfig.Combine,
Policy: ConftestConfig.Policies,
}
logger.Debugw(fmt.Sprintf("conftest"), "policy", ConftestConfig.Policies, "input", files)
res, err := r.Run(ctx, files)
if err != nil {
return err
Expand Down Expand Up @@ -70,6 +74,7 @@ func Check(ctx context.Context, cfg *config.Config) error {
Combine: ConftestConfig.Combine,
Policy: ConftestConfig.Policies,
}
logger.Debugw(fmt.Sprintf("conftest"), "policy", ConftestConfig.Policies, "input", files)
res, err := r.Run(ctx, files)
if err != nil {
return err
Expand Down
21 changes: 21 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package cmd
import (
"fmt"
"os"
"time"

"github.com/kobtea/gorgo/config"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var cfgFile string
var cfg *config.Config
var logLevel string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -26,6 +30,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "./gorgo.yaml", "config file")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "log level")
}

// initConfig reads in config file and ENV variables if set.
Expand All @@ -40,4 +45,20 @@ func initConfig() {
fmt.Println(err)
os.Exit(1)
}

loggerConfig := zap.NewProductionConfig()
lv, err := zap.ParseAtomicLevel(logLevel)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
loggerConfig.Level = lv
loggerConfig.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(time.RFC3339)
logger, err := loggerConfig.Build()
defer logger.Sync()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
zap.ReplaceGlobals(logger)
}
2 changes: 2 additions & 0 deletions fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"github.com/google/go-github/v48/github"
"github.com/kobtea/gorgo/config"
"github.com/kobtea/gorgo/storage"
"go.uber.org/zap"
"golang.org/x/oauth2"
)

func Fetch(ctx context.Context, cfg *config.Config) error {
zap.S().Named("fetch").Info("fetch data")
storage, err := storage.NewStorage(cfg.WorkingDir)
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/open-policy-agent/conftest v0.37.0
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1
go.uber.org/zap v1.24.0
golang.org/x/oauth2 v0.3.0
gopkg.in/yaml.v2 v2.4.0
)
Expand Down Expand Up @@ -115,6 +116,8 @@ require (
github.com/yashtewari/glob-intersection v0.1.0 // indirect
github.com/zclconf/go-cty v1.8.1 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sync v0.1.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ github.com/aws/aws-sdk-go v1.44.26/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
github.com/basgys/goxml2json v1.1.0 h1:4ln5i4rseYfXNd86lGEB+Vi652IsIXIvggKM/BhUKVw=
github.com/basgys/goxml2json v1.1.0/go.mod h1:wH7a5Np/Q4QoECFIU8zTQlZwZkrilY0itPfecMw41Dw=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
Expand Down Expand Up @@ -1395,9 +1396,16 @@ go.opentelemetry.io/otel/trace v1.0.0-RC1/go.mod h1:86UHmyHWFEtWjfWPSbu0+d0Pf9Q6
go.opentelemetry.io/proto/otlp v0.9.0/go.mod h1:1vKfU9rv61e9EVGthD1zNvUbiwPcimSsOPU9brfSHJg=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
go4.org v0.0.0-20180809161055-417644f6feb5/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE=
gocloud.dev v0.19.0/go.mod h1:SmKwiR8YwIMMJvQBKLsC3fHNyMwXLw3PMDO+VVteJMI=
golang.org/x/build v0.0.0-20190314133821-5284462c4bec/go.mod h1:atTaCNAy0f16Ah5aV1gMSwgiKVHwu/JncqDpuRr7lS4=
Expand Down
7 changes: 7 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"go.uber.org/zap"
)

const (
Expand All @@ -20,15 +21,18 @@ const (
type Storage struct {
workingDir string
gcCandidates []string
logger *zap.SugaredLogger
}

func NewStorage(workingDir string) (*Storage, error) {
s := &Storage{
workingDir: workingDir,
logger: zap.S().Named("storage"),
}
if err := s.prepareGc(); err != nil {
return nil, err
}
s.logger.Debug("initialized storage")
return s, nil
}

Expand Down Expand Up @@ -64,6 +68,7 @@ func (s *Storage) ListDirs() ([]string, error) {
}

func (s *Storage) UpdateRepoMetadata(domain, name, repo string, data []byte) error {
s.logger.Info(fmt.Sprintf("update repo metadata: %s/%s/%s", domain, name, repo))
path := s.RepoPath(MetadataDirname, domain, name, repo)
if err := os.MkdirAll(path, 0755); err != nil {
return err
Expand All @@ -76,6 +81,7 @@ func (s *Storage) UpdateRepoMetadata(domain, name, repo string, data []byte) err
}

func (s *Storage) UpdateSource(domain, name, repo, cloneUrl, tokenEnvvarName string) error {
s.logger.Info(fmt.Sprintf("update source: %s/%s/%s", domain, name, repo))
token := os.Getenv(tokenEnvvarName)
path := s.RepoPath(SourceDirname, domain, name, repo)
gitRepo, err := git.PlainOpen(path)
Expand Down Expand Up @@ -126,6 +132,7 @@ func (s *Storage) flagActive(path string) {

func (s *Storage) DoGc() error {
for _, path := range s.gcCandidates {
s.logger.Info(fmt.Sprintf("gc: remove unused dir: %s", path))
if err := os.RemoveAll(path); err != nil {
return err
}
Expand Down

0 comments on commit 56c3162

Please sign in to comment.