|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "codacy/cli-v2/plugins" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | + "text/template" |
| 12 | +) |
| 13 | + |
| 14 | +// InstallTools installs all tools defined in the configuration |
| 15 | +func InstallTools() error { |
| 16 | + for name, toolInfo := range Config.Tools() { |
| 17 | + err := InstallTool(name, toolInfo) |
| 18 | + if err != nil { |
| 19 | + return fmt.Errorf("failed to install tool %s: %w", name, err) |
| 20 | + } |
| 21 | + } |
| 22 | + return nil |
| 23 | +} |
| 24 | + |
| 25 | +// InstallTool installs a specific tool |
| 26 | +func InstallTool(name string, toolInfo *plugins.ToolInfo) error { |
| 27 | + // Check if the tool is already installed |
| 28 | + if isToolInstalled(toolInfo) { |
| 29 | + fmt.Printf("Tool %s v%s is already installed\n", name, toolInfo.Version) |
| 30 | + return nil |
| 31 | + } |
| 32 | + |
| 33 | + // Get the runtime for this tool |
| 34 | + runtimeInfo, ok := Config.Runtimes()[toolInfo.Runtime] |
| 35 | + if !ok { |
| 36 | + return fmt.Errorf("required runtime %s not found for tool %s", toolInfo.Runtime, name) |
| 37 | + } |
| 38 | + |
| 39 | + // Make sure the installation directory exists |
| 40 | + err := os.MkdirAll(toolInfo.InstallDir, 0755) |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf("failed to create installation directory: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + // Prepare template data |
| 46 | + templateData := map[string]string{ |
| 47 | + "InstallDir": toolInfo.InstallDir, |
| 48 | + "PackageName": toolInfo.Name, |
| 49 | + "Version": toolInfo.Version, |
| 50 | + "Registry": "", // TODO: Get registry from config |
| 51 | + } |
| 52 | + |
| 53 | + // Get package manager binary based on the tool configuration |
| 54 | + packageManagerName := toolInfo.PackageManager |
| 55 | + packageManagerBinary, ok := runtimeInfo.Binaries[packageManagerName] |
| 56 | + if !ok { |
| 57 | + return fmt.Errorf("package manager binary %s not found in runtime %s", packageManagerName, toolInfo.Runtime) |
| 58 | + } |
| 59 | + |
| 60 | + // Set registry if provided |
| 61 | + if toolInfo.RegistryCommand != "" { |
| 62 | + regCmd, err := executeToolTemplate(toolInfo.RegistryCommand, templateData) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("failed to prepare registry command: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + if regCmd != "" { |
| 68 | + registryCmd := exec.Command(packageManagerBinary, strings.Split(regCmd, " ")...) |
| 69 | + if output, err := registryCmd.CombinedOutput(); err != nil { |
| 70 | + return fmt.Errorf("failed to set registry: %s: %w", string(output), err) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Execute installation command |
| 76 | + installCmd, err := executeToolTemplate(toolInfo.InstallCommand, templateData) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("failed to prepare install command: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + // Execute the installation command using the package manager |
| 82 | + cmd := exec.Command(packageManagerBinary, strings.Split(installCmd, " ")...) |
| 83 | + |
| 84 | + log.Printf("Installing %s v%s...\n", toolInfo.Name, toolInfo.Version) |
| 85 | + output, err := cmd.CombinedOutput() |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("failed to install tool: %s: %w", string(output), err) |
| 88 | + } |
| 89 | + |
| 90 | + log.Printf("Successfully installed %s v%s\n", toolInfo.Name, toolInfo.Version) |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +// isToolInstalled checks if a tool is already installed by checking for the binary |
| 95 | +func isToolInstalled(toolInfo *plugins.ToolInfo) bool { |
| 96 | + // If there are no binaries, check the install directory |
| 97 | + if len(toolInfo.Binaries) == 0 { |
| 98 | + _, err := os.Stat(toolInfo.InstallDir) |
| 99 | + return err == nil |
| 100 | + } |
| 101 | + |
| 102 | + // Check if at least one binary exists |
| 103 | + for _, binaryPath := range toolInfo.Binaries { |
| 104 | + _, err := os.Stat(binaryPath) |
| 105 | + if err == nil { |
| 106 | + return true |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return false |
| 111 | +} |
| 112 | + |
| 113 | +// executeToolTemplate executes a template with the given data |
| 114 | +func executeToolTemplate(tmplStr string, data map[string]string) (string, error) { |
| 115 | + tmpl, err := template.New("command").Parse(tmplStr) |
| 116 | + if err != nil { |
| 117 | + return "", err |
| 118 | + } |
| 119 | + |
| 120 | + var buf bytes.Buffer |
| 121 | + err = tmpl.Execute(&buf, data) |
| 122 | + if err != nil { |
| 123 | + return "", err |
| 124 | + } |
| 125 | + |
| 126 | + return buf.String(), nil |
| 127 | +} |
0 commit comments