|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "runtime" |
| 12 | + |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var version string |
| 17 | + |
| 18 | +func init() { |
| 19 | + useCmd.Flags().StringVarP(&version, "version", "v", "", "Specific version to use (e.g. 1.0.0) or 'latest' to get the latest version") |
| 20 | + useCmd.MarkFlagRequired("version") |
| 21 | + rootCmd.AddCommand(useCmd) |
| 22 | +} |
| 23 | + |
| 24 | +var useCmd = &cobra.Command{ |
| 25 | + Use: "use", |
| 26 | + Short: "Switch to a specific version", |
| 27 | + Long: `Switch to a specific version of the CLI. The command will: |
| 28 | +1. Use the version if it's already installed locally |
| 29 | +2. If not, download and install the specified version |
| 30 | +3. Switch to the specified version |
| 31 | +
|
| 32 | +Use 'latest' to get the latest version.`, |
| 33 | + Run: func(cmd *cobra.Command, args []string) { |
| 34 | + // Check if CODACY_CLI_V2_VERSION is set |
| 35 | + if envVersion := os.Getenv("CODACY_CLI_V2_VERSION"); envVersion != "" { |
| 36 | + fmt.Printf("CODACY_CLI_V2_VERSION is set to '%s'\n", envVersion) |
| 37 | + fmt.Printf("Overwrite this version with `%s' ? (y/N): ", version) |
| 38 | + |
| 39 | + var response string |
| 40 | + fmt.Scanln(&response) |
| 41 | + if response != "y" && response != "Y" { |
| 42 | + fmt.Println("Operation cancelled") |
| 43 | + os.Exit(0) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // If version is "latest", get the latest version |
| 48 | + if version == "latest" { |
| 49 | + fmt.Println("Checking for latest version...") |
| 50 | + latestVersion, err := getLatestVersion() |
| 51 | + if err != nil { |
| 52 | + fmt.Printf("Failed to get latest version: %v\n", err) |
| 53 | + os.Exit(1) |
| 54 | + } |
| 55 | + version = latestVersion |
| 56 | + } |
| 57 | + |
| 58 | + // Get the cache directory based on OS |
| 59 | + var cacheDir string |
| 60 | + switch runtime.GOOS { |
| 61 | + case "linux": |
| 62 | + cacheDir = filepath.Join(os.Getenv("HOME"), ".cache", "codacy", "codacy-cli-v2") |
| 63 | + case "darwin": |
| 64 | + cacheDir = filepath.Join(os.Getenv("HOME"), "Library", "Caches", "Codacy", "codacy-cli-v2") |
| 65 | + default: |
| 66 | + cacheDir = ".codacy-cli-v2" |
| 67 | + } |
| 68 | + |
| 69 | + // Check if version is already installed |
| 70 | + versionDir := filepath.Join(cacheDir, version) |
| 71 | + cachedBinary := filepath.Join(versionDir, "codacy-cli-v2") |
| 72 | + if _, err := os.Stat(cachedBinary); err == nil { |
| 73 | + fmt.Printf("Version %s is already installed locally\n", version) |
| 74 | + } else { |
| 75 | + // Create version-specific directory |
| 76 | + if err := os.MkdirAll(versionDir, 0755); err != nil { |
| 77 | + fmt.Printf("Failed to create cache directory: %v\n", err) |
| 78 | + os.Exit(1) |
| 79 | + } |
| 80 | + |
| 81 | + // Download and extract the specified version to cache |
| 82 | + fmt.Printf("Downloading version %s...\n", version) |
| 83 | + if err := downloadAndExtract(version, versionDir); err != nil { |
| 84 | + fmt.Printf("Failed to download and extract version %s: %v\n", version, err) |
| 85 | + os.Exit(1) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Get the current executable path |
| 90 | + executable, err := os.Executable() |
| 91 | + if err != nil { |
| 92 | + fmt.Printf("Failed to get executable path: %v\n", err) |
| 93 | + os.Exit(1) |
| 94 | + } |
| 95 | + |
| 96 | + // Copy the binary from cache to the executable location |
| 97 | + if err := copyFile(cachedBinary, executable); err != nil { |
| 98 | + fmt.Printf("Failed to copy binary: %v\n", err) |
| 99 | + os.Exit(1) |
| 100 | + } |
| 101 | + |
| 102 | + fmt.Printf("Successfully switched to version %s\n", version) |
| 103 | + }, |
| 104 | +} |
| 105 | + |
| 106 | +func getLatestVersion() (string, error) { |
| 107 | + // Get the latest version from GitHub |
| 108 | + resp, err := http.Get("https://api.github.com/repos/codacy/codacy-cli-v2/releases/latest") |
| 109 | + if err != nil { |
| 110 | + return "", fmt.Errorf("failed to get latest version: %w", err) |
| 111 | + } |
| 112 | + defer resp.Body.Close() |
| 113 | + |
| 114 | + if resp.StatusCode != http.StatusOK { |
| 115 | + return "", fmt.Errorf("failed to get latest version: status code %d", resp.StatusCode) |
| 116 | + } |
| 117 | + |
| 118 | + var release struct { |
| 119 | + TagName string `json:"tag_name"` |
| 120 | + } |
| 121 | + if err := json.NewDecoder(resp.Body).Decode(&release); err != nil { |
| 122 | + return "", fmt.Errorf("failed to decode response: %w", err) |
| 123 | + } |
| 124 | + |
| 125 | + return release.TagName, nil |
| 126 | +} |
| 127 | + |
| 128 | +func downloadAndExtract(version string, targetDir string) error { |
| 129 | + // Construct the download URL |
| 130 | + osName := runtime.GOOS |
| 131 | + arch := runtime.GOARCH |
| 132 | + if arch == "amd64" { |
| 133 | + arch = "x86_64" |
| 134 | + } |
| 135 | + |
| 136 | + fileName := fmt.Sprintf("codacy-cli-v2_%s_%s_%s.tar.gz", version, osName, arch) |
| 137 | + url := fmt.Sprintf("https://github.com/codacy/codacy-cli-v2/releases/download/%s/%s", version, fileName) |
| 138 | + |
| 139 | + // Download the file directly to the target directory |
| 140 | + resp, err := http.Get(url) |
| 141 | + if err != nil { |
| 142 | + return fmt.Errorf("failed to download file: %w", err) |
| 143 | + } |
| 144 | + defer resp.Body.Close() |
| 145 | + |
| 146 | + if resp.StatusCode != http.StatusOK { |
| 147 | + return fmt.Errorf("failed to download file: status code %d", resp.StatusCode) |
| 148 | + } |
| 149 | + |
| 150 | + // Create the tar.gz file in the target directory |
| 151 | + tarFile := filepath.Join(targetDir, fileName) |
| 152 | + out, err := os.Create(tarFile) |
| 153 | + if err != nil { |
| 154 | + return fmt.Errorf("failed to create file: %w", err) |
| 155 | + } |
| 156 | + defer out.Close() |
| 157 | + |
| 158 | + // Write the downloaded content to the file |
| 159 | + if _, err := io.Copy(out, resp.Body); err != nil { |
| 160 | + return fmt.Errorf("failed to write to file: %w", err) |
| 161 | + } |
| 162 | + |
| 163 | + // Extract the tar.gz file |
| 164 | + cmd := exec.Command("tar", "xzf", tarFile, "-C", targetDir) |
| 165 | + if err := cmd.Run(); err != nil { |
| 166 | + return fmt.Errorf("failed to extract file: %w", err) |
| 167 | + } |
| 168 | + |
| 169 | + // Remove the tar.gz file after extraction |
| 170 | + if err := os.Remove(tarFile); err != nil { |
| 171 | + return fmt.Errorf("failed to remove tar file: %w", err) |
| 172 | + } |
| 173 | + |
| 174 | + return nil |
| 175 | +} |
| 176 | + |
| 177 | +func copyFile(src, dst string) error { |
| 178 | + srcFile, err := os.Open(src) |
| 179 | + if err != nil { |
| 180 | + return fmt.Errorf("failed to open source file: %w", err) |
| 181 | + } |
| 182 | + defer srcFile.Close() |
| 183 | + |
| 184 | + dstFile, err := os.Create(dst) |
| 185 | + if err != nil { |
| 186 | + return fmt.Errorf("failed to create destination file: %w", err) |
| 187 | + } |
| 188 | + defer dstFile.Close() |
| 189 | + |
| 190 | + if _, err := io.Copy(dstFile, srcFile); err != nil { |
| 191 | + return fmt.Errorf("failed to copy file: %w", err) |
| 192 | + } |
| 193 | + |
| 194 | + return nil |
| 195 | +} |
0 commit comments