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

Commit

Permalink
update to typical-go 0.9.9
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Dec 9, 2019
1 parent 55b4c93 commit 0589b0d
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 55 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ Use `./typicalw release` to make the release. [Learn More](https://typical-go.gi
- `./typicalw docker compose`: Generate docker-compose.yaml
- `./typicalw docker up`: Spin up docker containers
- `./typicalw docker down`: Take down all docker containers
- `./typicalw redis`: Redis Tool
- `./typicalw redis console`: Redis Interactive
- `./typicalw postgres`: Postgres Database Tool
- `./typicalw postgres create`: Create New Database
- `./typicalw postgres drop`: Drop Database
- `./typicalw postgres migrate`: Migrate Database
- `./typicalw postgres rollback`: Rollback Database
- `./typicalw postgres seed`: Database Seeding
- `./typicalw postgres console`: PostgreSQL Interactive
- `./typicalw redis`: Redis Tool
- `./typicalw redis console`: Redis Interactive
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (m module) Action() interface{} {
return startServer
}

func (m module) Commands(c *typcli.AppCli) []*cli.Command {
func (m module) AppCommands(c *typcli.AppCli) []*cli.Command {
return []*cli.Command{
{Name: "route", Usage: "Print available API Routes", Action: c.Action(taskRouteList)},
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.4.0
github.com/typical-go/typical-go v0.9.8
github.com/typical-go/typical-go v0.9.9
github.com/urfave/cli/v2 v2.0.0
github.com/valyala/fasttemplate v1.1.0 // indirect
github.com/yuin/gopher-lua v0.0.0-20190514113301-1cd887cd7036 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/typical-go/typical-go v0.9.8 h1:r6vjHQAq9S5dNAfYx0+ek1CI9rnFEcUQY63q4P6lqCg=
github.com/typical-go/typical-go v0.9.8/go.mod h1:7qbV2CpwulvgDWI6TpYe+sP2bJDFI1W5lD4dYj0Hp6w=
github.com/typical-go/typical-go v0.9.9/go.mod h1:f35xUuqFpsV5s6epiGeR1CrNNAmAhTi846jmireIIW4=
github.com/urfave/cli/v2 v2.0.0 h1:+HU9SCbu8GnEUFtIBfuUNXN39ofWViIEJIp6SURMpCg=
github.com/urfave/cli/v2 v2.0.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand Down Expand Up @@ -161,6 +162,7 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20191030062658-86caa796c7ab h1:tpc/nJ4vD66vAk/2KN0sw/DvQIz2sKmCpWvyKtPmfMQ=
golang.org/x/tools v0.0.0-20191030062658-86caa796c7ab/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191206204035-259af5ff87bd/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Expand Down
15 changes: 4 additions & 11 deletions pkg/typdocker/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ import (
"github.com/urfave/cli/v2"
)

// Module of docker
func Module() interface{} {
return dockerModule{
Name: "docker",
}
}

type dockerModule struct {
Name string
}
// Module of Docker
type Module struct{}

func (dockerModule) Commands(c *typcli.ModuleCli) []*cli.Command {
// BuildCommands is command collection to called from
func (*Module) BuildCommands(c *typcli.BuildCli) []*cli.Command {
cmd := dockerCommand{
Context: c.Context,
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/typdocker/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import (
)

func TestModule(t *testing.T) {
m := typdocker.Module()
m := &typdocker.Module{}
require.True(t, typcli.IsBuildCommander(m))
}
4 changes: 2 additions & 2 deletions pkg/typpostgres/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type Module struct {
DBName string
}

// Commands of module
func (p Module) Commands(c *typcli.ModuleCli) []*cli.Command {
// BuildCommands of module
func (p Module) BuildCommands(c *typcli.BuildCli) []*cli.Command {
return []*cli.Command{
{
Name: "postgres",
Expand Down
2 changes: 1 addition & 1 deletion pkg/typpostgres/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ func TestModule(t *testing.T) {
require.True(t, typmodule.IsProvider(m))
require.True(t, typmodule.IsDestroyer(m))
require.True(t, typmodule.IsPreparer(m))
require.True(t, typcli.IsModuleCommander(m))
require.True(t, typcli.IsBuildCommander(m))
require.True(t, typcfg.IsConfigurer(m))
}
3 changes: 1 addition & 2 deletions pkg/typreadme/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (Generator) Generate(ctx *typctx.Context, w io.Writer) (err error) {
md.Writef("- `%s`: Run the application\n", appName)
}
if commander, ok := ctx.AppModule.(typcli.AppCommander); ok {
for _, cmd := range commander.Commands(&typcli.AppCli{}) {
for _, cmd := range commander.AppCommands(&typcli.AppCli{}) {
md.Writef("- `%s %s`: %s\n", appName, cmd.Name, cmd.Usage)
for _, subcmd := range cmd.Subcommands {
md.Writef("\t- `%s %s %s`: %s\n", appName, cmd.Name, subcmd.Name, subcmd.Usage)
Expand Down Expand Up @@ -98,6 +98,5 @@ func fields(ctx *typctx.Context) (keys coll.Strings, m map[string]typcfg.Field)
}
}
}
// TODO: sort by name
return
}
32 changes: 15 additions & 17 deletions pkg/typredis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ type Config struct {
MaxConnAge time.Duration `envconfig:"MAX_CONN_AGE" default:"30m" required:"true"`
}

// Module of redis
func Module() interface{} {
return &module{}
}

type module struct{}
// Module of Redis
type Module struct{}

func (r module) Configure() (prefix string, spec, loadFn interface{}) {
// Configure Redis
func (r *Module) Configure() (prefix string, spec, loadFn interface{}) {
prefix = "REDIS"
spec = &Config{}
loadFn = func(loader typcfg.Loader) (cfg Config, err error) {
Expand All @@ -51,28 +48,28 @@ func (r module) Configure() (prefix string, spec, loadFn interface{}) {
}

// Provide dependencies
func (r module) Provide() []interface{} {
func (r *Module) Provide() []interface{} {
return []interface{}{
r.connect,
}
}

// Prepare the module
func (r module) Prepare() []interface{} {
func (r *Module) Prepare() []interface{} {
return []interface{}{
r.ping,
}
}

// Destroy dependencies
func (r module) Destroy() []interface{} {
func (r *Module) Destroy() []interface{} {
return []interface{}{
r.disconnect,
}
}

// BuildCommand of module
func (r module) Commands(c *typcli.ModuleCli) []*cli.Command {
// BuildCommands of module
func (r *Module) BuildCommands(c *typcli.BuildCli) []*cli.Command {
return []*cli.Command{
{
Name: "redis",
Expand All @@ -87,7 +84,8 @@ func (r module) Commands(c *typcli.ModuleCli) []*cli.Command {
}
}

func (r module) DockerCompose() typdocker.Compose {
// DockerCompose template
func (r *Module) DockerCompose() typdocker.Compose {
return typdocker.Compose{
Services: map[string]interface{}{
"redis": typdocker.Service{
Expand All @@ -107,7 +105,7 @@ func (r module) DockerCompose() typdocker.Compose {
}
}

func (module) connect(cfg Config) (client *redis.Client) {
func (*Module) connect(cfg Config) (client *redis.Client) {
client = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", cfg.Host, cfg.Port),
Password: cfg.Password,
Expand All @@ -123,16 +121,16 @@ func (module) connect(cfg Config) (client *redis.Client) {
return
}

func (module) ping(client *redis.Client) error {
func (*Module) ping(client *redis.Client) error {
log.Info("Ping to Redis")
return client.Ping().Err()
}

func (module) disconnect(client *redis.Client) (err error) {
func (*Module) disconnect(client *redis.Client) (err error) {
return client.Close()
}

func (module) console(config *Config) (err error) {
func (*Module) console(config *Config) (err error) {
args := []string{
"-h", config.Host,
"-p", config.Port,
Expand Down
4 changes: 2 additions & 2 deletions pkg/typredis/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
)

func TestModule(t *testing.T) {
m := typredis.Module()
m := &typredis.Module{}
require.True(t, typmodule.IsProvider(m))
require.True(t, typmodule.IsDestroyer(m))
require.True(t, typmodule.IsPreparer(m))
require.True(t, typcli.IsModuleCommander(m))
require.True(t, typcli.IsBuildCommander(m))
require.True(t, typcfg.IsConfigurer(m))
}
21 changes: 10 additions & 11 deletions pkg/typserver/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ type Config struct {
Debug bool `default:"false"`
}

// Module of server
func Module() interface{} {
return &module{}
}

type module struct{}
// Module of Server
type Module struct{}

func (s module) Configure() (prefix string, spec, loadFn interface{}) {
// Configure server
func (s *Module) Configure() (prefix string, spec, loadFn interface{}) {
prefix = "SERVER"
spec = &Config{}
loadFn = func(loader typcfg.Loader) (cfg Config, err error) {
Expand All @@ -33,20 +30,22 @@ func (s module) Configure() (prefix string, spec, loadFn interface{}) {
return
}

func (s module) Provide() []interface{} {
// Provide dependencies
func (s *Module) Provide() []interface{} {
return []interface{}{
s.Create,
}
}

func (s module) Destroy() []interface{} {
// Destroy dependencies
func (s *Module) Destroy() []interface{} {
return []interface{}{
s.Shutdown,
}
}

// Create new server
func (s module) Create(cfg Config) *echo.Echo {
func (s *Module) Create(cfg Config) *echo.Echo {
server := echo.New()
server.HideBanner = true
server.Debug = cfg.Debug
Expand All @@ -65,7 +64,7 @@ func (s module) Create(cfg Config) *echo.Echo {
}

// Shutdown the server
func (s module) Shutdown(server *echo.Echo) error {
func (s *Module) Shutdown(server *echo.Echo) error {
fmt.Println("Server is shutting down")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion pkg/typserver/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestModule(t *testing.T) {
m := typserver.Module()
m := &typserver.Module{}
require.True(t, typmodule.IsProvider(m))
require.True(t, typmodule.IsDestroyer(m))
require.True(t, typcfg.IsConfigurer(m))
Expand Down
6 changes: 3 additions & 3 deletions typical/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ var Context = &typctx.Context{
Package: "github.com/typical-go/typical-rest-server",
AppModule: app.Module(),
Modules: []interface{}{
typdocker.Module(),
typserver.Module(),
typredis.Module(),
&typdocker.Module{},
&typserver.Module{},
&typredis.Module{},
&typpostgres.Module{
DBName: "sample",
},
Expand Down

0 comments on commit 0589b0d

Please sign in to comment.