From 255b52b9bcb40baaeef2a95119537253deead316 Mon Sep 17 00:00:00 2001 From: congqixia Date: Wed, 9 Aug 2023 12:11:09 +0800 Subject: [PATCH] Firx one-line-command broken after AppplicationState refactoring (#179) Signed-off-by: Congqi Xia --- bapps/go_prompt.go | 2 +- bapps/olc.go | 1 + configs/config.go | 16 +++++++++++++--- states/app_state.go | 4 ++-- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/bapps/go_prompt.go b/bapps/go_prompt.go index 057ee27..5fd3f28 100644 --- a/bapps/go_prompt.go +++ b/bapps/go_prompt.go @@ -33,7 +33,7 @@ func NewPromptApp(config *configs.Config, opts ...AppOption) BApp { o(opt) } - config.Logger = opt.logger + config.SetLogger(opt.logger) // use workspace path to open&store history log hh := history.NewHistoryHelper(config.WorkspacePath) diff --git a/bapps/olc.go b/bapps/olc.go index 1823e25..24f276f 100644 --- a/bapps/olc.go +++ b/bapps/olc.go @@ -42,6 +42,7 @@ func (a *olcApp) Run(start framework.State) { if cmd.muted { os.Stdout = stdout } + app.SetupCommands() } } diff --git a/configs/config.go b/configs/config.go index 90b68a9..0f38708 100644 --- a/configs/config.go +++ b/configs/config.go @@ -3,7 +3,7 @@ package configs import ( "errors" "fmt" - "io/ioutil" + "io" "log" "os" "path" @@ -29,7 +29,17 @@ type Config struct { // backup workspace path, default $PWD/bw_workspace WorkspacePath string `yaml:"WorkspacePath"` - Logger *log.Logger + logger *log.Logger +} + +func (c *Config) SetLogger(logger *log.Logger) { + c.logger = logger +} + +func (c *Config) Log(v ...any) { + if c.logger != nil { + c.logger.Println(v...) + } } func (c *Config) load() error { @@ -43,7 +53,7 @@ func (c *Config) load() error { return err } defer f.Close() - bs, err := ioutil.ReadAll(f) + bs, err := io.ReadAll(f) if err != nil { return err } diff --git a/states/app_state.go b/states/app_state.go index 9ede242..d6f35be 100644 --- a/states/app_state.go +++ b/states/app_state.go @@ -42,7 +42,7 @@ func (app *ApplicationState) Label() string { } func (app *ApplicationState) Process(cmd string) (framework.State, error) { - app.config.Logger.Println("[INFO] begin to process command", cmd) + app.config.Log("[INFO] begin to process command", cmd) app.core.Process(cmd) // perform sub state transfer for key, state := range app.states { @@ -63,7 +63,7 @@ func (app *ApplicationState) Close() { } func (app *ApplicationState) SetNext(state framework.State) { - app.config.Logger.Println("[WARNING] SetNext called for ApplicationState, which is not expected.") + app.config.Log("[WARNING] SetNext called for ApplicationState, which is not expected.") } func (app *ApplicationState) NextState() framework.State {