Skip to content

Commit b39efb1

Browse files
committed
fix: update for codestyle
1 parent 7a4a350 commit b39efb1

3 files changed

Lines changed: 68 additions & 58 deletions

File tree

cmd/ggo/use/use.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -502,19 +502,19 @@ func outputEvalCommandsUnix(config *studio.GPUEnvConfig, envResult *studio.GPUEn
502502
script.WriteString("export _GGO_ORIG_LD_LIBRARY_PATH=\"$LD_LIBRARY_PATH\"\n")
503503
script.WriteString("export _GGO_ORIG_LD_PRELOAD=\"$LD_PRELOAD\"\n")
504504
script.WriteString("export _GGO_ORIG_PATH=\"$PATH\"\n")
505-
script.WriteString(fmt.Sprintf("export _GGO_CLEAN_FILE=\"%s\"\n", cleanFile))
505+
fmt.Fprintf(&script, "export _GGO_CLEAN_FILE=\"%s\"\n", cleanFile)
506506
script.WriteString("\n")
507507

508508
// Export TensorFusion environment variables
509509
for k, v := range envResult.EnvVars {
510-
script.WriteString(fmt.Sprintf("export %s=\"%s\"\n", k, v))
510+
fmt.Fprintf(&script, "export %s=\"%s\"\n", k, v)
511511
}
512512

513513
// Add LD_LIBRARY_PATH - use libs directory (contains only .so files)
514-
script.WriteString(fmt.Sprintf("export LD_LIBRARY_PATH=\"%s${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}\"\n", libsPath))
514+
fmt.Fprintf(&script, "export LD_LIBRARY_PATH=\"%s${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}\"\n", libsPath)
515515

516516
// Add GPU bin directory to PATH (for nvidia-smi, amdsmi, etc.)
517-
script.WriteString(fmt.Sprintf("export PATH=\"%s${PATH:+:$PATH}\"\n", binDir))
517+
fmt.Fprintf(&script, "export PATH=\"%s${PATH:+:$PATH}\"\n", binDir)
518518

519519
// Add LD_PRELOAD based on vendor - use libs directory
520520
libNames := studio.GetLibraryNames(config.Vendor)
@@ -523,13 +523,13 @@ func outputEvalCommandsUnix(config *studio.GPUEnvConfig, envResult *studio.GPUEn
523523
for _, lib := range libNames {
524524
preloadPaths = append(preloadPaths, filepath.Join(libsPath, lib))
525525
}
526-
script.WriteString(fmt.Sprintf("export LD_PRELOAD=\"%s${LD_PRELOAD:+:$LD_PRELOAD}\"\n", strings.Join(preloadPaths, ":")))
526+
fmt.Fprintf(&script, "export LD_PRELOAD=\"%s${LD_PRELOAD:+:$LD_PRELOAD}\"\n", strings.Join(preloadPaths, ":"))
527527
}
528528

529529
// Mark as activated
530530
script.WriteString("export _GGO_ACTIVE=1\n")
531-
script.WriteString(fmt.Sprintf("export _GGO_LIBS_PATH=\"%s\"\n", libsPath))
532-
script.WriteString(fmt.Sprintf("export _GGO_BIN_PATH=\"%s\"\n", binDir))
531+
fmt.Fprintf(&script, "export _GGO_LIBS_PATH=\"%s\"\n", libsPath)
532+
fmt.Fprintf(&script, "export _GGO_BIN_PATH=\"%s\"\n", binDir)
533533
script.WriteString("\n")
534534

535535
// Define ggo wrapper function to handle clean command automatically
@@ -621,28 +621,28 @@ func outputEvalCommandsPowerShell(config *studio.GPUEnvConfig, envResult *studio
621621
// Save original values for later restoration
622622
script.WriteString("# Save original environment for cleanup\n")
623623
script.WriteString("$env:_GGO_ORIG_PATH = $env:PATH\n")
624-
script.WriteString(fmt.Sprintf("$env:_GGO_CLEAN_FILE = \"%s\"\n", escapeForPowerShell(cleanFile)))
624+
fmt.Fprintf(&script, "$env:_GGO_CLEAN_FILE = \"%s\"\n", escapeForPowerShell(cleanFile))
625625
script.WriteString("\n")
626626

627627
// Export TensorFusion environment variables
628628
for k, v := range envResult.EnvVars {
629-
script.WriteString(fmt.Sprintf("$env:%s = \"%s\"\n", k, escapeForPowerShell(v)))
629+
fmt.Fprintf(&script, "$env:%s = \"%s\"\n", k, escapeForPowerShell(v))
630630
}
631631

632632
// Set GPU vendor
633-
script.WriteString(fmt.Sprintf("$env:TF_GPU_VENDOR = \"%s\"\n", config.Vendor))
633+
fmt.Fprintf(&script, "$env:TF_GPU_VENDOR = \"%s\"\n", config.Vendor)
634634

635635
// Add libs path and bin path to PATH at the front
636-
script.WriteString(fmt.Sprintf("$env:PATH = \"%s;%s;\" + $env:PATH\n", escapeForPowerShell(binDir), escapeForPowerShell(libsPath)))
636+
fmt.Fprintf(&script, "$env:PATH = \"%s;%s;\" + $env:PATH\n", escapeForPowerShell(binDir), escapeForPowerShell(libsPath))
637637

638638
// Set CUDA_PATH - point to libs directory
639-
script.WriteString(fmt.Sprintf("$env:CUDA_PATH = \"%s\"\n", escapeForPowerShell(libsPath)))
640-
script.WriteString(fmt.Sprintf("$env:CUDA_HOME = \"%s\"\n", escapeForPowerShell(libsPath)))
639+
fmt.Fprintf(&script, "$env:CUDA_PATH = \"%s\"\n", escapeForPowerShell(libsPath))
640+
fmt.Fprintf(&script, "$env:CUDA_HOME = \"%s\"\n", escapeForPowerShell(libsPath))
641641

642642
// Mark as activated
643643
script.WriteString("$env:_GGO_ACTIVE = \"1\"\n")
644-
script.WriteString(fmt.Sprintf("$env:_GGO_LIBS_PATH = \"%s\"\n", escapeForPowerShell(libsPath)))
645-
script.WriteString(fmt.Sprintf("$env:_GGO_BIN_PATH = \"%s\"\n", escapeForPowerShell(binDir)))
644+
fmt.Fprintf(&script, "$env:_GGO_LIBS_PATH = \"%s\"\n", escapeForPowerShell(libsPath))
645+
fmt.Fprintf(&script, "$env:_GGO_BIN_PATH = \"%s\"\n", escapeForPowerShell(binDir))
646646
script.WriteString("\n")
647647

648648
// Define ggo wrapper function for automatic clean handling
@@ -1264,7 +1264,7 @@ func setupLongTermWindows(shareInfo *api.SharePublicInfo, config *studio.GPUEnvC
12641264
if k == "PATH" {
12651265
continue
12661266
}
1267-
batContent.WriteString(fmt.Sprintf("setx %s \"%s\"\n", k, v))
1267+
fmt.Fprintf(&batContent, "setx %s \"%s\"\n", k, v)
12681268
}
12691269
batContent.WriteString("\necho Environment variables set. Please restart your terminal.\n")
12701270

internal/deps/deps.go

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,35 @@ func (m *Manager) DownloadLibraryToDir(ctx context.Context, lib Library, libsDir
677677
return m.downloadLibraryToDir(ctx, lib, libsDir, progressFn)
678678
}
679679

680+
// downloadToFile reads from reader into tmpFile in chunks, reporting progress
681+
// and returning the total bytes written. It closes tmpFile before returning.
682+
func downloadToFile(tmpFile *os.File, reader io.Reader, size int64, progressFn func(downloaded, total int64)) (int64, error) {
683+
var downloadedBytes int64
684+
buf := make([]byte, 32*1024)
685+
for {
686+
n, readErr := reader.Read(buf)
687+
if n > 0 {
688+
if _, writeErr := tmpFile.Write(buf[:n]); writeErr != nil {
689+
_ = tmpFile.Close()
690+
return 0, fmt.Errorf("failed to write file: %w", writeErr)
691+
}
692+
downloadedBytes += int64(n)
693+
if progressFn != nil {
694+
progressFn(downloadedBytes, size)
695+
}
696+
}
697+
if readErr == io.EOF {
698+
break
699+
}
700+
if readErr != nil {
701+
_ = tmpFile.Close()
702+
return 0, fmt.Errorf("failed to read response: %w", readErr)
703+
}
704+
}
705+
_ = tmpFile.Close()
706+
return downloadedBytes, nil
707+
}
708+
680709
// downloadLibraryToDir downloads a library to a specific libs directory.
681710
// Shared libraries (.so/.dll) go to libsDir; binaries go to cache root.
682711
func (m *Manager) downloadLibraryToDir(ctx context.Context, lib Library, libsDir string, progressFn func(downloaded, total int64)) error {
@@ -740,31 +769,12 @@ func (m *Manager) downloadLibraryToDir(ctx context.Context, lib Library, libsDir
740769

741770
// Download with progress and hash verification
742771
hash := sha256.New()
743-
var downloadedBytes int64
744772
reader := io.TeeReader(resp.Body, hash)
745773

746-
buf := make([]byte, 32*1024)
747-
for {
748-
n, readErr := reader.Read(buf)
749-
if n > 0 {
750-
if _, writeErr := tmpFile.Write(buf[:n]); writeErr != nil {
751-
_ = tmpFile.Close()
752-
return fmt.Errorf("failed to write file: %w", writeErr)
753-
}
754-
downloadedBytes += int64(n)
755-
if progressFn != nil {
756-
progressFn(downloadedBytes, lib.Size)
757-
}
758-
}
759-
if readErr == io.EOF {
760-
break
761-
}
762-
if readErr != nil {
763-
_ = tmpFile.Close()
764-
return fmt.Errorf("failed to read response: %w", readErr)
765-
}
774+
downloadedBytes, err := downloadToFile(tmpFile, reader, lib.Size, progressFn)
775+
if err != nil {
776+
return err
766777
}
767-
_ = tmpFile.Close()
768778

769779
// Verify hash (skip if SHA256 is empty)
770780
if lib.SHA256 != "" {

internal/studio/env.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,12 @@ func GenerateEnvScript(config *GPUEnvConfig, paths *platform.Paths) (string, err
338338

339339
// Export environment variables
340340
for k, v := range result.EnvVars {
341-
script.WriteString(fmt.Sprintf("export %s=\"%s\"\n", k, v))
341+
fmt.Fprintf(&script, "export %s=\"%s\"\n", k, v)
342342
}
343343

344344
// Add LD_LIBRARY_PATH - use libs directory (only contains .so files)
345345
script.WriteString("\n# Add GPU libraries to library path\n")
346-
script.WriteString(fmt.Sprintf("export LD_LIBRARY_PATH=\"%s:$LD_LIBRARY_PATH\"\n", libsPath))
346+
fmt.Fprintf(&script, "export LD_LIBRARY_PATH=\"%s:$LD_LIBRARY_PATH\"\n", libsPath)
347347

348348
// Add LD_PRELOAD based on vendor - use libs directory
349349
libNames := GetLibraryNames(config.Vendor)
@@ -353,12 +353,12 @@ func GenerateEnvScript(config *GPUEnvConfig, paths *platform.Paths) (string, err
353353
preloadPaths = append(preloadPaths, filepath.Join(libsPath, lib))
354354
}
355355
script.WriteString("\n# Preload GPU libraries\n")
356-
script.WriteString(fmt.Sprintf("export LD_PRELOAD=\"%s${LD_PRELOAD:+:$LD_PRELOAD}\"\n", strings.Join(preloadPaths, ":")))
356+
fmt.Fprintf(&script, "export LD_PRELOAD=\"%s${LD_PRELOAD:+:$LD_PRELOAD}\"\n", strings.Join(preloadPaths, ":"))
357357
}
358358

359359
script.WriteString("\n# GPU Go environment activated\n")
360-
script.WriteString(fmt.Sprintf("echo \"GPU Go environment activated for vendor: %s\"\n", config.Vendor))
361-
script.WriteString(fmt.Sprintf("echo \"Connection URL: %s\"\n", config.ConnectionURL))
360+
fmt.Fprintf(&script, "echo \"GPU Go environment activated for vendor: %s\"\n", config.Vendor)
361+
fmt.Fprintf(&script, "echo \"Connection URL: %s\"\n", config.ConnectionURL)
362362

363363
return script.String(), nil
364364
}
@@ -388,34 +388,34 @@ func GeneratePowerShellScript(config *GPUEnvConfig, paths *platform.Paths) (stri
388388

389389
// Export environment variables
390390
for k, v := range result.EnvVars {
391-
script.WriteString(fmt.Sprintf("$env:%s = \"%s\"\n", k, v))
391+
fmt.Fprintf(&script, "$env:%s = \"%s\"\n", k, v)
392392
}
393393

394394
// Set GPU vendor for ggo launch to detect correct DLLs
395-
script.WriteString(fmt.Sprintf("$env:TF_GPU_VENDOR = \"%s\"\n", config.Vendor))
395+
fmt.Fprintf(&script, "$env:TF_GPU_VENDOR = \"%s\"\n", config.Vendor)
396396

397397
// Add libs path to PATH at the FRONT (best effort for DLL loading)
398398
// libs directory contains only .dll files
399399
script.WriteString("\n# Add GPU libraries to PATH (prepend for priority)\n")
400-
script.WriteString(fmt.Sprintf("$env:PATH = \"%s;$env:PATH\"\n", libsPath))
400+
fmt.Fprintf(&script, "$env:PATH = \"%s;$env:PATH\"\n", libsPath)
401401

402402
// Set CUDA_PATH for applications that check it - point to libs directory
403403
script.WriteString("\n# Set CUDA_PATH for CUDA-aware applications\n")
404-
script.WriteString(fmt.Sprintf("$env:CUDA_PATH = \"%s\"\n", libsPath))
405-
script.WriteString(fmt.Sprintf("$env:CUDA_HOME = \"%s\"\n", libsPath))
404+
fmt.Fprintf(&script, "$env:CUDA_PATH = \"%s\"\n", libsPath)
405+
fmt.Fprintf(&script, "$env:CUDA_HOME = \"%s\"\n", libsPath)
406406

407407
// List required DLLs for this vendor
408408
windowsDLLs := GetWindowsLibraryNames(config.Vendor)
409409
if len(windowsDLLs) > 0 {
410410
script.WriteString("\n# Required DLLs for this vendor (should be in libs directory)\n")
411411
for _, dll := range windowsDLLs {
412-
script.WriteString(fmt.Sprintf("# - %s\n", dll))
412+
fmt.Fprintf(&script, "# - %s\n", dll)
413413
}
414414
}
415415

416416
script.WriteString("\n# GPU Go environment activated\n")
417-
script.WriteString(fmt.Sprintf("Write-Host \"GPU Go environment activated for vendor: %s\" -ForegroundColor Green\n", config.Vendor))
418-
script.WriteString(fmt.Sprintf("Write-Host \"Connection URL: %s\"\n", config.ConnectionURL))
417+
fmt.Fprintf(&script, "Write-Host \"GPU Go environment activated for vendor: %s\" -ForegroundColor Green\n", config.Vendor)
418+
fmt.Fprintf(&script, "Write-Host \"Connection URL: %s\"\n", config.ConnectionURL)
419419
script.WriteString("Write-Host \"\"\n")
420420
script.WriteString("Write-Host \"TIP: For reliable DLL loading, use 'ggo launch <program>'\" -ForegroundColor Yellow\n")
421421
script.WriteString("Write-Host \"Example: ggo launch python train.py\"\n")
@@ -449,32 +449,32 @@ func GenerateBatchScript(config *GPUEnvConfig, paths *platform.Paths) (string, e
449449

450450
// Export environment variables
451451
for k, v := range result.EnvVars {
452-
script.WriteString(fmt.Sprintf("set %s=%s\n", k, v))
452+
fmt.Fprintf(&script, "set %s=%s\n", k, v)
453453
}
454454

455455
// Set GPU vendor for ggo launch to detect correct DLLs
456-
script.WriteString(fmt.Sprintf("set TF_GPU_VENDOR=%s\n", config.Vendor))
456+
fmt.Fprintf(&script, "set TF_GPU_VENDOR=%s\n", config.Vendor)
457457

458458
// Add libs path to PATH at the FRONT - libs directory contains only .dll files
459459
script.WriteString("\nREM Add GPU libraries to PATH (prepend for priority)\n")
460-
script.WriteString(fmt.Sprintf("set PATH=%s;%%PATH%%\n", libsPath))
460+
fmt.Fprintf(&script, "set PATH=%s;%%PATH%%\n", libsPath)
461461

462462
// Set CUDA_PATH - point to libs directory
463463
script.WriteString("\nREM Set CUDA_PATH for CUDA-aware applications\n")
464-
script.WriteString(fmt.Sprintf("set CUDA_PATH=%s\n", libsPath))
465-
script.WriteString(fmt.Sprintf("set CUDA_HOME=%s\n", libsPath))
464+
fmt.Fprintf(&script, "set CUDA_PATH=%s\n", libsPath)
465+
fmt.Fprintf(&script, "set CUDA_HOME=%s\n", libsPath)
466466

467467
// List required DLLs for this vendor
468468
windowsDLLs := GetWindowsLibraryNames(config.Vendor)
469469
if len(windowsDLLs) > 0 {
470470
script.WriteString("\nREM Required DLLs for this vendor (should be in libs directory)\n")
471471
for _, dll := range windowsDLLs {
472-
script.WriteString(fmt.Sprintf("REM - %s\n", dll))
472+
fmt.Fprintf(&script, "REM - %s\n", dll)
473473
}
474474
}
475475

476476
script.WriteString("\necho GPU Go environment activated!\n")
477-
script.WriteString(fmt.Sprintf("echo Connection URL: %s\n", config.ConnectionURL))
477+
fmt.Fprintf(&script, "echo Connection URL: %s\n", config.ConnectionURL)
478478
script.WriteString("echo.\n")
479479
script.WriteString("echo TIP: For reliable DLL loading, use 'ggo launch ^<program^>'\n")
480480
script.WriteString("echo Example: ggo launch python train.py\n")

0 commit comments

Comments
 (0)