Skip to content

Commit

Permalink
Merge pull request #177 from poteto-go/20241229/hot-reload
Browse files Browse the repository at this point in the history
`poteto-cli run` start app with hot-reload
  • Loading branch information
poteto0 authored Jan 3, 2025
2 parents 68f071e + 91dbacb commit 80544b8
Show file tree
Hide file tree
Showing 29 changed files with 1,163 additions and 22 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ or `/examples`

## Poteto-Cli

It will be moved to other repo.

We support cli tool. But if you doesn't like it, you can create poteto-app w/o cli of course.

```sh
Expand All @@ -37,6 +39,19 @@ fast mode.
poteto-cli new --fast
```

### run app with hot-reload
- create `poteto.yaml`
```yaml
version: "0.27"
build_script_path: "main.go"
debug_mode: true
```
- command
```sh
poteto-cli run
```

### Demo

https://github.com/user-attachments/assets/4b739964-1b4f-4913-b643-5984bf1ceae1
Expand Down
2 changes: 1 addition & 1 deletion binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (*binder) Bind(ctx Context, object any) error {
}
}

// if not application/json
// if not application/
// return nil
return nil
}
2 changes: 1 addition & 1 deletion cmd/cmd-new/cmd_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func CommandNew() {
param := engine.EngineNewParam{}

fmt.Println("You can also use poteto-cli -h | --help")
fmt.Println("You can also use poteto-cli new -h | --help")
for i := 2; i < len(os.Args); i++ {
switch {
case os.Args[i] == "-h", os.Args[i] == "--help":
Expand Down
63 changes: 63 additions & 0 deletions cmd/cmd-run/cmd_run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmdrun

import (
"fmt"
"os"

"github.com/poteto0/poteto/cmd/core"
"github.com/poteto0/poteto/cmd/engine"
"github.com/poteto0/poteto/utils"
)

func loadOption() core.RunnerOption {
configFile, err := os.Open("./poteto.yaml")
defer configFile.Close()

if err != nil {
utils.PotetoPrint("you can use poteto.yaml")
return core.DefaultRunnerOption
}

configBytes := make([]byte, 1024)
n, err := configFile.Read(configBytes)
if err != nil || n == 0 {
utils.PotetoPrint("warning error on reading poteto.yaml, use default option")
return core.DefaultRunnerOption
}

var option core.RunnerOption
err = utils.YamlParse(configBytes[:n], &option)
if err != nil {
utils.PotetoPrint("warning error on reading poteto.yaml, use default option")
return core.DefaultRunnerOption
}

return option
}

func CommandRun() {
option := loadOption()

fmt.Println("You can also use poteto-cli run -h | --help")
for i := 2; i < len(os.Args); i++ {
switch {
case os.Args[i] == "-h", os.Args[i] == "--help":
help()
os.Exit(-1)
default:
fmt.Println("unknown command or option:", os.Args[i])
os.Exit(-1)
}
}

engine.RunRun(option)
}

func help() {
fmt.Println("poteto-cli run: hot-reload run api server")
fmt.Println("https://github.com/poteto0/poteto")
fmt.Println("========================================")
fmt.Println("")
fmt.Println("Options:")
fmt.Println(" -h, --help: Display help (this is this)")
}
68 changes: 68 additions & 0 deletions cmd/cmd-run/cmd_run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmdrun

import (
"os"
"testing"

"bou.ke/monkey"
"github.com/poteto0/poteto/cmd/core"
"github.com/poteto0/poteto/cmd/engine"
)

func TestHelp(t *testing.T) {
help()
}

func TestCommandRun(t *testing.T) {
defer monkey.UnpatchAll()

calledExit := false
monkey.Patch(os.Exit, func(code int) {
calledExit = true
})
monkey.Patch(engine.RunRun, func(param core.RunnerOption) error {
return nil
})

tests := []struct {
name string
args []string
expected bool
}{
{
"test -h case",
[]string{"poteto-cli", "run", "-h"},
true,
},
{
"test --help case",
[]string{"poteto-cli", "run", "-h"},
true,
},
{
"test unknown case",
[]string{"poteto-cli", "run", "unknown"},
true,
},
{
"test nothing case (work)",
[]string{"poteto-cli", "run"},
false,
},
}

for _, it := range tests {
t.Run(it.name, func(t *testing.T) {
defer func() {
calledExit = false
}()

os.Args = it.args
CommandRun()

if calledExit != it.expected {
t.Errorf("Unmatched call os.Exit")
}
})
}
}
Loading

0 comments on commit 80544b8

Please sign in to comment.