From cf03b9637870d798ea06634e2925726f2bcb868b Mon Sep 17 00:00:00 2001 From: iman tung Date: Wed, 30 Oct 2019 13:25:21 +0700 Subject: [PATCH] creata typiobj package --- EXPERIMENTAL/typicmd/prebuilder/run.go | 2 +- EXPERIMENTAL/typictx/context.go | 13 +++---- EXPERIMENTAL/{typienv => typictx}/env_file.go | 23 ++++++++----- EXPERIMENTAL/typiobj/cli.go | 24 +++++++++++++ .../{typictx => typiobj}/configuration.go | 2 +- EXPERIMENTAL/{typictx => typiobj}/modules.go | 2 +- EXPERIMENTAL/{typictx => typiobj}/object.go | 2 +- EXPERIMENTAL/{typictx => typiobj}/types.go | 2 +- app/module.go | 6 ++-- pkg/typpostgres/module.go | 34 ++++++------------- pkg/typredis/module.go | 25 +++----------- pkg/typserver/module.go | 6 ++-- 12 files changed, 71 insertions(+), 70 deletions(-) rename EXPERIMENTAL/{typienv => typictx}/env_file.go (71%) create mode 100644 EXPERIMENTAL/typiobj/cli.go rename EXPERIMENTAL/{typictx => typiobj}/configuration.go (92%) rename EXPERIMENTAL/{typictx => typiobj}/modules.go (98%) rename EXPERIMENTAL/{typictx => typiobj}/object.go (98%) rename EXPERIMENTAL/{typictx => typiobj}/types.go (97%) diff --git a/EXPERIMENTAL/typicmd/prebuilder/run.go b/EXPERIMENTAL/typicmd/prebuilder/run.go index 94b34bcf..6604879f 100644 --- a/EXPERIMENTAL/typicmd/prebuilder/run.go +++ b/EXPERIMENTAL/typicmd/prebuilder/run.go @@ -34,7 +34,7 @@ func Run(ctx *typictx.Context) { if err = ctx.Preparing(); err != nil { log.Fatal(err.Error()) } - if err = typienv.PrepareEnvFile(ctx); err != nil { + if err = typictx.PrepareEnvFile(ctx); err != nil { log.Fatal(err.Error()) } if err := prebuilder.Initiate(ctx); err != nil { diff --git a/EXPERIMENTAL/typictx/context.go b/EXPERIMENTAL/typictx/context.go index 8853a758..71195567 100644 --- a/EXPERIMENTAL/typictx/context.go +++ b/EXPERIMENTAL/typictx/context.go @@ -2,12 +2,13 @@ package typictx import ( "github.com/typical-go/typical-rest-server/EXPERIMENTAL/slice" + "github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj" "go.uber.org/dig" ) // Context of typical application type Context struct { - Modules + typiobj.Modules Release Application interface{} Name string @@ -20,8 +21,8 @@ type Context struct { } // Configurations return config list -func (c *Context) Configurations() (cfgs []Configuration) { - if configurer, ok := c.Application.(Configurer); ok { +func (c *Context) Configurations() (cfgs []typiobj.Configuration) { + if configurer, ok := c.Application.(typiobj.Configurer); ok { cfgs = append(cfgs, configurer.Configure()) } cfgs = append(cfgs, c.Modules.Configurations()...) @@ -56,7 +57,7 @@ func (c *Context) Construct(container *dig.Container) (err error) { return err } } - if constructor, ok := c.Application.(Constructor); ok { + if constructor, ok := c.Application.(typiobj.Constructor); ok { if err = constructor.Construct(container); err != nil { return } @@ -66,7 +67,7 @@ func (c *Context) Construct(container *dig.Container) (err error) { // Destruct dependencies func (c *Context) Destruct(container *dig.Container) (err error) { - if destructor, ok := c.Application.(Destructor); ok { + if destructor, ok := c.Application.(typiobj.Destructor); ok { if err = destructor.Destruct(container); err != nil { return } @@ -90,7 +91,7 @@ func (c *Context) validate() error { if c.Root == "" { return invalidContextError("Root can't not empty") } - if _, ok := c.Application.(Runner); !ok { + if _, ok := c.Application.(typiobj.Runner); !ok { return invalidContextError("Application must implement Runner") } return nil diff --git a/EXPERIMENTAL/typienv/env_file.go b/EXPERIMENTAL/typictx/env_file.go similarity index 71% rename from EXPERIMENTAL/typienv/env_file.go rename to EXPERIMENTAL/typictx/env_file.go index e7408289..f4b1d916 100644 --- a/EXPERIMENTAL/typienv/env_file.go +++ b/EXPERIMENTAL/typictx/env_file.go @@ -1,4 +1,4 @@ -package typienv +package typictx import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/joho/godotenv" "github.com/kelseyhightower/envconfig" log "github.com/sirupsen/logrus" - "github.com/typical-go/typical-rest-server/EXPERIMENTAL/typictx" + "github.com/urfave/cli" ) const ( @@ -18,7 +18,13 @@ const ( {{end}}` ) +// CliLoadEnvFile is cli version of LoadEnvFile +func CliLoadEnvFile(ctx *cli.Context) (err error) { + return LoadEnvFile() +} + // LoadEnvFile to load environment from .env file +// TODO: move to util func LoadEnvFile() (err error) { configSource := os.Getenv(configKey) var configs []string @@ -45,19 +51,18 @@ func LoadEnvFile() (err error) { } // PrepareEnvFile to write .env file if not exist -func PrepareEnvFile(ctx *typictx.Context) (err error) { - _, err = os.Stat(defaultDotEnv) - if !os.IsNotExist(err) { +func PrepareEnvFile(ctx *Context) (err error) { + if _, err = os.Stat(defaultDotEnv); !os.IsNotExist(err) { return } log.Infof("Generate new project environment at '%s'", defaultDotEnv) - buf, err := os.Create(defaultDotEnv) - if err != nil { + var file *os.File + if file, err = os.Create(defaultDotEnv); err != nil { return } - defer buf.Close() + defer file.Close() for _, cfg := range ctx.Configurations() { - envconfig.Usagef(cfg.Prefix, cfg.Spec, buf, envTemplate) + envconfig.Usagef(cfg.Prefix, cfg.Spec, file, envTemplate) } return } diff --git a/EXPERIMENTAL/typiobj/cli.go b/EXPERIMENTAL/typiobj/cli.go new file mode 100644 index 00000000..c9a78f15 --- /dev/null +++ b/EXPERIMENTAL/typiobj/cli.go @@ -0,0 +1,24 @@ +package typiobj + +import ( + "github.com/urfave/cli" + "go.uber.org/dig" +) + +// CliAction to return cli action +func CliAction(p interface{}, fn interface{}) func(ctx *cli.Context) error { + return func(ctx *cli.Context) (err error) { + c := dig.New() + defer func() { + if destructor, ok := p.(Destructor); ok { + destructor.Destruct(c) + } + }() + if constructor, ok := p.(Constructor); ok { + if err = constructor.Construct(c); err != nil { + return + } + } + return c.Invoke(fn) + } +} diff --git a/EXPERIMENTAL/typictx/configuration.go b/EXPERIMENTAL/typiobj/configuration.go similarity index 92% rename from EXPERIMENTAL/typictx/configuration.go rename to EXPERIMENTAL/typiobj/configuration.go index ed69c9df..97c0b6e3 100644 --- a/EXPERIMENTAL/typictx/configuration.go +++ b/EXPERIMENTAL/typiobj/configuration.go @@ -1,4 +1,4 @@ -package typictx +package typiobj // Configuration represent the configuration type Configuration struct { diff --git a/EXPERIMENTAL/typictx/modules.go b/EXPERIMENTAL/typiobj/modules.go similarity index 98% rename from EXPERIMENTAL/typictx/modules.go rename to EXPERIMENTAL/typiobj/modules.go index a9f7ceb1..ce06425e 100644 --- a/EXPERIMENTAL/typictx/modules.go +++ b/EXPERIMENTAL/typiobj/modules.go @@ -1,4 +1,4 @@ -package typictx +package typiobj import ( "github.com/typical-go/typical-rest-server/EXPERIMENTAL/slice" diff --git a/EXPERIMENTAL/typictx/object.go b/EXPERIMENTAL/typiobj/object.go similarity index 98% rename from EXPERIMENTAL/typictx/object.go rename to EXPERIMENTAL/typiobj/object.go index e9b57ed2..fcfbcff8 100644 --- a/EXPERIMENTAL/typictx/object.go +++ b/EXPERIMENTAL/typiobj/object.go @@ -1,4 +1,4 @@ -package typictx +package typiobj // Name of object. Return value from name field if available or return its type. func Name(obj interface{}) string { diff --git a/EXPERIMENTAL/typictx/types.go b/EXPERIMENTAL/typiobj/types.go similarity index 97% rename from EXPERIMENTAL/typictx/types.go rename to EXPERIMENTAL/typiobj/types.go index df9dd7c5..06d5dcbd 100644 --- a/EXPERIMENTAL/typictx/types.go +++ b/EXPERIMENTAL/typiobj/types.go @@ -1,4 +1,4 @@ -package typictx +package typiobj import ( "github.com/urfave/cli" diff --git a/app/module.go b/app/module.go index 187a16dd..05b9c540 100644 --- a/app/module.go +++ b/app/module.go @@ -2,7 +2,7 @@ package app import ( "github.com/kelseyhightower/envconfig" - "github.com/typical-go/typical-rest-server/EXPERIMENTAL/typictx" + "github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj" "github.com/typical-go/typical-rest-server/app/config" "github.com/urfave/cli" "go.uber.org/dig" @@ -11,7 +11,7 @@ import ( // Module of application func Module() interface{} { return applicationModule{ - Configuration: typictx.Configuration{ + Configuration: typiobj.Configuration{ Prefix: "APP", Spec: &config.Config{}, }, @@ -19,7 +19,7 @@ func Module() interface{} { } type applicationModule struct { - typictx.Configuration + typiobj.Configuration } func (m applicationModule) CommandLine() cli.Command { diff --git a/pkg/typpostgres/module.go b/pkg/typpostgres/module.go index 593fe618..e57ba2f3 100644 --- a/pkg/typpostgres/module.go +++ b/pkg/typpostgres/module.go @@ -15,7 +15,7 @@ import ( _ "github.com/lib/pq" log "github.com/sirupsen/logrus" "github.com/typical-go/typical-rest-server/EXPERIMENTAL/typictx" - "github.com/typical-go/typical-rest-server/EXPERIMENTAL/typienv" + "github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj" "github.com/urfave/cli" "go.uber.org/dig" ) @@ -29,7 +29,7 @@ const ( func Module() interface{} { return &postgresModule{ Name: "Postgres", - Configuration: typictx.Configuration{ + Configuration: typiobj.Configuration{ Prefix: "PG", Spec: &Config{}, }, @@ -37,7 +37,7 @@ func Module() interface{} { } type postgresModule struct { - typictx.Configuration + typiobj.Configuration Name string } @@ -47,14 +47,14 @@ func (p postgresModule) CommandLine() cli.Command { Name: "postgres", ShortName: "pg", Usage: "Postgres Database Tool", - Before: p.cliBefore, + Before: typictx.CliLoadEnvFile, Subcommands: []cli.Command{ - {Name: "create", Usage: "Create New Database", Action: p.action(p.createDB)}, - {Name: "drop", Usage: "Drop Database", Action: p.action(p.dropDB)}, - {Name: "migrate", Usage: "Migrate Database", Action: p.action(p.migrateDB)}, - {Name: "rollback", Usage: "Rollback Database", Action: p.action(p.rollbackDB)}, - {Name: "seed", Usage: "Database Seeding", Action: p.action(p.seedDB)}, - {Name: "console", Usage: "PostgreSQL interactive terminal", Action: p.action(p.console)}, + {Name: "create", Usage: "Create New Database", Action: typiobj.CliAction(p, p.createDB)}, + {Name: "drop", Usage: "Drop Database", Action: typiobj.CliAction(p, p.dropDB)}, + {Name: "migrate", Usage: "Migrate Database", Action: typiobj.CliAction(p, p.migrateDB)}, + {Name: "rollback", Usage: "Rollback Database", Action: typiobj.CliAction(p, p.rollbackDB)}, + {Name: "seed", Usage: "Database Seeding", Action: typiobj.CliAction(p, p.seedDB)}, + {Name: "console", Usage: "PostgreSQL interactive terminal", Action: typiobj.CliAction(p, p.console)}, }, } } @@ -70,20 +70,6 @@ func (p postgresModule) Destruct(c *dig.Container) (err error) { return c.Invoke(p.closeConnection) } -func (p postgresModule) cliBefore(ctx *cli.Context) (err error) { - return typienv.LoadEnvFile() -} - -func (p postgresModule) action(fn interface{}) func(ctx *cli.Context) error { - return func(ctx *cli.Context) (err error) { - c := dig.New() - if err = c.Provide(p.loadConfig); err != nil { - return - } - return c.Invoke(fn) - } -} - func (p postgresModule) loadConfig() (cfg *Config, err error) { cfg = new(Config) err = envconfig.Process(p.Configure().Prefix, cfg) diff --git a/pkg/typredis/module.go b/pkg/typredis/module.go index 755a3656..4cc800c0 100644 --- a/pkg/typredis/module.go +++ b/pkg/typredis/module.go @@ -5,11 +5,10 @@ import ( "os" "os/exec" - "github.com/typical-go/typical-rest-server/EXPERIMENTAL/typienv" - "github.com/go-redis/redis" "github.com/kelseyhightower/envconfig" "github.com/typical-go/typical-rest-server/EXPERIMENTAL/typictx" + "github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj" "github.com/urfave/cli" "go.uber.org/dig" ) @@ -18,7 +17,7 @@ import ( func Module() interface{} { return &redisModule{ Name: "Redis", - Configuration: typictx.Configuration{ + Configuration: typiobj.Configuration{ Prefix: "REDIS", Spec: &Config{}, }, @@ -26,7 +25,7 @@ func Module() interface{} { } type redisModule struct { - typictx.Configuration + typiobj.Configuration Name string } @@ -46,27 +45,13 @@ func (r redisModule) CommandLine() cli.Command { return cli.Command{ Name: "redis", Usage: "Redis Utility Tool", - Before: r.cliBefore, + Before: typictx.CliLoadEnvFile, Subcommands: []cli.Command{ - {Name: "console", ShortName: "c", Action: r.action(r.console)}, + {Name: "console", ShortName: "c", Action: typiobj.CliAction(r, r.console)}, }, } } -func (redisModule) cliBefore(ctx *cli.Context) (err error) { - return typienv.LoadEnvFile() -} - -func (r redisModule) action(fn interface{}) func(ctx *cli.Context) error { - return func(ctx *cli.Context) (err error) { - c := dig.New() - if err = c.Provide(r.loadConfig); err != nil { - return - } - return c.Invoke(fn) - } -} - func (r redisModule) loadConfig() (cfg *Config, err error) { cfg = new(Config) err = envconfig.Process(r.Configure().Prefix, cfg) diff --git a/pkg/typserver/module.go b/pkg/typserver/module.go index 25d95ac7..61b8453e 100644 --- a/pkg/typserver/module.go +++ b/pkg/typserver/module.go @@ -5,7 +5,7 @@ import ( "time" "github.com/kelseyhightower/envconfig" - "github.com/typical-go/typical-rest-server/EXPERIMENTAL/typictx" + "github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj" "go.uber.org/dig" logrusmiddleware "github.com/bakatz/echo-logrusmiddleware" @@ -17,7 +17,7 @@ import ( func Module() interface{} { return &serverModule{ Name: "Server", - Configuration: typictx.Configuration{ + Configuration: typiobj.Configuration{ Prefix: "SERVER", Spec: &Config{}, }, @@ -25,7 +25,7 @@ func Module() interface{} { } type serverModule struct { - typictx.Configuration + typiobj.Configuration Name string }