• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fredbi / go-cli / 6734984523

02 Nov 2023 03:48PM UTC coverage: 76.135% (-0.5%) from 76.649%
6734984523

push

github

web-flow
fixed injectables (#5)

* fixed zap logger injectable context key
* ensured xxxFromContext does not panic when nil
* added lock to guard global die function

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

19 of 19 new or added lines in 4 files covered. (100.0%)

587 of 771 relevant lines covered (76.13%)

16.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.23
/cli/injectable/config.go
1
package injectable
2

3
import (
4
        "context"
5

6
        "github.com/spf13/viper"
7
)
8

9
type (
10
        commandCtxKey uint8
11

12
        // Config can wrap a viper.Viper configuration in the context
13
        Config struct {
14
                config *viper.Viper
15
        }
16
)
17

18
const (
19
        ctxConfig commandCtxKey = iota + 1
20
        ctxSLogger
21
        ctxZapLogger
22
)
23

24
var _ ContextInjectable = &Config{}
25

26
func NewConfig(v *viper.Viper) *Config {
8✔
27
        return &Config{config: v}
8✔
28
}
8✔
29

30
func (c *Config) Context(ctx context.Context) context.Context {
8✔
31
        return context.WithValue(ctx, ctxConfig, c.config)
8✔
32
}
8✔
33

34
func (c Config) FromContext(ctx context.Context) interface{} {
6✔
35
        cfg, ok := ctx.Value(ctxConfig).(*viper.Viper)
6✔
36
        if !ok {
6✔
37
                return nil
×
38
        }
×
39

40
        return cfg
6✔
41
}
42

43
// ConfigFromContext retrieves a configuration registry from the context.
44
//
45
// An optional defaulter can be added to deal with a non-existing config.
46
func ConfigFromContext(ctx context.Context, defaulters ...func() *viper.Viper) *viper.Viper {
6✔
47
        var c Config
6✔
48

6✔
49
        cfg := c.FromContext(ctx)
6✔
50

6✔
51
        if cfg == nil {
6✔
52
                for _, defaulter := range defaulters {
×
53
                        if defaulter == nil {
×
54
                                continue
×
55
                        }
56

57
                        cfg = defaulter()
×
58
                }
59
        }
60
        if cfg == nil {
6✔
61
                return nil
×
62
        }
×
63

64
        return cfg.(*viper.Viper)
6✔
65
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc