Skip to content
4 changes: 2 additions & 2 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ jobs:
submodules: 'recursive'
fetch-depth: 0
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v8
with:
args: --disable-all -E errcheck -E staticcheck
version: v2.1
skip-cache: true
problem-matchers: true
- name: Test (go test)
Expand Down
69 changes: 69 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# This configuration file is not a recommendation.
#
# We intentionally use a limited set of linters.
# This configuration file is used with different version of golangci-lint to avoid regressions:
# the linters can change between version,
# their configuration may be not compatible or their reports can be different,
# and this can break some of our tests.
# Also, some linters are not relevant for the project (e.g. linters related to SQL).
#
# We have specific constraints, so we use a specific configuration.
#
# See the file `.golangci.reference.yml` to have a list of all available configuration options.

version: "2"

linters:
default: none
# This list of linters is not a recommendation (same thing for all this configuration file).
# We intentionally use a limited set of linters.
# See the comment on top of this file.
enable:
- errcheck
- staticcheck
- errorlint

settings:
errorlint:
asserts: false
staticcheck:
checks:
# Invalid regular expression.
# https://staticcheck.dev/docs/checks/#SA1000
- all
- "-ST1000"
- "-S1023"
- "-S1005"
- "-QF1004"

exclusions:
paths:
- dist/
- docs/
- tests/
- bin/
- images/
- install/
- utils/

formatters:
enable:
- gofmt
- goimports
settings:
gofmt:
rewrite-rules:
- pattern: 'interface{}'
replacement: 'any'
goimports:
local-prefixes:
- github.com/gojue/moling
exclusions:
paths:
- dist/
- docs/
- tests/
- bin/
- images/
- install/
- utils/
8 changes: 5 additions & 3 deletions cli/cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
package cmd

import (
"github.com/gojue/moling/client"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"os"
"time"

"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/gojue/moling/client"
)

var clientCmd = &cobra.Command{
Expand Down
37 changes: 19 additions & 18 deletions cli/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import (
"path/filepath"
"time"

"github.com/gojue/moling/pkg/comm"
"github.com/gojue/moling/pkg/services"
"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/gojue/moling/pkg/comm"
"github.com/gojue/moling/pkg/services"
)

var configCmd = &cobra.Command{
Expand Down Expand Up @@ -58,32 +59,32 @@ func ConfigCommandFunc(command *cobra.Command, args []string) error {
// 当前配置文件检测
hasConfig := false
var nowConfig []byte
nowConfigJson := make(map[string]interface{})
nowConfigJSON := make(map[string]any)
configFilePath := filepath.Join(mlConfig.BasePath, mlConfig.ConfigFile)
if nowConfig, err = os.ReadFile(configFilePath); err == nil {
hasConfig = true
}
if hasConfig {
err = json.Unmarshal(nowConfig, &nowConfigJson)
err = json.Unmarshal(nowConfig, &nowConfigJSON)
if err != nil {
return fmt.Errorf("Error unmarshaling JSON: %v, payload:%s\n", err, string(nowConfig))
return fmt.Errorf("error unmarshaling JSON: %w, payload:%s", err, string(nowConfig))
}
}

bf := bytes.Buffer{}
bf.WriteString("\n{\n")

// 写入GlobalConfig
mlConfigJson, err := json.Marshal(mlConfig)
mlConfigJSON, err := json.Marshal(mlConfig)
if err != nil {
return fmt.Errorf("Error marshaling GlobalConfig: %v\n", err)
return fmt.Errorf("error marshaling GlobalConfig: %w", err)
}
bf.WriteString("\t\"MoLingConfig\":\n")
bf.WriteString(fmt.Sprintf("\t%s,\n", mlConfigJson))
bf.WriteString(fmt.Sprintf("\t%s,\n", mlConfigJSON))
first := true
for srvName, nsv := range services.ServiceList() {
// 获取服务对应的配置
cfg, ok := nowConfigJson[string(srvName)].(map[string]interface{})
cfg, ok := nowConfigJSON[string(srvName)].(map[string]any)

srv, err := nsv(ctx)
if err != nil {
Expand All @@ -93,15 +94,15 @@ func ConfigCommandFunc(command *cobra.Command, args []string) error {
if ok {
err = srv.LoadConfig(cfg)
if err != nil {
return fmt.Errorf("Error loading config for service %s: %v\n", srv.Name(), err)
return fmt.Errorf("error loading config for service %s: %w", srv.Name(), err)
}
} else {
logger.Debug().Str("service", string(srv.Name())).Msg("Service not found in config, using default config")
}
// srv Init
err = srv.Init()
if err != nil {
return fmt.Errorf("Error initializing service %s: %v\n", srv.Name(), err)
return fmt.Errorf("error initializing service %s: %w", srv.Name(), err)
}
if !first {
bf.WriteString(",\n")
Expand All @@ -112,31 +113,31 @@ func ConfigCommandFunc(command *cobra.Command, args []string) error {
}
bf.WriteString("}\n")
// 解析原始 JSON 字符串
var data interface{}
var data any
err = json.Unmarshal(bf.Bytes(), &data)
if err != nil {
return fmt.Errorf("Error unmarshaling JSON: %v, payload:%s\n", err, bf.String())
return fmt.Errorf("error unmarshaling JSON: %w, payload:%s", err, bf.String())
}

// 格式化 JSON
formattedJson, err := json.MarshalIndent(data, "", " ")
formattedJSON, err := json.MarshalIndent(data, "", " ")
if err != nil {
return fmt.Errorf("Error marshaling JSON: %v\n", err)
return fmt.Errorf("error marshaling JSON: %w", err)
}

// 如果不存在配置文件
if !hasConfig {
logger.Info().Msgf("Configuration file %s does not exist. Creating a new one.", configFilePath)
err = os.WriteFile(configFilePath, formattedJson, 0644)
err = os.WriteFile(configFilePath, formattedJSON, 0644)
if err != nil {
return fmt.Errorf("Error writing configuration file: %v\n", err)
return fmt.Errorf("error writing configuration file: %w", err)
}
logger.Info().Msgf("Configuration file %s created successfully.", configFilePath)
}
logger.Info().Str("config", configFilePath).Msg("Current loaded configuration file path")
logger.Info().Msg("You can modify the configuration file to change the settings.")
if !initial {
logger.Info().Msgf("Configuration details: \n%s\n", formattedJson)
logger.Info().Msgf("Configuration details: \n%s\n", formattedJSON)
}
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/perrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ package cmd
import (
"path/filepath"

"github.com/gojue/moling/pkg/utils"
"github.com/spf13/cobra"

"github.com/gojue/moling/pkg/utils"
)

// mlsCommandPreFunc is a pre-run function for the MoLing command.
Expand Down
15 changes: 8 additions & 7 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ import (
"syscall"
"time"

"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/gojue/moling/cli/cobrautl"
"github.com/gojue/moling/pkg/comm"
"github.com/gojue/moling/pkg/config"
"github.com/gojue/moling/pkg/server"
"github.com/gojue/moling/pkg/services"
"github.com/gojue/moling/pkg/services/abstract"
"github.com/gojue/moling/pkg/utils"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
)

const (
Expand Down Expand Up @@ -161,7 +162,7 @@ func initLogger(mlDataPath string) zerolog.Logger {
// 初始化 RotateWriter
rw, err := utils.NewRotateWriter(logFile, MaxLogSize) // 512MB 阈值
if err != nil {
panic(fmt.Sprintf("failed to open log file %s: %v", logFile, err))
panic(fmt.Sprintf("failed to open log file %s: %s", logFile, err.Error()))
}
logger = zerolog.New(rw).With().Timestamp().Logger()
logger.Info().Uint32("MaxLogSize", MaxLogSize).Msgf("Log files are automatically rotated when they exceed the size threshold, and saved to %s.1 and %s.2 respectively", LogFileName, LogFileName)
Expand All @@ -173,7 +174,7 @@ func mlsCommandFunc(command *cobra.Command, args []string) error {
mlConfig.SetLogger(loger)
var err error
var nowConfig []byte
var nowConfigJson map[string]interface{}
var nowConfigJSON map[string]any

// 增加实例重复运行检测
pidFilePath := filepath.Join(mlConfig.BasePath, MLPidName)
Expand All @@ -187,9 +188,9 @@ func mlsCommandFunc(command *cobra.Command, args []string) error {
loger.Info().Str("ServerName", MCPServerName).Str("version", GitVersion).Msg("start")
configFilePath := filepath.Join(mlConfig.BasePath, mlConfig.ConfigFile)
if nowConfig, err = os.ReadFile(configFilePath); err == nil {
err = json.Unmarshal(nowConfig, &nowConfigJson)
err = json.Unmarshal(nowConfig, &nowConfigJSON)
if err != nil {
return fmt.Errorf("Error unmarshaling JSON: %v, config file:%s\n", err, configFilePath)
return fmt.Errorf("error unmarshaling JSON: %w, config file:%s", err, configFilePath)
}
}
loger.Info().Str("config_file", configFilePath).Msg("load config file")
Expand All @@ -211,7 +212,7 @@ func mlsCommandFunc(command *cobra.Command, args []string) error {
}
loger.Debug().Str("moduleName", string(srvName)).Msgf("starting %s service", srvName)
}
cfg, ok := nowConfigJson[string(srvName)].(map[string]interface{})
cfg, ok := nowConfigJSON[string(srvName)].(map[string]any)
srv, err := nsv(ctxNew)
if err != nil {
loger.Error().Err(err).Msgf("failed to create service %s", srv.Name())
Expand Down
14 changes: 7 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ package client
import (
"encoding/json"
"errors"
"github.com/rs/zerolog"
"os"

"github.com/rs/zerolog"
)

var (
Expand All @@ -40,7 +41,7 @@ type MCPServerConfig struct {
IsActive bool `json:"isActive"` // Is the MCP Server active
Command string `json:"command,omitempty"` // Command to start the MCP Server, STDIO mode only
Args []string `json:"args,omitempty"` // Arguments to pass to the command, STDIO mode only
BaseUrl string `json:"baseUrl,omitempty"` // Base URL of the MCP Server, SSE mode only
BaseURL string `json:"baseUrl,omitempty"` // Base URL of the MCP Server, SSE mode only
TimeOut uint16 `json:"timeout,omitempty"` // Timeout for the MCP Server, default is 300 seconds
ServerName string
}
Expand All @@ -52,7 +53,7 @@ func NewMCPServerConfig(description string, command string, srvName string) MCPS
IsActive: true,
Command: command,
Args: []string{"-m", "Browser"},
BaseUrl: "",
BaseURL: "",
ServerName: srvName,
TimeOut: 300,
}
Expand Down Expand Up @@ -118,19 +119,18 @@ func (c *Manager) SetupConfig() {
}
c.logger.Info().Str("Client Name", name).Msgf("Successfully added config to %s", path)
}
return
}

// appendConfig appends the mlMCPConfig to the client config.
func (c *Manager) appendConfig(name string, payload []byte) ([]byte, error) {
var err error
var jsonMap map[string]interface{}
var jsonMap map[string]any
var jsonBytes []byte
err = json.Unmarshal(payload, &jsonMap)
if err != nil {
return nil, err
}
jsonMcpServer, ok := jsonMap[MCPServersKey].(map[string]interface{})
jsonMcpServer, ok := jsonMap[MCPServersKey].(map[string]any)
if !ok {
return nil, errors.New("MCPServersKey not found in JSON")
}
Expand All @@ -151,7 +151,7 @@ func (c *Manager) checkExist(path string) bool {
c.logger.Debug().Msgf("Client config file %s does not exist", path)
return false
}
c.logger.Info().Msgf("check file failed, error:%v", err)
c.logger.Info().Msgf("check file failed, error:%s", err.Error())
return false
}
return true
Expand Down
15 changes: 9 additions & 6 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
package client

import (
"github.com/rs/zerolog"
"os"
"testing"

"github.com/rs/zerolog"
)

func TestClientManager_ListClient(t *testing.T) {
Expand Down Expand Up @@ -76,13 +77,13 @@ func TestClientManager_ListClient(t *testing.T) {

result, err := cm.appendConfig("TestClient", payload)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
t.Fatalf("Expected no error, got %s", err.Error())
}

var resultMap map[string]interface{}
var resultMap map[string]any
err = json.Unmarshal(result, &resultMap)
if err != nil {
t.Fatalf("Expected valid JSON, got error %v", err)
t.Fatalf("Expected valid JSON, got error %s", err.Error())
}

if resultMap["existingKey"] != "existingValue" {
Expand All @@ -106,9 +107,11 @@ func TestClientManager_checkExist(t *testing.T) {
// Test with an existing file
file, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
t.Fatalf("Failed to create temp file: %s", err.Error())
}
defer os.Remove(file.Name())
defer func() {
_ = os.Remove(file.Name())
}()
t.Logf("Created temp file: %s", file.Name())
exists = cm.checkExist(file.Name())
if !exists {
Expand Down
3 changes: 2 additions & 1 deletion pkg/comm/comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (
"os"
"path/filepath"

"github.com/gojue/moling/pkg/config"
"github.com/rs/zerolog"

"github.com/gojue/moling/pkg/config"
)

type MoLingServerType string
Expand Down
Loading
Loading