Skip to content

Commit a550ffb

Browse files
authored
fix #4 invalid data path (#5)
* feat: enhance browser and command execution with improved configuration and data path handling Signed-off-by: cfc4n <cfc4n.cs@gmail.com> * refactor: streamline directory creation and improve configuration access in CommandServer Signed-off-by: cfc4n <cfc4n.cs@gmail.com> --------- Signed-off-by: cfc4n <cfc4n.cs@gmail.com>
1 parent fcc4d2a commit a550ffb

17 files changed

Lines changed: 87 additions & 50 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/services/command_mt.go
1+
/services/mo*
2+

cli/cmd/moling_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestNewMLServer(t *testing.T) {
2626
if err != nil {
2727
t.Errorf("Failed to create base directory: %v", err)
2828
}
29-
for _, dirName := range []string{"logs", "config", "browser"} {
29+
for _, dirName := range mlDirectories {
3030
err = CreateDirectory(filepath.Join(mlConfig.BasePath, dirName))
3131
if err != nil {
3232
t.Errorf("Failed to create directory %s: %v", dirName, err)

cli/cmd/root.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ var (
7171

7272
// mlDirectories is a list of directories to be created in the base path
7373
mlDirectories = []string{
74-
"logs",
75-
"config",
76-
"browser",
77-
"data",
74+
"logs", // log file
75+
"config", // config file
76+
"browser", // browser cache
77+
"data", // data
7878
"cache",
7979
}
8080
)

go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.24.1
44

55
require (
66
github.com/chromedp/chromedp v0.13.3
7-
github.com/mark3labs/mcp-go v0.15.0
7+
github.com/mark3labs/mcp-go v0.17.0
88
github.com/rs/zerolog v1.34.0
99
github.com/spf13/cobra v1.9.1
1010
github.com/spf13/pflag v1.0.6
@@ -13,14 +13,14 @@ require (
1313
require (
1414
github.com/chromedp/cdproto v0.0.0-20250319231242-a755498943c8 // indirect
1515
github.com/chromedp/sysutil v1.1.0 // indirect
16-
github.com/go-json-experiment/json v0.0.0-20250211171154-1ae217ad3535 // indirect
16+
github.com/go-json-experiment/json v0.0.0-20250223041408-d3c622f1b874 // indirect
1717
github.com/gobwas/httphead v0.1.0 // indirect
1818
github.com/gobwas/pool v0.2.1 // indirect
1919
github.com/gobwas/ws v1.4.0 // indirect
2020
github.com/google/uuid v1.6.0 // indirect
2121
github.com/inconshreveable/mousetrap v1.1.0 // indirect
22-
github.com/mattn/go-colorable v0.1.13 // indirect
23-
github.com/mattn/go-isatty v0.0.19 // indirect
22+
github.com/mattn/go-colorable v0.1.14 // indirect
23+
github.com/mattn/go-isatty v0.0.20 // indirect
2424
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
25-
golang.org/x/sys v0.29.0 // indirect
25+
golang.org/x/sys v0.31.0 // indirect
2626
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
1010
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1111
github.com/go-json-experiment/json v0.0.0-20250211171154-1ae217ad3535 h1:yE7argOs92u+sSCRgqqe6eF+cDaVhSPlioy1UkA0p/w=
1212
github.com/go-json-experiment/json v0.0.0-20250211171154-1ae217ad3535/go.mod h1:BWmvoE1Xia34f3l/ibJweyhrT+aROb/FQ6d+37F0e2s=
13+
github.com/go-json-experiment/json v0.0.0-20250223041408-d3c622f1b874 h1:F8d1AJ6M9UQCavhwmO6ZsrYLfG8zVFWfEfMS2MXPkSY=
14+
github.com/go-json-experiment/json v0.0.0-20250223041408-d3c622f1b874/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
1315
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
1416
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
1517
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
@@ -25,11 +27,17 @@ github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kUL
2527
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=
2628
github.com/mark3labs/mcp-go v0.15.0 h1:lViiC4dk6chJHZccezaTzZLMOQVUXJDGNQPtzExr5NQ=
2729
github.com/mark3labs/mcp-go v0.15.0/go.mod h1:xBB350hekQsJAK7gJAii8bcEoWemboLm2mRm5/+KBaU=
30+
github.com/mark3labs/mcp-go v0.17.0 h1:5Ps6T7qXr7De/2QTqs9h6BKeZ/qdeUeGrgM5lPzi930=
31+
github.com/mark3labs/mcp-go v0.17.0/go.mod h1:KmJndYv7GIgcPVwEKJjNcbhVQ+hJGJhrCCB/9xITzpE=
2832
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
2933
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
34+
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
35+
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
3036
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
3137
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
3238
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
39+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
40+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
3341
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde h1:x0TT0RDC7UhAVbbWWBzr41ElhJx5tXPWkIHA2HWPRuw=
3442
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
3543
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -52,6 +60,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5260
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5361
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
5462
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
63+
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
64+
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
5565
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5666
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
5767
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

services/browser.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ type BrowserServer struct {
4646
func NewBrowserServer(ctx context.Context, args []string) (Service, error) {
4747

4848
bc := NewBrowserConfig()
49+
globalConf := ctx.Value(MoLingConfigKey).(*MoLingConfig)
50+
userDataDir := filepath.Join(globalConf.BasePath, DataPath)
51+
52+
bc.DataPath = filepath.Join(globalConf.BasePath, "data")
4953
logger, ok := ctx.Value(MoLingLoggerKey).(zerolog.Logger)
5054
if !ok {
5155
return nil, fmt.Errorf("BrowserServer: invalid logger type: %T", ctx.Value(MoLingLoggerKey))
@@ -59,8 +63,7 @@ func NewBrowserServer(ctx context.Context, args []string) (Service, error) {
5963
config: bc,
6064
}
6165
bs.logger = logger.Hook(loggerNameHook)
62-
globalConf := ctx.Value(MoLingConfigKey).(*MoLingConfig)
63-
userDataDir := filepath.Join(globalConf.BasePath, DataPath)
66+
6467
err := bs.initBrowser(userDataDir)
6568
if err != nil {
6669
return nil, fmt.Errorf("failed to initialize browser: %v", err)

services/browser_config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package services
1919

2020
import (
2121
"fmt"
22+
"os"
23+
"path/filepath"
2224
)
2325

2426
type BrowserConfig struct {
@@ -55,6 +57,6 @@ func NewBrowserConfig() *BrowserConfig {
5557
CSSTimeout: 10,
5658
UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3 (GoJue MoLing)",
5759
DefaultLanguage: "en-US",
58-
DataPath: "/Users/cfc4n/Downloads/moling_data",
60+
DataPath: filepath.Join(os.TempDir(), ".moling", "data"),
5961
}
6062
}

services/command.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ var (
3737
// CommandServer implements the Service interface and provides methods to execute named commands.
3838
type CommandServer struct {
3939
MLService
40-
config *CommandConfig
41-
globalConfig *MoLingConfig
42-
osName string
43-
osVersion string
40+
config *CommandConfig
41+
osName string
42+
osVersion string
4443
}
4544

4645
// NewCommandServer creates a new CommandServer with the given allowed commands.
@@ -63,11 +62,11 @@ func NewCommandServer(ctx context.Context, args []string) (Service, error) {
6362

6463
cs := &CommandServer{
6564
MLService: MLService{
66-
ctx: ctx,
67-
logger: lger.Hook(loggerNameHook),
65+
ctx: ctx,
66+
logger: lger.Hook(loggerNameHook),
67+
mlConfig: gConf,
6868
},
69-
config: cc,
70-
globalConfig: gConf,
69+
config: cc,
7170
}
7271

7372
err = cs.init()
@@ -104,7 +103,7 @@ func (cs *CommandServer) handlePrompt(ctx context.Context, request mcp.GetPrompt
104103
Role: mcp.RoleUser,
105104
Content: mcp.TextContent{
106105
Type: "text",
107-
Text: "This is a simple prompt without arguments.",
106+
Text: fmt.Sprintf("You are a command-line tool assistant, using %s system commands to help users troubleshoot network issues, system performance, among other things.", cs.MlConfig().SystemInfo),
108107
},
109108
},
110109
},
@@ -120,7 +119,7 @@ func (cs *CommandServer) handleExecuteCommand(ctx context.Context, request mcp.C
120119

121120
// Check if the command is allowed
122121
if !cs.isAllowedCommand(command) {
123-
cs.logger.Err(ErrCommandNotAllowed).Str("command", command).Msgf("If you want to allow this command, add it to %s", filepath.Join(cs.globalConfig.BasePath, "config", cs.globalConfig.ConfigFile))
122+
cs.logger.Err(ErrCommandNotAllowed).Str("command", command).Msgf("If you want to allow this command, add it to %s", filepath.Join(cs.MlConfig().BasePath, "config", cs.MlConfig().ConfigFile))
124123
return &mcp.CallToolResult{
125124
Content: []mcp.Content{
126125
mcp.TextContent{
@@ -133,7 +132,7 @@ func (cs *CommandServer) handleExecuteCommand(ctx context.Context, request mcp.C
133132
}
134133

135134
// Execute the command
136-
output, err := cs.executeCommand(command)
135+
output, err := ExecCommand(command)
137136
if err != nil {
138137
return &mcp.CallToolResult{
139138
Content: []mcp.Content{

services/command_config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var (
3434
"iostat", "mpstat", "sar", "uptime", "cut", "sort", "uniq", "wc", "awk", "sed",
3535
"diff", "cmp", "comm", "file", "basename", "dirname", "chmod", "chown", "curl",
3636
"nslookup", "dig", "host", "ssh", "scp", "sftp", "ftp", "wget", "tar", "gzip",
37+
"scutil", "networksetup",
3738
}
3839
)
3940

services/command_exec.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import (
2525
"time"
2626
)
2727

28-
// executeCommand executes a command and returns its output.
29-
func (cs *CommandServer) executeCommand(command string) (string, error) {
28+
// ExecCommand executes a command and returns its output.
29+
func ExecCommand(command string) (string, error) {
3030
var cmd *exec.Cmd
31-
ctx, cfunc := context.WithTimeout(context.Background(), time.Second*3)
31+
ctx, cfunc := context.WithTimeout(context.Background(), time.Second*10)
3232
defer cfunc()
3333
cmd = exec.CommandContext(ctx, "sh", "-c", command)
3434
output, err := cmd.CombinedOutput()

0 commit comments

Comments
 (0)