|
1 | 1 | package main |
2 | 2 |
|
3 | | -import "codacy/cli-v2/cmd" |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "codacy/cli-v2/cmd" |
| 6 | + "compress/gzip" |
| 7 | + "fmt" |
| 8 | + "gopkg.in/yaml.v3" |
| 9 | + "io" |
| 10 | + "log" |
| 11 | + "net/http" |
| 12 | + "os" |
| 13 | + "path/filepath" |
| 14 | + "runtime" |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +// https://nodejs.org/dist/v22.2.0/node-v22.2.0-linux-arm64.tar.xz |
| 19 | +// https://nodejs.org/dist/v22.2.0/node-v22.2.0-darwin-x64.tar.gz |
| 20 | +// https://nodejs.org/dist/v13.14.0/node-v13.14.0-win-x64.zip |
| 21 | +// https://nodejs.org/dist/v22.2.0/node-v22.2.0-linux-armv7l.tar.xz |
| 22 | +func getNodeDownloadURL(version string) string { |
| 23 | + // Detect the OS and architecture |
| 24 | + goos := runtime.GOOS |
| 25 | + goarch := runtime.GOARCH |
| 26 | + |
| 27 | + // Map Go architecture to Node.js architecture |
| 28 | + var nodeArch string |
| 29 | + switch goarch { |
| 30 | + case "386": |
| 31 | + nodeArch = "x86" |
| 32 | + case "amd64": |
| 33 | + nodeArch = "x64" |
| 34 | + case "arm": |
| 35 | + nodeArch = "armv7l" |
| 36 | + case "arm64": |
| 37 | + nodeArch = "arm64" |
| 38 | + default: |
| 39 | + nodeArch = goarch |
| 40 | + } |
| 41 | + |
| 42 | + // Construct the Node.js download URL |
| 43 | + extension := "tar.gz" |
| 44 | + if goos == "windows" { |
| 45 | + extension = "zip" |
| 46 | + } |
| 47 | + |
| 48 | + downloadURL := fmt.Sprintf("https://nodejs.org/dist/%s/node-%s-%s-%s.%s", version, version, goos, nodeArch, extension) |
| 49 | + return downloadURL |
| 50 | +} |
| 51 | + |
| 52 | +func downloadFile(url string, destDir string) (string, error) { |
| 53 | + // Get the file name from the URL |
| 54 | + fileName := filepath.Base(url) |
| 55 | + |
| 56 | + // Create the destination file path |
| 57 | + destPath := filepath.Join(destDir, fileName) |
| 58 | + |
| 59 | + _, errInfo := os.Stat(destPath) |
| 60 | + if errInfo != nil && os.IsExist(errInfo) { |
| 61 | + return destPath, nil |
| 62 | + } |
| 63 | + // Create the destination file |
| 64 | + outFile, err := os.Create(destPath) |
| 65 | + if err != nil { |
| 66 | + return "", fmt.Errorf("failed to create file: %w", err) |
| 67 | + } |
| 68 | + defer outFile.Close() |
| 69 | + |
| 70 | + // Make the HTTP GET request |
| 71 | + resp, err := http.Get(url) |
| 72 | + if err != nil { |
| 73 | + return "", fmt.Errorf("failed to make GET request: %w", err) |
| 74 | + } |
| 75 | + defer resp.Body.Close() |
| 76 | + |
| 77 | + // Check if the request was successful |
| 78 | + if resp.StatusCode != http.StatusOK { |
| 79 | + return "", fmt.Errorf("failed to download file: status code %d", resp.StatusCode) |
| 80 | + } |
| 81 | + |
| 82 | + // Copy the response body to the destination file |
| 83 | + _, err = io.Copy(outFile, resp.Body) |
| 84 | + if err != nil { |
| 85 | + return "", fmt.Errorf("failed to copy file contents: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + return destPath, nil |
| 89 | +} |
| 90 | + |
4 | 91 |
|
5 | 92 | func main() { |
| 93 | + content, err := os.ReadFile(".codacy/codacy.yaml") |
| 94 | + if err != nil { |
| 95 | + log.Fatal(err) |
| 96 | + } |
| 97 | + |
| 98 | + config := Config{} |
| 99 | + if err := yaml.Unmarshal(content, &config); err != nil { |
| 100 | + log.Fatalf("error: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + fmt.Println(config) |
| 104 | + downloadURL := getNodeDownloadURL("v22.2.0") |
| 105 | + nodeTar, err := downloadFile(downloadURL, ".codacy") |
| 106 | + fmt.Println(nodeTar) |
| 107 | + |
| 108 | + t, _ := os.Open(nodeTar) |
| 109 | + defer t.Close() |
| 110 | + uncompressedStream, _ := gzip.NewReader(t) |
| 111 | + |
| 112 | + tr := tar.NewReader(uncompressedStream) |
| 113 | + for { |
| 114 | + hdr, err := tr.Next() |
| 115 | + if err == io.EOF { |
| 116 | + break |
| 117 | + } |
| 118 | + if err != nil { |
| 119 | + log.Fatal(err) |
| 120 | + } |
| 121 | + fmt.Printf("Contents of %s:\n", hdr.Name) |
| 122 | + |
| 123 | + outFile, err := os.Create(".codacy/node/" + hdr.Name) |
| 124 | + if err != nil { |
| 125 | + break |
| 126 | + } |
| 127 | + |
| 128 | + if _, err := io.Copy(outFile, tr); err != nil { |
| 129 | + log.Fatal(err) |
| 130 | + } |
| 131 | + fmt.Println() |
| 132 | + } |
| 133 | + |
6 | 134 | cmd.Execute() |
7 | 135 | } |
0 commit comments