-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from poteto-go/20241229/hot-reload
`poteto-cli run` start app with hot-reload
- Loading branch information
Showing
29 changed files
with
1,163 additions
and
22 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
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,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)") | ||
} |
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,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") | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.