fix: optimize report command to avoid creating timestamp directories

- Implement lazy loading for directory creation in config.go
- Add logFile parameter to InitLogger for better control
- Use dynamic directory existence check instead of flags
- Report command now uses console-only logging to prevent directory creation
- Support both JSON and colorized console output formats
- Maintain backward compatibility for all other commands

Changes:
- config.go: Convert directory paths to getter methods with lazy creation
- logger.go: Add logFile parameter and improve logging control
- cmd/root.go: Detect report command and disable file logging
- uixt/*: Update all references to use new getter methods

Fixes the issue where 'hrp report results/' would create unwanted timestamp directories
This commit is contained in:
lilong.129
2025-06-10 12:06:08 +08:00
parent 6588d95154
commit f5f6d177ab
9 changed files with 84 additions and 36 deletions

View File

@@ -21,12 +21,13 @@ const (
type Config struct {
RootDir string
ResultsDir string
ResultsPath string
DownloadsPath string
ScreenShotsPath string
resultsPath string
downloadsPath string
screenShotsPath string
StartTime time.Time
ActionLogFilePath string
DeviceActionLogFilePath string
mu sync.Mutex
}
var (
@@ -48,25 +49,58 @@ func GetConfig() *Config {
startTimeStr := cfg.StartTime.Format("20060102150405")
cfg.ResultsDir = filepath.Join(ResultsDirName, startTimeStr)
cfg.ResultsPath = filepath.Join(cfg.RootDir, cfg.ResultsDir)
cfg.DownloadsPath = filepath.Join(cfg.RootDir, filepath.Join(DownloadsDirName, startTimeStr))
cfg.ScreenShotsPath = filepath.Join(cfg.ResultsPath, ScreenshotsDirName)
cfg.resultsPath = filepath.Join(cfg.RootDir, cfg.ResultsDir)
cfg.downloadsPath = filepath.Join(cfg.RootDir, filepath.Join(DownloadsDirName, startTimeStr))
cfg.screenShotsPath = filepath.Join(cfg.resultsPath, ScreenshotsDirName)
cfg.ActionLogFilePath = filepath.Join(cfg.ResultsDir, ActionLogDirName)
cfg.DeviceActionLogFilePath = "/sdcard/Android/data/io.appium.uiautomator2.server/files/hodor"
// create results directory
if err := builtin.EnsureFolderExists(cfg.ResultsPath); err != nil {
log.Fatal().Err(err).Msg("create results directory failed")
}
if err := builtin.EnsureFolderExists(cfg.DownloadsPath); err != nil {
log.Fatal().Err(err).Msg("create downloads directory failed")
}
if err := builtin.EnsureFolderExists(cfg.ScreenShotsPath); err != nil {
log.Fatal().Err(err).Msg("create screenshots directory failed")
}
globalConfig = cfg
})
return globalConfig
}
// ResultsPath returns the results path and creates the directory if it doesn't exist
func (c *Config) ResultsPath() string {
c.mu.Lock()
defer c.mu.Unlock()
// Check if directory exists, create if it doesn't
if _, err := os.Stat(c.resultsPath); os.IsNotExist(err) {
if err := builtin.EnsureFolderExists(c.resultsPath); err != nil {
log.Error().Err(err).Str("path", c.resultsPath).Msg("failed to create results directory")
} else {
log.Info().Str("path", c.resultsPath).Msg("create folder")
}
}
return c.resultsPath
}
// DownloadsPath returns the downloads path and creates the directory if it doesn't exist
func (c *Config) DownloadsPath() string {
c.mu.Lock()
defer c.mu.Unlock()
// Check if directory exists, create if it doesn't
if _, err := os.Stat(c.downloadsPath); os.IsNotExist(err) {
if err := builtin.EnsureFolderExists(c.downloadsPath); err != nil {
log.Error().Err(err).Str("path", c.downloadsPath).Msg("failed to create downloads directory")
}
}
return c.downloadsPath
}
// ScreenShotsPath returns the screenshots path and creates the directory if it doesn't exist
func (c *Config) ScreenShotsPath() string {
c.mu.Lock()
defer c.mu.Unlock()
// Check if directory exists, create if it doesn't
if _, err := os.Stat(c.screenShotsPath); os.IsNotExist(err) {
if err := builtin.EnsureFolderExists(c.screenShotsPath); err != nil {
log.Error().Err(err).Str("path", c.screenShotsPath).Msg("failed to create screenshots directory")
}
}
return c.screenShotsPath
}

View File

@@ -1 +1 @@
v5.0.0-beta-2506101103
v5.0.0-beta-2506101206