Skip to content

Commit

Permalink
Prepare release 2.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyashtrikul committed Feb 20, 2021
1 parent 6c3ea70 commit 6035b84
Show file tree
Hide file tree
Showing 43 changed files with 524 additions and 316 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]


## [2.1.4] - 2021-02-21
- provider registration refactoring
- Deprecate app.NoWait()
- Deprecate server.NoWait()
- Deprecate app.NoDefaultProviders()
- Deprecate server.NoDefaultProviders()

## [2.1.3] - 2021-01-21
- Fixed server mode startup

Expand Down Expand Up @@ -73,7 +80,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## 1.0.0 - 2020-09-28

[Unreleased]: https://github.com/vseinstrumentiru/lego/compare/v2.1.3...HEAD
[Unreleased]: https://github.com/vseinstrumentiru/lego/compare/v2.1.4...HEAD
[2.1.4]: https://github.com/vseinstrumentiru/lego/compare/v2.1.3...v2.1.4
[2.1.3]: https://github.com/vseinstrumentiru/lego/compare/v2.1.0...v2.1.3
[2.1.0]: https://github.com/vseinstrumentiru/lego/compare/v2.0.10...v2.1.0

Expand Down
52 changes: 50 additions & 2 deletions app/command.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
package app

import "github.com/vseinstrumentiru/lego/v2/internal/execute"
import (
"os"

"emperror.dev/emperror"
"github.com/spf13/cobra"
"go.uber.org/dig"

"github.com/vseinstrumentiru/lego/v2/config"
di "github.com/vseinstrumentiru/lego/v2/internal/container"
"github.com/vseinstrumentiru/lego/v2/internal/env"
"github.com/vseinstrumentiru/lego/v2/version"
)

func rootCommand() *cobra.Command {
return &cobra.Command{
Use: "lego",
Hidden: true,
}
}

func showVersion(e env.Env, c di.ChainContainer) {
e.OnFlag("version", func(bool) {
c.Execute(func(ver *version.Info) { ver.Print() })
os.Exit(0)
})
}

type cmdArgs struct {
dig.In
Root *cobra.Command
Children []*cobra.Command `group:"cmd"`
}

func printDIGraph(e env.Env, c di.Container, cfg *config.Application) {
if cfg.DebugMode {
e.OnFlag("di", func(bool) {
emperror.Panic(c.Visualize(os.Stdout))
os.Exit(0)
})
}
}

func command(r *runtime) {
r.container.Execute(execute.RunCommands)
r.container.Execute(func(in cmdArgs) error {
in.Root.AddCommand(in.Children...)
cmds := in.Root.Commands()
if len(cmds) > 0 {
return in.Root.Execute()
}

return nil
})
}
46 changes: 37 additions & 9 deletions app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,47 @@ package app

import (
config "github.com/vseinstrumentiru/lego/v2/config"
"github.com/vseinstrumentiru/lego/v2/di"
"github.com/vseinstrumentiru/lego/v2/internal/env"
)

type Option func(r *runtime)

func resolveModulePack(pack []di.Module) (providers []interface{}, configurations []interface{}) {
for _, m := range pack {
p, c := m()
if p != nil {
providers = append(providers, p)
}

if len(c) > 0 {
configurations = append(configurations, c...)
}
}

return
}

func Provide(providers ...interface{}) Option {
return func(r *runtime) {
for _, provider := range providers {
r.container.Register(provider)
if pack, ok := provider.(func() []di.Module); ok {
p, c := resolveModulePack(pack())
for _, i := range p {
if i != nil {
r.container.Register(i)
}
}
r.configurations = append(r.configurations, c...)
} else if m, ok := provider.(func() (interface{}, []interface{})); ok {
p, c := m()
r.configurations = append(r.configurations, c...)
if p != nil {
r.container.Register(p)
}
} else {
r.container.Register(provider)
}
}
}
}
Expand All @@ -21,30 +53,26 @@ func LocalDebug() Option {
}
}

// Deprecated: use app.NewRuntime().Run()
func NoDefaultProviders() Option {
return func(r *runtime) {
r.opts.Set(config.OptWithoutProviders, true)
}
return func(r *runtime) {}
}

func CommandMode() Option {
return func(r *runtime) {
r.exec = command
r.opts.Set(config.ServerMode, false)
}
}

func ServerMode() Option {
return func(r *runtime) {
r.opts.Set(config.ServerMode, true)
r.exec = serve
}
}

// Deprecated: use app.CommandMode()
func NoWait() Option {
return func(r *runtime) {
r.opts.Set(config.ServerMode, false)
}
return CommandMode()
}

func EnvPath(path string) Option {
Expand Down
8 changes: 3 additions & 5 deletions server/run_test.go → app/run_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package server
package app

import (
"testing"

"github.com/vseinstrumentiru/lego/v2/app"
)

type testApp struct {
Expand All @@ -13,9 +11,9 @@ type testConfig struct {
}

func Test_WithConfig(t *testing.T) {
Run(testApp{}, app.WithConfig(&testConfig{}), app.NoWait())
NewRuntime(WithConfig(&testConfig{})).Run(testApp{})
}

func Test_NoConfig(t *testing.T) {
Run(testApp{}, app.NoWait())
NewRuntime().Run(testApp{})
}
28 changes: 10 additions & 18 deletions app/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/vseinstrumentiru/lego/v2/config"
di "github.com/vseinstrumentiru/lego/v2/internal/container"
"github.com/vseinstrumentiru/lego/v2/internal/env"
"github.com/vseinstrumentiru/lego/v2/internal/execute"
"github.com/vseinstrumentiru/lego/v2/internal/provide"
"github.com/vseinstrumentiru/lego/v2/multilog"
"github.com/vseinstrumentiru/lego/v2/multilog/multilogprovider"
"github.com/vseinstrumentiru/lego/v2/server/shutdown"
Expand Down Expand Up @@ -41,31 +39,33 @@ func NewRuntime(opts ...Option) *runtime {
}

type runtime struct {
container di.ChainContainer
log multilog.Logger
opts cast.CastableRWSet
exec runner
container di.ChainContainer
configurations []interface{}
log multilog.Logger
opts cast.CastableRWSet
exec runner
}

func (r *runtime) Providers() []interface{} {
return []interface{}{
provide.RootCommand,
rootCommand,
version.New,
shutdown.NewCloseGroup,
multilogprovider.Provide,
provide.Env,
env.Provide,
}
}

func (r *runtime) Configurations() []interface{} {
exec := []interface{}{
r.configureEnv,
execute.Version,
printDIGraph,
r.configureVersion,
showVersion,
r.configureLogger,
}

return exec
return append(exec, r.configurations...)
}

func (r *runtime) configureEnv(e env.Env) {
Expand Down Expand Up @@ -119,17 +119,9 @@ func (r *runtime) Run(apps ...interface{}) {

// constructing application
log.Trace("constructing application")
for _, provider := range provide.All(r) {
r.container.Register(provider)
}

for i := 0; i < len(apps); i++ {
r.container.Make(apps[i])
}

for _, e := range execute.All(r) {
r.container.Execute(e)
}
log.Trace("constructing application", map[string]interface{}{"status": "completed"})

r.exec(r)
Expand Down
18 changes: 1 addition & 17 deletions app/server.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
package app

import (
"os"
"os/signal"
"syscall"

"emperror.dev/errors/match"
"github.com/cloudflare/tableflip"
"github.com/oklog/run"
)

func serve(r *runtime) {
log := r.log
log.Trace("starting pipeline")
var pipeline *run.Group
var upg *tableflip.Upgrader
r.container.Execute(func(p *run.Group, u *tableflip.Upgrader) {
r.container.Execute(func(p *run.Group) {
pipeline = p
upg = u
})

go func() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGHUP)
for range ch {
log.Info("graceful reloading")
log.Notify(upg.Upgrade())
}
}()

// running application
if err := pipeline.Run(); err != nil {
log.WithErrFilter(match.As(&run.SignalError{}).MatchError).Notify(err)
Expand Down
18 changes: 18 additions & 0 deletions common/netx/listen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package netx

import (
"net"

"emperror.dev/errors"
"github.com/cloudflare/tableflip"
)

func Listen(network, addr string, upg *tableflip.Upgrader) (net.Listener, error) {
if upg != nil {
return upg.Listen(network, addr)
}

ln, err := net.Listen(network, addr)

return ln, errors.Wrap(err, "can't create new listener")
}
7 changes: 2 additions & 5 deletions config/options.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package config

const (
ServerMode = "server_mode"
OptEnvPath = "opt_env_path"

OptLocalDebug = "opt_local_debug"
OptWithoutProviders = "opt_without_providers"
OptEnvPath = "opt_env_path"
OptLocalDebug = "opt_local_debug"
)
4 changes: 4 additions & 0 deletions di/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package di

// Provider returns provider - function for registering provider and configurations - functions for configure it
type Module func() (provider interface{}, configurations []interface{})
13 changes: 6 additions & 7 deletions internal/provide/env.go → internal/env/provider.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package provide
package env

import (
"github.com/spf13/cobra"
"go.uber.org/dig"

"github.com/vseinstrumentiru/lego/v2/config"
"github.com/vseinstrumentiru/lego/v2/internal/env"
)

const (
Expand All @@ -15,23 +14,23 @@ const (
type envArgs struct {
dig.In
Runtime config.Runtime
Config env.Config `optional:"true"`
Config Config `optional:"true"`
Cmd *cobra.Command
}

func Env(in envArgs) (env.Env, config.Env) {
func Provide(in envArgs) (Env, config.Env) {
path := defaultEnvPath

in.Runtime.On(config.OptEnvPath, func(newPath string) {
path = newPath
})

var instance env.Env
var instance Env

if in.Config == nil {
instance = env.NewNoConfigEnv(in.Cmd.PersistentFlags(), path)
instance = NewNoConfigEnv(in.Cmd.PersistentFlags(), path)
} else {
instance = env.NewConfigEnv(in.Cmd.PersistentFlags(), path)
instance = NewConfigEnv(in.Cmd.PersistentFlags(), path)
}

instance.SetFlag("version", false, "show version")
Expand Down
Loading

0 comments on commit 6035b84

Please sign in to comment.