Skip to content

Commit

Permalink
Merge pull request #120 from confetti-framework/#105-baker
Browse files Browse the repository at this point in the history
#105 CLI console
  • Loading branch information
reindert-vetter authored Mar 4, 2021
2 parents d15b395 + 6ed9331 commit 2f88cbe
Show file tree
Hide file tree
Showing 16 changed files with 307 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_DATABASE=confetti
DB_USERNAME=root
DB_PASSWORD=
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ RUN chown -R confetti:confetti $GOCACHE $GOMODCACHE
USER confetti

RUN go get github.com/cespare/reflex
CMD ~/go/bin/reflex -r '(\.go$|\.gohtml$|go\.mod$|\.env$)' -s -- sh -c "go run -race main.go"
CMD ~/go/bin/reflex -r '(\.go$|\.gohtml$|go\.mod$|\.env$)' -s -- sh -c "go run -race main.go app:serve"
51 changes: 51 additions & 0 deletions app/console/commands/app_serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package commands

import (
"github.com/confetti-framework/contract/inter"
net "net/http"
"strconv"
"time"
)

type AppServe struct {
Port int `short:"p" flag:"port"`
}

func (s AppServe) Name() string {
return "app:serve"
}

func (s AppServe) Description() string {
return "Start the http server to handle requests."
}

func (s AppServe) Handle(c inter.Cli) inter.ExitCode {
name := c.App().Make("config.App.Name").(string)
handler := c.App().Make((*net.HandlerFunc)(nil)).(func(net.ResponseWriter, *net.Request))

c.Info("Start %s to handle requests", name)
server := &net.Server{
Addr: s.getPortAddr(c.App()),
Handler: net.HandlerFunc(handler),
WriteTimeout: 30 * time.Second,
ReadTimeout: 30 * time.Second,
}
if err := server.ListenAndServe(); err != nil && err != net.ErrServerClosed {
c.Error("Could not %s", err)
return inter.Failure
}

c.Info("Server stopped")

return inter.Success
}

func (s AppServe) getPortAddr(app inter.App) string {
var port int
if s.Port != 0 {
port = s.Port
} else {
port = app.Make("config.App.Port").(int)
}
return ":" + strconv.Itoa(port)
}
22 changes: 22 additions & 0 deletions app/console/commands/example_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package commands

import (
"github.com/confetti-framework/contract/inter"
)

type ExampleCommand struct {
FirstFlag string `short:"f" flag:"first" description:"Configure your first flag" required:"true"`
}

func (t ExampleCommand) Name() string {
return "example:command"
}

func (t ExampleCommand) Description() string {
return "You can adjust this command to your wishes."
}

func (t ExampleCommand) Handle(c inter.Cli) inter.ExitCode {
c.Info("Value in fist flag: %s", t.FirstFlag)
return inter.Success
}
36 changes: 36 additions & 0 deletions app/console/getters/int_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package getters

import (
"fmt"
"github.com/confetti-framework/errors"
"strconv"
"strings"
)

type IntList []int

func (s *IntList) String() string {
return fmt.Sprintf("%v", *s)
}

func (s *IntList) Set(value string) error {
//goland:noinspection GoPreferNilSlice
result := []int{}
for _, part := range strings.Split(value, ",") {
v, err := strconv.ParseInt(part, 0, 0)
if err != nil {
return errors.New("unable to cast %#v of type %T to int", part, part)
}

result = append(result, int(v))
}
*s = result
return nil
}

// Get returns the value of type which must be
// the same type as defined in a field of a command.
func (s *IntList) Get() interface{} {
return []int(*s)
}

23 changes: 23 additions & 0 deletions app/console/getters/string_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package getters

import (
"fmt"
"strings"
)

type StringList []string

func (s *StringList) String() string {
return fmt.Sprintf("%v", *s)
}

func (s *StringList) Set(value string) error {
*s = strings.Split(value, ",")
return nil
}

// Get returns the value of type which must be
// the same type as defined in a field of a command.
func (s *StringList) Get() interface{} {
return []string(*s)
}
23 changes: 19 additions & 4 deletions app/console/kernel.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package console

import (
"confetti-framework/app/console/commands"
"confetti-framework/app/console/getters"
"flag"
"github.com/confetti-framework/contract/inter"
"github.com/confetti-framework/foundation/console"
)

type Kernel struct {
App inter.App
var flagGetters = func() []flag.Getter {
return []flag.Getter{
new(getters.StringList),
new(getters.IntList),
}
}

func NewKernel(app inter.App) Kernel {
return Kernel{app}
func NewKernel(app inter.App) console.Kernel {
return console.Kernel{
App: app,
Commands: []inter.Command{
commands.AppServe{},
console.LogClear{},
commands.ExampleCommand{},
},
FlagProviders: []func() []flag.Getter{flagGetters},
}
}
3 changes: 1 addition & 2 deletions app/http/decorator/register_providers.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package decorator

import (
"github.com/confetti-framework/contract/inter"
"confetti-framework/app/providers"
"github.com/confetti-framework/contract/inter"
)

type RegisterProviders struct{}

// Providers are located in config/providers/providers.go
func (r RegisterProviders) Bootstrap(container inter.Container) inter.Container {
for _, bootstrapper := range providers.Providers.RegisterProviders {
container = bootstrapper.Register(container)
Expand Down
4 changes: 2 additions & 2 deletions app/providers/provider_index.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package providers

import (
"confetti-framework/config"
"github.com/confetti-framework/contract/inter"
"github.com/confetti-framework/foundation/providers"
"confetti-framework/config"
)

var Providers = struct {
Expand All @@ -12,7 +12,7 @@ var Providers = struct {
}{
/*
|--------------------------------------------------------------------------
| Autoloaded Register Service Providers
| Register Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
Expand Down
10 changes: 8 additions & 2 deletions bootstrap/app.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package bootstrap

import (
"github.com/confetti-framework/contract/inter"
"github.com/confetti-framework/foundation"
"confetti-framework/app/console"
"confetti-framework/app/http"
"confetti-framework/app/http/decorator"
"github.com/confetti-framework/contract/inter"
"github.com/confetti-framework/foundation"
net "net/http"
)

var bootContainer inter.Container
Expand Down Expand Up @@ -53,5 +54,10 @@ func NewAppFromBoot() inter.App {
console.NewKernel(app),
)

app.Bind(
(*net.HandlerFunc)(nil),
HandleHttpKernel,
)

return app
}
22 changes: 22 additions & 0 deletions bootstrap/http_kernel_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package bootstrap

import (
"github.com/confetti-framework/foundation/http"
net "net/http"
)

func HandleHttpKernel(response net.ResponseWriter, request *net.Request) {
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate Go development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
app := NewAppFromBoot()
http.HandleHttpKernel(app, response, request)
}
12 changes: 12 additions & 0 deletions config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"github.com/confetti-framework/support/env"
"golang.org/x/text/language"
"os"
"time"
)

Expand All @@ -13,6 +14,7 @@ var App = struct {
LineSeparator,
Key,
Env string
OsArgs []string
Port int
Cipher string
Debug bool
Expand Down Expand Up @@ -118,6 +120,16 @@ var App = struct {
*/
Port: env.Int("APP_PORT"),

/*
|--------------------------------------------------------------------------
| Application Command-line Arguments
|--------------------------------------------------------------------------
|
| Args hold the command-line arguments, starting with the program name.
|
*/
OsArgs: os.Args,

/*
|--------------------------------------------------------------------------
| Application Debug Mode
Expand Down
2 changes: 1 addition & 1 deletion config/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var Logging = struct {
|--------------------------------------------------------------------------
|
| Here you may configure the log channels for your application. Out of
| the box, Laravel uses the confetti-framework/syslog logging library. This gives
| the box, Confetti uses the confetti-framework/syslog logging library. This gives
| you a variety of powerful log handlers / formatters to utilize.
|
| The name provided is for reference only, so you can log specifically to
Expand Down
13 changes: 4 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ module confetti-framework
go 1.15

require (
github.com/confetti-framework/contract v0.1.2
github.com/confetti-framework/contract v0.2.1
github.com/confetti-framework/errors v0.11.0
github.com/confetti-framework/foundation v0.4.0
github.com/confetti-framework/support v0.2.0
github.com/confetti-framework/foundation v0.5.6
github.com/confetti-framework/support v0.2.3
github.com/confetti-framework/syslog v0.1.1
github.com/confetti-framework/validation v0.1.0-rc.1
github.com/gorilla/mux v1.8.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/tidwall/gjson v1.6.8 // indirect
github.com/vigneshuvi/GoDateFormat v0.0.0-20210204121036-67364dc23c79 // indirect
github.com/confetti-framework/validation v0.1.0
golang.org/x/text v0.3.5
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
Loading

0 comments on commit 2f88cbe

Please sign in to comment.