-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#105 CLI console
- Loading branch information
Showing
16 changed files
with
307 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.