(scriptPath, installDir string)
| 35 | } |
| 36 | |
| 37 | func (c *CondaPython) installConda(scriptPath, installDir string) error { |
| 38 | if !fileio.PathExists(scriptPath) { |
| 39 | return fmt.Errorf("Miniconda script not found at %s", scriptPath) |
| 40 | } |
| 41 | |
| 42 | // If installation directory already exists, check if conda is already functional. |
| 43 | if fileio.PathExists(installDir) { |
| 44 | binDir := expr.If(runtime.GOOS == "windows", "Scripts", "bin") |
| 45 | condaName := expr.If(runtime.GOOS == "windows", "conda.exe", "conda") |
| 46 | condaBinary := filepath.Join(installDir, binDir, condaName) |
| 47 | if fileio.PathExists(condaBinary) { |
| 48 | if err := exec.Command(condaBinary, "--version").Run(); err == nil { |
| 49 | color.PrintPass("tool: %s", "conda-"+c.condaVersion) |
| 50 | color.PrintHint("Location: %s\n", installDir) |
| 51 | return nil |
| 52 | } |
| 53 | } |
| 54 | // Directory exists but conda might be broken, try to update existing installation. |
| 55 | color.PrintHint("Found existing conda directory, attempting update...") |
| 56 | } else { |
| 57 | // Ensure install directory exists |
| 58 | if err := os.MkdirAll(installDir, os.ModePerm); err != nil { |
| 59 | return fmt.Errorf("failed to create conda install directory %s -> %w", installDir, err) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | switch runtime.GOOS { |
| 64 | case "linux", "darwin": |
| 65 | // Make script executable on Unix systems. |
| 66 | if err := os.Chmod(scriptPath, os.ModePerm); err != nil { |
| 67 | return fmt.Errorf("failed to make conda script executable: %w", err) |
| 68 | } |
| 69 | |
| 70 | // Run conda installer in batch mode with -b -p flags. |
| 71 | // -b: batch mode (no interactive prompts) |
| 72 | // -p: installation prefix (directory) |
| 73 | // -u: update existing installation (if directory already exists) |
| 74 | command := fmt.Sprintf("bash %s -b -p %s", scriptPath, installDir) |
| 75 | executor := cmd.NewExecutor("[conda install]", command) |
| 76 | if err := executor.Execute(); err != nil { |
| 77 | // If installation failed and directory exists, try update mode. |
| 78 | if fileio.PathExists(installDir) { |
| 79 | color.PrintHint("Initial installation failed, trying update mode...") |
| 80 | command := fmt.Sprintf("bash %s -b -u -p %s", scriptPath, installDir) |
| 81 | executor := cmd.NewExecutor("[conda install in update mode]", command) |
| 82 | if err := executor.Execute(); err != nil { |
| 83 | return fmt.Errorf("failed to install/update conda: %w", err) |
| 84 | } |
| 85 | } else { |
| 86 | return fmt.Errorf("failed to install conda: %w", err) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Verify conda binary exists. |
| 91 | condaBinary := filepath.Join(installDir, "bin", "conda") |
| 92 | if !fileio.PathExists(condaBinary) { |
| 93 | return fmt.Errorf("conda binary not found after installation at %s", condaBinary) |
| 94 | } |
no test coverage detected