Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
pass around the correct logger to the services
Browse files Browse the repository at this point in the history
Also did some minor refactoring to make the code similar to the other
services.
  • Loading branch information
David Christofas committed Jun 23, 2020
1 parent 58aef84 commit 33b4af8
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 60 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ require (
github.com/owncloud/ocis-settings v0.0.0-20200522101320-46ea31026363
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/restic/calens v0.2.0
github.com/rs/zerolog v1.19.0
github.com/spf13/viper v1.7.0
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
github.com/tredoe/osutil v1.0.5
Expand Down
81 changes: 75 additions & 6 deletions go.sum

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ var (

// Execute is the entry point for the ocis-accounts command.
func Execute() error {
rootCfg := config.New()
cfg := config.New()
app := &cli.App{
Name: "ocis-accounts",
Version: version.String,
Usage: "Example service for Reva/oCIS",
Flags: flagset.RootWithConfig(rootCfg),
Name: "ocis-accounts",
Version: version.String,
Usage: "Example service for Reva/oCIS",
Compiled: version.Compiled(),
Flags: flagset.RootWithConfig(cfg),
Before: func(c *cli.Context) error {
logger := NewLogger(config.New())
err := ParseConfig(c, cfg)
logger := NewLogger(cfg)
for _, v := range defaultConfigPaths {
// location is the user's home
if v[0] == '$' || v[0] == '~' {
Expand All @@ -46,7 +48,7 @@ func Execute() error {
}
}
}
return nil
return err
},

Authors: []*cli.Author{
Expand All @@ -57,7 +59,7 @@ func Execute() error {
},

Commands: []*cli.Command{
Server(rootCfg),
Server(cfg),
},
}

Expand Down
13 changes: 2 additions & 11 deletions pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import (
"github.com/owncloud/ocis-accounts/pkg/config"
"github.com/owncloud/ocis-accounts/pkg/server/grpc"
svc "github.com/owncloud/ocis-accounts/pkg/service/v0"
oclog "github.com/owncloud/ocis-pkg/v2/log"
)

var (
logger oclog.Logger
)

// Server is the entry point for the server command.
Expand All @@ -26,15 +21,10 @@ func Server(cfg *config.Config) *cli.Command {
Description: "uses an LDAP server as the storage backend",
Flags: flagset.ServerWithConfig(cfg),
Before: func(c *cli.Context) error {
logger = oclog.NewLogger(
oclog.Name(cfg.Server.Name),
oclog.Level("info"),
oclog.Color(true),
oclog.Pretty(true),
)
return ParseConfig(c, cfg)
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
gr := run.Group{}
ctx, cancel := context.WithCancel(context.Background())

Expand All @@ -46,6 +36,7 @@ func Server(cfg *config.Config) *cli.Command {
grpc.Name(cfg.Server.Name),
grpc.Namespace(cfg.Server.Namespace),
grpc.Address(cfg.Server.Address),
grpc.Flags(flagset.RootWithConfig(config.New())),
)

gr.Add(func() error {
Expand Down
9 changes: 9 additions & 0 deletions pkg/server/grpc/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package grpc
import (
"context"

"github.com/micro/cli/v2"
"github.com/owncloud/ocis-accounts/pkg/config"
"github.com/owncloud/ocis-pkg/v2/log"
)
Expand All @@ -18,6 +19,7 @@ type Options struct {
Context context.Context
Config *config.Config
Namespace string
Flags []cli.Flag
}

// newOptions initializes the available default options.
Expand Down Expand Up @@ -72,3 +74,10 @@ func Namespace(val string) Option {
o.Namespace = val
}
}

// Flags provides a function to set the flags option.
func Flags(val []cli.Flag) Option {
return func(o *Options) {
o.Flags = append(o.Flags, val...)
}
}
3 changes: 2 additions & 1 deletion pkg/server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ func NewService(opts ...Option) grpc.Service {
grpc.Address(options.Address),
grpc.Namespace(options.Namespace),
grpc.Logger(options.Logger),
grpc.Flags(options.Flags...),
)

var hdlr *svc.Service
var err error

if hdlr, err = svc.New(options.Config); err != nil {
if hdlr, err = svc.New(svc.Logger(options.Logger), svc.Config(options.Config)); err != nil {
options.Logger.Fatal().Err(err).Msg("could not initialize service handler")
}
if err = proto.RegisterAccountsServiceHandler(service.Server(), hdlr); err != nil {
Expand Down
39 changes: 39 additions & 0 deletions pkg/service/v0/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package service

import (
"github.com/owncloud/ocis-accounts/pkg/config"
"github.com/owncloud/ocis-pkg/v2/log"
)

// Option defines a single option function.
type Option func(o *Options)

// Options defines the available options for this package.
type Options struct {
Logger log.Logger
Config *config.Config
}

func newOptions(opts ...Option) Options {
opt := Options{}

for _, o := range opts {
o(&opt)
}

return opt
}

// Logger provides a function to set the logger option.
func Logger(val log.Logger) Option {
return func(o *Options) {
o.Logger = val
}
}

// Config provides a function to set the config option.
func Config(val *config.Config) Option {
return func(o *Options) {
o.Config = val
}
}
Loading

0 comments on commit 33b4af8

Please sign in to comment.