Skip to content

Commit 372df27

Browse files
Read config. Download node.
1 parent 6107a3f commit 372df27

File tree

7 files changed

+443
-1
lines changed

7 files changed

+443
-1
lines changed

.codacy/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
runtimes

.codacy/codacy.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
runtimes:
2+
3+
tools:
4+

cli-v2.go

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,135 @@
11
package main
22

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+
491

592
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+
6134
cmd.Execute()
7135
}

cmd/analyze.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package cmd

config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
4+
type Config struct {
5+
RUNTIMES []string
6+
TOOLS []string
7+
}

go.mod

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@ go 1.22.3
55
require github.com/spf13/cobra v1.8.0
66

77
require (
8+
github.com/andybalholm/brotli v1.0.4 // indirect
9+
github.com/bodgit/plumbing v1.2.0 // indirect
10+
github.com/bodgit/sevenzip v1.3.0 // indirect
11+
github.com/bodgit/windows v1.0.0 // indirect
12+
github.com/connesc/cipherio v0.2.1 // indirect
13+
github.com/dsnet/compress v0.0.1 // indirect
14+
github.com/fatih/color v1.17.0 // indirect
15+
github.com/goccy/go-yaml v1.11.3 // indirect
16+
github.com/golang/snappy v0.0.4 // indirect
17+
github.com/hashicorp/errwrap v1.0.0 // indirect
18+
github.com/hashicorp/go-multierror v1.1.1 // indirect
819
github.com/inconshreveable/mousetrap v1.1.0 // indirect
20+
github.com/klauspost/compress v1.15.9 // indirect
21+
github.com/klauspost/pgzip v1.2.5 // indirect
22+
github.com/mattn/go-colorable v0.1.13 // indirect
23+
github.com/mattn/go-isatty v0.0.20 // indirect
24+
github.com/mholt/archiver/v4 v4.0.0-alpha.8 // indirect
25+
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect
26+
github.com/pierrec/lz4/v4 v4.1.15 // indirect
927
github.com/spf13/pflag v1.0.5 // indirect
28+
github.com/therootcompany/xz v1.0.1 // indirect
29+
github.com/ulikunitz/xz v0.5.10 // indirect
30+
go4.org v0.0.0-20200411211856-f5505b9728dd // indirect
31+
golang.org/x/sys v0.20.0 // indirect
32+
golang.org/x/text v0.3.8 // indirect
33+
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
34+
gopkg.in/yaml.v3 v3.0.1 // indirect
1035
)

0 commit comments

Comments
 (0)