Skip to content

Commit e395bc5

Browse files
committed
Add GetLogDir method to ServerInterface and implement log directory handling
- Introduced GetLogDir method in ServerInterface for retrieving the log directory path. - Implemented GetLogDir in Server to return configured log directory or default paths. - Updated tray application to include functionality for opening the logs directory. - Enhanced error handling and logging for directory access operations.
1 parent c6072be commit e395bc5

4 files changed

Lines changed: 57 additions & 9 deletions

File tree

internal/server/server.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"mcpproxy-go/internal/cache"
1515
"mcpproxy-go/internal/config"
1616
"mcpproxy-go/internal/index"
17+
"mcpproxy-go/internal/logs"
1718
"mcpproxy-go/internal/storage"
1819
"mcpproxy-go/internal/truncate"
1920
"mcpproxy-go/internal/upstream"
@@ -1040,3 +1041,16 @@ func (s *Server) cleanupOrphanedIndexEntries() {
10401041
func (s *Server) GetConfigPath() string {
10411042
return config.GetConfigPath(s.config.DataDir)
10421043
}
1044+
1045+
// GetLogDir returns the log directory path for tray UI
1046+
func (s *Server) GetLogDir() string {
1047+
if s.config.Logging != nil && s.config.Logging.LogDir != "" {
1048+
return s.config.Logging.LogDir
1049+
}
1050+
// Return OS-specific default log directory if not configured
1051+
if defaultLogDir, err := logs.GetLogDir(); err == nil {
1052+
return defaultLogDir
1053+
}
1054+
// Last resort fallback to data directory
1055+
return s.config.DataDir
1056+
}

internal/tray/tray.go

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"net/http"
1515
"os"
1616
"os/exec"
17+
"path/filepath"
1718
"runtime"
1819
"strings"
1920
"time"
@@ -66,6 +67,7 @@ type ServerInterface interface {
6667
// Config management for file watching
6768
ReloadConfiguration() error
6869
GetConfigPath() string
70+
GetLogDir() string
6971
}
7072

7173
// App represents the system tray application
@@ -321,7 +323,8 @@ func (a *App) onReady() {
321323

322324
// --- Other Menu Items ---
323325
updateItem := systray.AddMenuItem("Check for Updates...", "Check for a new version of the proxy")
324-
openConfigItem := systray.AddMenuItem("Open Config", "Open the configuration file")
326+
openConfigItem := systray.AddMenuItem("Open config dir", "Open the configuration directory")
327+
openLogsItem := systray.AddMenuItem("Open logs dir", "Open the logs directory")
325328
systray.AddSeparator()
326329

327330
// --- Autostart Menu Item (macOS only) ---
@@ -351,7 +354,9 @@ func (a *App) onReady() {
351354
case <-updateItem.ClickedCh:
352355
go a.checkForUpdates()
353356
case <-openConfigItem.ClickedCh:
354-
a.openConfig()
357+
a.openConfigDir()
358+
case <-openLogsItem.ClickedCh:
359+
a.openLogsDir()
355360
case <-quitItem.ClickedCh:
356361
a.logger.Info("Quit item clicked, shutting down")
357362
if a.shutdown != nil {
@@ -927,28 +932,52 @@ func (a *App) applyTarGzUpdate(body io.Reader) error {
927932
return fmt.Errorf("no mcpproxy binary found in tar.gz archive")
928933
}
929934

930-
// openConfig opens the configuration file in the default editor
931-
func (a *App) openConfig() {
935+
// openConfigDir opens the directory containing the configuration file
936+
func (a *App) openConfigDir() {
932937
if a.configPath == "" {
933938
a.logger.Warn("Config path is not set, cannot open")
934939
return
935940
}
936941

942+
configDir := filepath.Dir(a.configPath)
943+
a.openDirectory(configDir, "config directory")
944+
}
945+
946+
// openLogsDir opens the logs directory
947+
func (a *App) openLogsDir() {
948+
if a.server == nil {
949+
a.logger.Warn("Server interface not available, cannot open logs directory")
950+
return
951+
}
952+
953+
logDir := a.server.GetLogDir()
954+
if logDir == "" {
955+
a.logger.Warn("Log directory path is not set, cannot open")
956+
return
957+
}
958+
959+
a.openDirectory(logDir, "logs directory")
960+
}
961+
962+
// openDirectory opens a directory using the OS-specific file manager
963+
func (a *App) openDirectory(dirPath, dirType string) {
937964
var cmd *exec.Cmd
938965
switch runtime.GOOS {
939966
case "darwin":
940-
cmd = exec.Command("open", a.configPath)
967+
cmd = exec.Command("open", dirPath)
941968
case "linux":
942-
cmd = exec.Command("xdg-open", a.configPath)
969+
cmd = exec.Command("xdg-open", dirPath)
943970
case "windows":
944-
cmd = exec.Command("cmd", "/C", "start", a.configPath)
971+
cmd = exec.Command("explorer", dirPath)
945972
default:
946-
a.logger.Warn("Unsupported OS for opening config file", zap.String("os", runtime.GOOS))
973+
a.logger.Warn("Unsupported OS for opening directory", zap.String("os", runtime.GOOS))
947974
return
948975
}
949976

950977
if err := cmd.Run(); err != nil {
951-
a.logger.Error("Failed to open config file", zap.Error(err))
978+
a.logger.Error("Failed to open directory", zap.Error(err), zap.String("dir_type", dirType), zap.String("path", dirPath))
979+
} else {
980+
a.logger.Info("Successfully opened directory", zap.String("dir_type", dirType), zap.String("path", dirPath))
952981
}
953982
}
954983

internal/tray/tray_stub.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type ServerInterface interface {
3030
// Config management for file watching
3131
ReloadConfiguration() error
3232
GetConfigPath() string
33+
GetLogDir() string
3334
}
3435

3536
// App represents the system tray application (stub version)

internal/tray/tray_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ func (m *MockServerInterface) GetConfigPath() string {
136136
return m.configPath
137137
}
138138

139+
func (m *MockServerInterface) GetLogDir() string {
140+
return "/test/logs"
141+
}
142+
139143
// Helper methods for testing
140144
func (m *MockServerInterface) AddServer(name, url string, enabled, quarantined bool) {
141145
server := map[string]interface{}{

0 commit comments

Comments
 (0)