Skip to content

Commit

Permalink
Use Context instead if Registry
Browse files Browse the repository at this point in the history
  • Loading branch information
snovichkov committed Oct 15, 2019
1 parent f47e578 commit f693cd5
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package viper

import (
"context"
"errors"
"os"
"strings"

Expand Down Expand Up @@ -30,6 +32,14 @@ type (
optionFunc func(bundle *Bundle)
)

var (
// ErrUndefinedAppPath is error, triggered when app.path is undefined in current context.
ErrUndefinedAppPath = errors.New("app.path is undefined")

// ErrUndefinedCliCmd is error, triggered when cli.cmd is undefined in current context.
ErrUndefinedCliCmd = errors.New("cli.cmd is undefined")
)

const (
// BundleName is default definition name.
BundleName = "viper"
Expand Down Expand Up @@ -122,21 +132,21 @@ func (b *Bundle) Build(builder *di.Builder) error {
di.Def{
Name: BundleName,
Build: func(ctn di.Container) (_ interface{}, err error) {
var registry glue.Registry
if err = ctn.Fill(glue.DefRegistry, &registry); err != nil {
var ctx context.Context
if err = ctn.Fill(glue.DefContext, &ctx); err != nil {
return nil, err
}

var path string
if err = registry.Fill("app.path", &path); err != nil {
return nil, err
var path, ok = ctx.Value("app.path").(string)
if !ok {
return nil, ErrUndefinedAppPath
}

b.viper.AddConfigPath(path)

var cmd *cobra.Command
if err = registry.Fill("cli.cmd", &cmd); err != nil {
return nil, err
if cmd, ok = ctx.Value("cli.cmd").(*cobra.Command); !ok {
return nil, ErrUndefinedCliCmd
}

var configFile string
Expand Down

0 comments on commit f693cd5

Please sign in to comment.