Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
creata typiobj package
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Oct 30, 2019
1 parent 8d94424 commit cf03b96
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 70 deletions.
2 changes: 1 addition & 1 deletion EXPERIMENTAL/typicmd/prebuilder/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions EXPERIMENTAL/typictx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()...)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package typienv
package typictx

import (
"fmt"
Expand All @@ -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 (
Expand All @@ -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
Expand All @@ -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
}
24 changes: 24 additions & 0 deletions EXPERIMENTAL/typiobj/cli.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package typictx
package typiobj

// Configuration represent the configuration
type Configuration struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package typictx
package typiobj

import (
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/slice"
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package typictx
package typiobj

import (
"github.com/urfave/cli"
Expand Down
6 changes: 3 additions & 3 deletions app/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -11,15 +11,15 @@ import (
// Module of application
func Module() interface{} {
return applicationModule{
Configuration: typictx.Configuration{
Configuration: typiobj.Configuration{
Prefix: "APP",
Spec: &config.Config{},
},
}
}

type applicationModule struct {
typictx.Configuration
typiobj.Configuration
}

func (m applicationModule) CommandLine() cli.Command {
Expand Down
34 changes: 10 additions & 24 deletions pkg/typpostgres/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -29,15 +29,15 @@ const (
func Module() interface{} {
return &postgresModule{
Name: "Postgres",
Configuration: typictx.Configuration{
Configuration: typiobj.Configuration{
Prefix: "PG",
Spec: &Config{},
},
}
}

type postgresModule struct {
typictx.Configuration
typiobj.Configuration
Name string
}

Expand All @@ -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)},
},
}
}
Expand All @@ -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)
Expand Down
25 changes: 5 additions & 20 deletions pkg/typredis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -18,15 +17,15 @@ import (
func Module() interface{} {
return &redisModule{
Name: "Redis",
Configuration: typictx.Configuration{
Configuration: typiobj.Configuration{
Prefix: "REDIS",
Spec: &Config{},
},
}
}

type redisModule struct {
typictx.Configuration
typiobj.Configuration
Name string
}

Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/typserver/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,15 +17,15 @@ import (
func Module() interface{} {
return &serverModule{
Name: "Server",
Configuration: typictx.Configuration{
Configuration: typiobj.Configuration{
Prefix: "SERVER",
Spec: &Config{},
},
}
}

type serverModule struct {
typictx.Configuration
typiobj.Configuration
Name string
}

Expand Down

0 comments on commit cf03b96

Please sign in to comment.