Skip to content

Commit f5e0d69

Browse files
committed
Centralize logging utilities
- Create pkg/util/log.go with centralized LogVerbose and IsVerbose functions - Replace all duplicate logVerbose functions across the codebase with util.LogVerbose - Remove duplicate isVerbose functions - Add util import to all files that use verbose logging - Maintain consistent [VERBOSE] prefix for all verbose log messages - Check MVX_VERBOSE environment variable in a single location
1 parent 309899c commit f5e0d69

File tree

11 files changed

+122
-118
lines changed

11 files changed

+122
-118
lines changed

pkg/executor/executor.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@ import (
1111
"github.com/gnodet/mvx/pkg/config"
1212
"github.com/gnodet/mvx/pkg/shell"
1313
"github.com/gnodet/mvx/pkg/tools"
14+
"github.com/gnodet/mvx/pkg/util"
1415
)
1516

16-
// logVerbose prints verbose log messages
17-
func logVerbose(format string, args ...interface{}) {
18-
if os.Getenv("MVX_VERBOSE") == "true" {
19-
fmt.Printf("[VERBOSE] "+format+"\n", args...)
20-
}
21-
}
22-
2317
// Executor handles command execution with proper environment setup
2418
type Executor struct {
2519
config *config.Config
@@ -166,19 +160,19 @@ func (e *Executor) setupEnvironment(cmdConfig config.CommandConfig) ([]string, e
166160
requiredTools = append(requiredTools, toolName)
167161
}
168162
}
169-
logVerbose("Required tools for command: %v", requiredTools)
163+
util.LogVerbose("Required tools for command: %v", requiredTools)
170164

171165
// Add tool bin directories to PATH
172166
for _, toolName := range requiredTools {
173167
if toolConfig, exists := e.config.Tools[toolName]; exists {
174168
// EnsureTool handles version resolution, installation check, auto-install, and path retrieval
175169
binPath, err := e.toolManager.EnsureTool(toolName, toolConfig)
176170
if err != nil {
177-
logVerbose("Skipping tool %s: %v", toolName, err)
171+
util.LogVerbose("Skipping tool %s: %v", toolName, err)
178172
continue
179173
}
180174

181-
logVerbose("Adding %s bin path to PATH: %s", toolName, binPath)
175+
util.LogVerbose("Adding %s bin path to PATH: %s", toolName, binPath)
182176
pathDirs = append(pathDirs, binPath)
183177
}
184178
}
@@ -191,9 +185,9 @@ func (e *Executor) setupEnvironment(cmdConfig config.CommandConfig) ([]string, e
191185
newPath = newPath + string(os.PathListSeparator) + currentPath
192186
}
193187
envVars["PATH"] = newPath
194-
logVerbose("Updated PATH with %d tool directories: %s", len(pathDirs), newPath)
188+
util.LogVerbose("Updated PATH with %d tool directories: %s", len(pathDirs), newPath)
195189
} else {
196-
logVerbose("No tool directories added to PATH")
190+
util.LogVerbose("No tool directories added to PATH")
197191
}
198192

199193
// Convert environment map back to slice format
@@ -219,11 +213,11 @@ func (e *Executor) processScriptString(script string, args []string) string {
219213

220214
// executeScriptWithInterpreter executes a script using the specified interpreter
221215
func (e *Executor) executeScriptWithInterpreter(script, workDir string, env []string, interpreter string) error {
222-
logVerbose("executeScriptWithInterpreter called with interpreter: '%s', script: '%s'", interpreter, script)
216+
util.LogVerbose("executeScriptWithInterpreter called with interpreter: '%s', script: '%s'", interpreter, script)
223217

224218
// Default to native interpreter if not specified
225219
if interpreter == "" || interpreter == "native" {
226-
logVerbose("Using native interpreter")
220+
util.LogVerbose("Using native interpreter")
227221
return e.executeNativeScript(script, workDir, env)
228222
}
229223

@@ -247,14 +241,14 @@ func (e *Executor) executeNativeScript(script, workDir string, env []string) err
247241
shellArgs = []string{"/c"}
248242
}
249243

250-
logVerbose("Executing native script: %s", script)
251-
logVerbose("Working directory: %s", workDir)
252-
logVerbose("Environment variables count: %d", len(env))
244+
util.LogVerbose("Executing native script: %s", script)
245+
util.LogVerbose("Working directory: %s", workDir)
246+
util.LogVerbose("Environment variables count: %d", len(env))
253247

254248
// Log PATH specifically
255249
for _, envVar := range env {
256250
if strings.HasPrefix(envVar, "PATH=") {
257-
logVerbose("PATH in environment: %s", envVar)
251+
util.LogVerbose("PATH in environment: %s", envVar)
258252
break
259253
}
260254
}

pkg/shell/shell.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"path/filepath"
88
"runtime"
99
"strings"
10+
11+
"github.com/gnodet/mvx/pkg/util"
1012
)
1113

1214
// MVXShell provides cross-platform command execution
@@ -629,18 +631,11 @@ func (s *MVXShell) open(args []string) error {
629631
return cmd.Run()
630632
}
631633

632-
// logVerbose prints verbose log messages for mvx-shell
633-
func logVerbose(format string, args ...interface{}) {
634-
if os.Getenv("MVX_VERBOSE") == "true" {
635-
fmt.Printf("[VERBOSE] "+format+"\n", args...)
636-
}
637-
}
638-
639634
// executeExternal executes an external command
640635
func (s *MVXShell) executeExternal(cmd Command) error {
641-
logVerbose("mvx-shell executing external command: %s %v", cmd.Name, cmd.Args)
642-
logVerbose("mvx-shell working directory: %s", s.workDir)
643-
logVerbose("mvx-shell environment variables count: %d", len(s.env))
636+
util.LogVerbose("mvx-shell executing external command: %s %v", cmd.Name, cmd.Args)
637+
util.LogVerbose("mvx-shell working directory: %s", s.workDir)
638+
util.LogVerbose("mvx-shell environment variables count: %d", len(s.env))
644639

645640
execCmd := exec.Command(cmd.Name, cmd.Args...)
646641
execCmd.Dir = s.workDir
@@ -675,7 +670,7 @@ func (s *MVXShell) executeExternal(cmd Command) error {
675670
// Log PATH specifically
676671
for _, envVar := range env {
677672
if strings.HasPrefix(envVar, "PATH=") {
678-
logVerbose("mvx-shell PATH in environment: %s", envVar)
673+
util.LogVerbose("mvx-shell PATH in environment: %s", envVar)
679674
break
680675
}
681676
}

pkg/tools/base_tool.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"sync"
1212

1313
"github.com/gnodet/mvx/pkg/config"
14+
"github.com/gnodet/mvx/pkg/util"
1415
"github.com/gnodet/mvx/pkg/version"
1516
)
1617

@@ -261,7 +262,7 @@ func (b *BaseTool) VerifyWithConfig(version string, cfg config.ToolConfig, verif
261262
// Set up environment for verification (needed for tools like Maven that depend on Java)
262263
env, err := b.setupVerificationEnvironment(cfg)
263264
if err != nil {
264-
logVerbose("Failed to setup verification environment: %v", err)
265+
util.LogVerbose("Failed to setup verification environment: %v", err)
265266
env = nil // Fall back to default environment
266267
}
267268

@@ -287,7 +288,7 @@ func (b *BaseTool) setupVerificationEnvironment(cfg config.ToolConfig) ([]string
287288
if tool, err := b.manager.GetTool(b.toolName); err == nil {
288289
if depProvider, ok := tool.(DependencyProvider); ok {
289290
dependencies := depProvider.GetDependencies()
290-
logVerbose("Setting up dependencies for %s verification: %v", b.toolName, dependencies)
291+
util.LogVerbose("Setting up dependencies for %s verification: %v", b.toolName, dependencies)
291292

292293
// Add dependencies to the temporary config
293294
for _, depName := range dependencies {
@@ -300,7 +301,7 @@ func (b *BaseTool) setupVerificationEnvironment(cfg config.ToolConfig) ([]string
300301
Distribution: installedVersions[0].Distribution,
301302
}
302303
tempConfig.Tools[depName] = depConfig
303-
logVerbose("Added dependency %s %s to verification environment", depName, installedVersions[0].Version)
304+
util.LogVerbose("Added dependency %s %s to verification environment", depName, installedVersions[0].Version)
304305
}
305306
}
306307
}
@@ -342,15 +343,15 @@ func (b *BaseTool) setupVerificationEnvironment(cfg config.ToolConfig) ([]string
342343
func (b *BaseTool) SetupHomeEnvironment(version string, cfg config.ToolConfig, envVars map[string]string, envVarName string, getPath func(string, config.ToolConfig) (string, error)) error {
343344
binPath, err := getPath(version, cfg)
344345
if err != nil {
345-
logVerbose("Could not determine %s for %s %s: %v", envVarName, b.toolName, version, err)
346+
util.LogVerbose("Could not determine %s for %s %s: %v", envVarName, b.toolName, version, err)
346347
return nil
347348
}
348349

349350
// *_HOME should point to the installation directory, not the bin directory
350351
if strings.HasSuffix(binPath, "/bin") {
351352
homeDir := strings.TrimSuffix(binPath, "/bin")
352353
envVars[envVarName] = homeDir
353-
logVerbose("Set %s=%s for %s %s", envVarName, homeDir, b.toolName, version)
354+
util.LogVerbose("Set %s=%s for %s %s", envVarName, homeDir, b.toolName, version)
354355
}
355356

356357
return nil
@@ -544,7 +545,7 @@ func (b *BaseTool) getDownloadOptions() DownloadOptions {
544545
func (b *BaseTool) StandardInstall(version string, cfg config.ToolConfig, getDownloadURL func(string) string) error {
545546
// Check if we should use system tool instead of downloading
546547
if UseSystemTool(b.toolName) {
547-
logVerbose("%s=true, forcing use of system %s", getSystemToolEnvVar(b.toolName), b.toolName)
548+
util.LogVerbose("%s=true, forcing use of system %s", getSystemToolEnvVar(b.toolName), b.toolName)
548549

549550
// Try primary binary name in PATH
550551
if toolPath, err := exec.LookPath(b.binaryName); err == nil {
@@ -665,29 +666,29 @@ func (b *BaseTool) ListInstalledVersions(distribution string) []InstalledVersion
665666
func (b *BaseTool) StandardIsInstalled(versionSpec string, cfg config.ToolConfig, getPath func(string, config.ToolConfig) (string, error)) bool {
666667
if UseSystemTool(b.toolName) {
667668
if _, err := exec.LookPath(b.GetBinaryName()); err == nil {
668-
logVerbose("System %s is available in PATH (MVX_USE_SYSTEM_%s=true)", b.toolName, strings.ToUpper(b.toolName))
669+
util.LogVerbose("System %s is available in PATH (MVX_USE_SYSTEM_%s=true)", b.toolName, strings.ToUpper(b.toolName))
669670
return true
670671
}
671672

672-
logVerbose("System %s not available: not found in environment variables or PATH", b.toolName)
673+
util.LogVerbose("System %s not available: not found in environment variables or PATH", b.toolName)
673674
return false
674675
}
675676

676677
tool, err := b.manager.GetTool(b.toolName)
677678
if err != nil {
678-
logVerbose("Failed to get tool %s: %v", b.toolName, err)
679+
util.LogVerbose("Failed to get tool %s: %v", b.toolName, err)
679680
return false
680681
}
681682

682683
spec, err := version.ParseSpec(versionSpec)
683684
if err != nil {
684-
logVerbose("Failed to parse version spec %q: %v", versionSpec, err)
685+
util.LogVerbose("Failed to parse version spec %q: %v", versionSpec, err)
685686
return false
686687
}
687688

688689
targetVersion, resolveErr := b.resolveTargetVersion(tool, spec, versionSpec, cfg)
689690
if resolveErr != nil {
690-
logVerbose("Failed to resolve target version for %s %s: %v", b.toolName, versionSpec, resolveErr)
691+
util.LogVerbose("Failed to resolve target version for %s %s: %v", b.toolName, versionSpec, resolveErr)
691692
}
692693

693694
installed := b.ListInstalledVersions(cfg.Distribution)
@@ -700,7 +701,7 @@ func (b *BaseTool) StandardIsInstalled(versionSpec string, cfg config.ToolConfig
700701
for _, inst := range installed {
701702
parsed, err := version.ParseVersion(inst.Version)
702703
if err != nil {
703-
logVerbose("Failed to parse installed version %s: %v", inst.Version, err)
704+
util.LogVerbose("Failed to parse installed version %s: %v", inst.Version, err)
704705
continue
705706
}
706707
if spec.Matches(parsed) {
@@ -718,7 +719,7 @@ func (b *BaseTool) StandardIsInstalled(versionSpec string, cfg config.ToolConfig
718719

719720
// Check candidates in order, return immediately on first valid installation
720721
for _, candidate := range candidates {
721-
logVerbose(" Checking installed version: %s (%s) at %s", candidate.info.Version, candidate.info.Distribution, candidate.info.Path)
722+
util.LogVerbose(" Checking installed version: %s (%s) at %s", candidate.info.Version, candidate.info.Distribution, candidate.info.Path)
722723

723724
candidateCfg := cfg
724725
candidateCfg.Version = candidate.info.Version
@@ -728,12 +729,12 @@ func (b *BaseTool) StandardIsInstalled(versionSpec string, cfg config.ToolConfig
728729

729730
binPath, err := getPath(candidate.info.Version, candidateCfg)
730731
if err != nil {
731-
logVerbose("Failed to compute binary path for %s %s: %v", b.toolName, candidate.info.Version, err)
732+
util.LogVerbose("Failed to compute binary path for %s %s: %v", b.toolName, candidate.info.Version, err)
732733
continue
733734
}
734735

735736
if !b.IsInstalled(binPath) {
736-
logVerbose("Binary for %s %s not found at %s", b.toolName, candidate.info.Version, binPath)
737+
util.LogVerbose("Binary for %s %s not found at %s", b.toolName, candidate.info.Version, binPath)
737738
continue
738739
}
739740

@@ -742,7 +743,7 @@ func (b *BaseTool) StandardIsInstalled(versionSpec string, cfg config.ToolConfig
742743
// This avoids running "java -version", "mvn --version", etc. on every startup
743744

744745
// Found a valid installation - return immediately
745-
logVerbose("Using previously installed %s %s (%s)", b.toolName, candidate.info.Version, candidate.info.Distribution)
746+
util.LogVerbose("Using previously installed %s %s (%s)", b.toolName, candidate.info.Version, candidate.info.Distribution)
746747
return true
747748
}
748749

@@ -751,26 +752,26 @@ func (b *BaseTool) StandardIsInstalled(versionSpec string, cfg config.ToolConfig
751752
}
752753

753754
if targetVersion == "" {
754-
logVerbose("Resolved target version for %s %s is empty", b.toolName, versionSpec)
755+
util.LogVerbose("Resolved target version for %s %s is empty", b.toolName, versionSpec)
755756
return false
756757
}
757758

758759
installCfg := cfg
759760
installCfg.Version = targetVersion
760761

761-
logVerbose("%s version %s not installed, attempting automatic installation", b.toolName, targetVersion)
762+
util.LogVerbose("%s version %s not installed, attempting automatic installation", b.toolName, targetVersion)
762763
if err := tool.Install(targetVersion, installCfg); err != nil {
763-
logVerbose("Automatic installation of %s %s failed: %v", b.toolName, targetVersion, err)
764+
util.LogVerbose("Automatic installation of %s %s failed: %v", b.toolName, targetVersion, err)
764765
return false
765766
}
766767

767768
if err := tool.Verify(targetVersion, installCfg); err != nil {
768-
logVerbose("Verification after installing %s %s failed: %v", b.toolName, targetVersion, err)
769+
util.LogVerbose("Verification after installing %s %s failed: %v", b.toolName, targetVersion, err)
769770
return false
770771
}
771772

772773
b.clearPathCache()
773-
logVerbose("Successfully installed %s %s on demand", b.toolName, targetVersion)
774+
util.LogVerbose("Successfully installed %s %s on demand", b.toolName, targetVersion)
774775
return true
775776
}
776777

@@ -817,7 +818,7 @@ func (b *BaseTool) StandardGetPath(version string, cfg config.ToolConfig, getIns
817818
if UseSystemTool(b.toolName) {
818819
// Try primary binary name in PATH
819820
if _, err := exec.LookPath(b.GetBinaryName()); err == nil {
820-
logVerbose("Using system %s from PATH (MVX_USE_SYSTEM_%s=true)", b.toolName, strings.ToUpper(b.toolName))
821+
util.LogVerbose("Using system %s from PATH (MVX_USE_SYSTEM_%s=true)", b.toolName, strings.ToUpper(b.toolName))
821822
b.setCachedPath(cacheKey, "", nil)
822823
return "", nil
823824
}

pkg/tools/download.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/gnodet/mvx/pkg/config"
16+
"github.com/gnodet/mvx/pkg/util"
1617
)
1718

1819
// DownloadConfig contains configuration for robust downloads with checksum verification
@@ -72,7 +73,7 @@ func RobustDownload(config *DownloadConfig) (*DownloadResult, error) {
7273
originalURL := config.URL
7374
urlReplacer, err := LoadURLReplacer()
7475
if err != nil {
75-
logVerbose("Warning: failed to load URL replacements: %v", err)
76+
util.LogVerbose("Warning: failed to load URL replacements: %v", err)
7677
} else {
7778
config.URL = urlReplacer.ApplyReplacements(config.URL)
7879
if config.URL != originalURL {

pkg/tools/extraction.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"os/exec"
1111
"path/filepath"
1212
"strings"
13+
14+
"github.com/gnodet/mvx/pkg/util"
1315
)
1416

1517
// detectSingleTopLevelDirectory checks if all files in a ZIP archive are under a single top-level directory
@@ -295,7 +297,7 @@ func extractTarGzFile(src, dest string) error {
295297
}
296298
default:
297299
// Skip other file types (char devices, block devices, etc.)
298-
logVerbose("Skipping unsupported file type %d for %s", header.Typeflag, header.Name)
300+
util.LogVerbose("Skipping unsupported file type %d for %s", header.Typeflag, header.Name)
299301
}
300302
}
301303

@@ -334,14 +336,14 @@ func createSymlinkSafely(linkname, targetPath string) error {
334336
if existingLink, err := os.Readlink(targetPath); err == nil {
335337
if existingLink == linkname {
336338
// Already the correct symlink, nothing to do
337-
logVerbose("Symlink %s already exists with correct target %s", targetPath, linkname)
339+
util.LogVerbose("Symlink %s already exists with correct target %s", targetPath, linkname)
338340
return nil
339341
}
340342
// Different symlink target, remove and recreate
341-
logVerbose("Removing existing symlink %s (target: %s) to create new one (target: %s)", targetPath, existingLink, linkname)
343+
util.LogVerbose("Removing existing symlink %s (target: %s) to create new one (target: %s)", targetPath, existingLink, linkname)
342344
} else {
343345
// Not a symlink, but some other file/directory exists
344-
logVerbose("Removing existing file/directory %s to create symlink (target: %s)", targetPath, linkname)
346+
util.LogVerbose("Removing existing file/directory %s to create symlink (target: %s)", targetPath, linkname)
345347
}
346348

347349
// Remove existing file/symlink/directory

0 commit comments

Comments
 (0)