支持了是否显示预设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.AddConfigPath(configDir)
viper.SetConfigName("config") viper.SetConfigName("config")
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
logger.UpdateLogLevel()
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
logger.Error("Error initializing config: %v", err) logger.Error("Error initializing config: %v", err)
} }
logger.UpdateLogLevel()
logger.Debug("Initializing...") logger.Debug("Initializing...")
} }

View File

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

View File

@ -52,7 +52,7 @@ func WithBaseURL(baseURL string) ClientOption {
} }
func init() { 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 { func NewClient(opts ...ClientOption) *Client {

View File

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