支持了是否显示预设msg的配置

This commit is contained in:
Pan Qiancheng 2025-04-24 20:35:01 +08:00
parent 6f4d653835
commit 9e0f7bd2ea
4 changed files with 36 additions and 17 deletions

View File

@ -187,10 +187,10 @@ func initConfig() {
viper.AddConfigPath(configDir)
viper.SetConfigName("config")
viper.SetConfigType("yaml")
logger.UpdateLogLevel()
if err := viper.ReadInConfig(); err != nil {
logger.Error("Error initializing config: %v", err)
}
logger.UpdateLogLevel()
logger.Debug("Initializing...")
}

View File

@ -11,6 +11,8 @@ import (
"path/filepath"
"strings"
"time"
"github.com/spf13/viper"
)
type ExecParams struct {
@ -30,6 +32,10 @@ func NewUnixSocketTask() *UnixSocketTask {
return &UnixSocketTask{}
}
func init() {
viper.SetDefault("reporter.show_msg", true)
}
func (t *UnixSocketTask) Execute(ctx context.Context) {
socketPath := constants.SocketPath
@ -167,23 +173,27 @@ func (t *UnixSocketTask) handleConnection(conn net.Conn) {
// 在HTTP调用前发送[start]标记
t.writeMessage(conn, "[sthttp]")
initialMessage := constants.ColorBlue + "检测到您的命令执行出现错误! \n" + constants.ColorReset
t.writeMessage(conn, initialMessage)
logger.Debug("write: %s", initialMessage)
showMsg := viper.GetBool("reporter.show_msg")
// 显示加载动画
loading := true
go func() {
frames := []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
i := 0
for loading {
t.writeMessage(conn, "\r正在查找解决方案"+frames[i%len(frames)])
time.Sleep(100 * time.Millisecond)
i++
}
if showMsg {
initialMessage := constants.ColorBlue + "检测到您的命令执行出现错误! \n" + constants.ColorReset
t.writeMessage(conn, initialMessage)
go func() {
frames := []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
i := 0
for loading {
t.writeMessage(conn, "\r正在查找解决方案"+frames[i%len(frames)])
time.Sleep(100 * time.Millisecond)
i++
}
}()
}()
time.Sleep(200 * time.Millisecond)
}
time.Sleep(200 * time.Millisecond)
client := client.NewClient()
handleQuestion := func(question string) {
@ -210,10 +220,19 @@ func (t *UnixSocketTask) handleConnection(conn net.Conn) {
if err != nil {
logger.Error("PostToStream error: %v", err)
if showMsg {
t.writeMessage(conn, constants.ColorRed+"请求失败,请稍后再试"+constants.ColorReset)
}
}
var resetMsg string
if showMsg {
resetMsg = constants.ColorReset
} else {
resetMsg = "\n\r" + constants.ColorReset
}
// 结束后发送结束标记
t.writeMessage(conn, "\n\r"+constants.ColorReset)
t.writeMessage(conn, resetMsg)
t.writeMessage(conn, "[end]")
conn.Close() // 关闭连接防止客户端一直等待inf
logger.Debug("connection done.")

View File

@ -52,7 +52,7 @@ func WithBaseURL(baseURL string) ClientOption {
}
func init() {
viper.SetDefault("machine_registry.endpoint", "http://localhost:3001/endpoint")
viper.SetDefault("machine_registry.endpoint", "https://bash-backend.zustmyy.top/endpoint")
}
func NewClient(opts ...ClientOption) *Client {

View File

@ -9,6 +9,6 @@ const (
ClientTimeout = 120 * time.Second
MachineInfoApi = "/machine"
GetVersionApi = "/version"
QuestionStreamApi = "/question"
QuestionStreamApi = "/problem"
GetConfigApi = "/config"
)