Skip to content

Commit

Permalink
Support sub states logic for application (#176)
Browse files Browse the repository at this point in the history
Signed-off-by: Congqi Xia <[email protected]>
  • Loading branch information
congqixia authored Aug 7, 2023
1 parent da722bb commit 7737b6a
Show file tree
Hide file tree
Showing 37 changed files with 855 additions and 577 deletions.
4 changes: 2 additions & 2 deletions bapps/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package bapps
import (
"log"

"github.com/milvus-io/birdwatcher/states"
"github.com/milvus-io/birdwatcher/framework"
)

// BApp interface for birdwatcher application
type BApp interface {
Run(states.State)
Run(framework.State)
}

// AppOption application setup option function.
Expand Down
24 changes: 19 additions & 5 deletions bapps/go_prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ import (

"github.com/c-bata/go-prompt"
"github.com/milvus-io/birdwatcher/configs"
"github.com/milvus-io/birdwatcher/framework"
"github.com/milvus-io/birdwatcher/history"
"github.com/milvus-io/birdwatcher/states"
"github.com/samber/lo"
)

// PromptApp wraps go-prompt as application.
type PromptApp struct {
exited bool
currentState states.State
currentState framework.State
sugguestHistory bool
historyHelper *history.Helper
logger *log.Logger
prompt *prompt.Prompt
config *configs.Config
}

func NewPromptApp(config *configs.Config, opts ...AppOption) BApp {
Expand All @@ -32,9 +33,14 @@ func NewPromptApp(config *configs.Config, opts ...AppOption) BApp {
o(opt)
}

config.Logger = opt.logger

// use workspace path to open&store history log
hh := history.NewHistoryHelper(config.WorkspacePath)
pa := &PromptApp{historyHelper: hh}
pa := &PromptApp{
historyHelper: hh,
config: config,
}
pa.logger = opt.logger

historyItems := hh.List("")
Expand Down Expand Up @@ -69,7 +75,7 @@ func NewPromptApp(config *configs.Config, opts ...AppOption) BApp {
return pa
}

func (a *PromptApp) Run(start states.State) {
func (a *PromptApp) Run(start framework.State) {
a.currentState = start
a.prompt.Run()
}
Expand Down Expand Up @@ -132,7 +138,7 @@ func (a *PromptApp) promptExecute(in string) {
os.Stdout = stdout
close(pagerSig)
}
a.currentState, _ = a.currentState.Process(in)
nextState, err := a.currentState.Process(in)
if writer != nil {
writer.Close()
}
Expand All @@ -143,6 +149,14 @@ func (a *PromptApp) promptExecute(in string) {
a.historyHelper.AddLog(in)
a.sugguestHistory = false

if err != nil {
fmt.Println(err.Error())
return
}

nextState.SetupCommands()
a.currentState = nextState

if a.currentState.IsEnding() {
fmt.Println("Bye!")
a.exited = true
Expand Down
4 changes: 2 additions & 2 deletions bapps/olc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"strings"

"github.com/milvus-io/birdwatcher/states"
"github.com/milvus-io/birdwatcher/framework"
"github.com/samber/lo"
)

Expand All @@ -24,7 +24,7 @@ func NewOlcApp(script string) BApp {
}
}

func (a *olcApp) Run(start states.State) {
func (a *olcApp) Run(start framework.State) {
app := start
cmds := a.parseScripts(a.script)
var err error
Expand Down
10 changes: 6 additions & 4 deletions bapps/promptui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import (
"errors"

"github.com/manifoldco/promptui"
"github.com/milvus-io/birdwatcher/states"
"github.com/milvus-io/birdwatcher/common"
"github.com/milvus-io/birdwatcher/framework"
)

// simpleApp wraps promptui as BApp.
type simpleApp struct {
currentState states.State
currentState framework.State
}

func NewSimpleApp() BApp {
return &simpleApp{}
}

// Run starts BirdWatcher with promptui. (disable suggestion and history)
func (a *simpleApp) Run(start states.State) {
func (a *simpleApp) Run(start framework.State) {
app := start
for {
p := promptui.Prompt{
Expand All @@ -30,12 +31,13 @@ func (a *simpleApp) Run(start states.State) {
line, err := p.Run()
if err == nil {
app, err = app.Process(line)
if errors.Is(err, states.ExitErr) {
if errors.Is(err, common.ExitErr) {
break
}
if app.IsEnding() {
return
}
app.SetupCommands()
}
}
}
9 changes: 5 additions & 4 deletions bapps/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type InstanceInfo struct {
RootPath string `form:"rootPath"`
}

func (app *WebServerApp) Run(states.State) {
func (app *WebServerApp) Run(framework.State) {
r := gin.Default()
etcdversion.SetVersion(models.GTEVersion2_2)

Expand All @@ -41,7 +41,7 @@ func (app *WebServerApp) Run(states.State) {
r.Run(fmt.Sprintf(":%d", app.port))
}

func (app *WebServerApp) ParseRouter(r *gin.Engine, s states.State) {
func (app *WebServerApp) ParseRouter(r *gin.Engine, s framework.State) {
v := reflect.ValueOf(s)
tp := v.Type()

Expand Down Expand Up @@ -105,7 +105,7 @@ func (app *WebServerApp) parseMethod(r *gin.Engine, mt reflect.Method, name stri

//fmt.Println(mt.Name)
cp := reflect.New(paramType.Elem()).Interface().(framework.CmdParam)
fUse, _ := states.GetCmdFromFlag(cp)
fUse, _ := framework.GetCmdFromFlag(cp)
if len(use) == 0 {
use = fUse
}
Expand All @@ -114,7 +114,7 @@ func (app *WebServerApp) parseMethod(r *gin.Engine, mt reflect.Method, name stri
fnName := mt.Name
use = strings.ToLower(fnName[:len(fnName)-8])
}
uses := states.ParseUseSegments(use)
uses := framework.ParseUseSegments(use)
lastKw := uses[len(uses)-1]
// hard code, show xxx command only
if uses[0] != "show" {
Expand All @@ -135,6 +135,7 @@ func (app *WebServerApp) parseMethod(r *gin.Engine, mt reflect.Method, name stri
c.Error(err)
return
}
s.SetupCommands()

v := reflect.ValueOf(s)
cp := reflect.New(paramType.Elem()).Interface().(framework.CmdParam)
Expand Down
12 changes: 12 additions & 0 deletions common/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package common

// ExitErr is the error indicates user needs to exit application.
var ExitErr = exitErr{}

// exitErr internal err type for comparing.
type exitErr struct{}

// Error implements error.
func (e exitErr) Error() string {
return "exited"
}
2 changes: 1 addition & 1 deletion common/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import "github.com/blang/semver/v4"
var Version semver.Version

func init() {
Version = semver.MustParse("1.0.2")
Version = semver.MustParse("1.1.0-dev")
}
3 changes: 3 additions & 0 deletions configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path"

Expand All @@ -27,6 +28,8 @@ type Config struct {
ConfigPath string `yaml:"-"`
// backup workspace path, default $PWD/bw_workspace
WorkspacePath string `yaml:"WorkspacePath"`

Logger *log.Logger
}

func (c *Config) load() error {
Expand Down
Loading

0 comments on commit 7737b6a

Please sign in to comment.