Skip to content

Commit ce84ede

Browse files
Use config file instead of hard-coded values
1 parent 7f083ca commit ce84ede

File tree

3 files changed

+68
-71
lines changed

3 files changed

+68
-71
lines changed

.codacy/codacy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
runtimes:
2-
- node@22.1.0
2+
- node@22.2.0
33
tools:
44

cli-v2.go

Lines changed: 60 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ import (
1616
"github.com/mholt/archiver/v4"
1717
)
1818

19-
// https://nodejs.org/dist/v22.2.0/node-v22.2.0-linux-arm64.tar.xz
20-
// https://nodejs.org/dist/v22.2.0/node-v22.2.0-darwin-x64.tar.gz
21-
// https://nodejs.org/dist/v13.14.0/node-v13.14.0-win-x64.zip
22-
// https://nodejs.org/dist/v22.2.0/node-v22.2.0-linux-armv7l.tar.xz
23-
func getNodeDownloadURL(version string) string {
19+
func getNodeFileName(nodeRuntime *config.Runtime) string {
2420
// Detect the OS and architecture
2521
goos := runtime.GOOS
2622
goarch := runtime.GOARCH
@@ -40,13 +36,20 @@ func getNodeDownloadURL(version string) string {
4036
nodeArch = goarch
4137
}
4238

39+
return fmt.Sprintf("node-v%s-%s-%s", nodeRuntime.Version(), goos, nodeArch)
40+
}
41+
42+
func getNodeDownloadURL(nodeRuntime *config.Runtime) string {
43+
// Detect the OS and architecture
44+
goos := runtime.GOOS
45+
4346
// Construct the Node.js download URL
4447
extension := "tar.gz"
4548
if goos == "windows" {
4649
extension = "zip"
4750
}
4851

49-
downloadURL := fmt.Sprintf("https://nodejs.org/dist/%s/node-%s-%s-%s.%s", version, version, goos, nodeArch, extension)
52+
downloadURL := fmt.Sprintf("https://nodejs.org/dist/v%s/%s.%s", nodeRuntime.Version(), getNodeFileName(nodeRuntime), extension)
5053
return downloadURL
5154
}
5255

@@ -89,29 +92,14 @@ func downloadFile(url string, destDir string) (string, error) {
8992
return destPath, nil
9093
}
9194

92-
func extract(t *os.File, codacyDirectory string) {
93-
95+
func extract(t *os.File, targetDir string) {
9496
format := archiver.CompressedArchive{
9597
Compression: archiver.Gz{},
9698
Archival: archiver.Tar{},
9799
}
98100

99-
// format, _, err := archiver.Identify(t.Name(), nil)
100-
// if err != nil {
101-
// log.Fatal(err)
102-
// }
103-
104-
// the list of files we want out of the archive; any
105-
// directories will include all their contents unless
106-
// we return fs.SkipDir from our handler
107-
// (leave this nil to walk ALL files from the archive)
108-
// fileList := []string{"file1.txt", "subfolder"}
109-
110101
handler := func(ctx context.Context, f archiver.File) error {
111-
112-
fmt.Printf("Contents of %s:\n", f.NameInArchive)
113-
114-
path := filepath.Join(codacyDirectory, "runtimes", f.NameInArchive)
102+
path := filepath.Join(targetDir, f.NameInArchive)
115103

116104
switch f.IsDir() {
117105
case true:
@@ -123,20 +111,19 @@ func extract(t *os.File, codacyDirectory string) {
123111
}
124112

125113
case false:
114+
log.Print("extracting: " + f.NameInArchive)
126115

116+
// if is a symlink
127117
if f.LinkTarget != "" {
128118
os.Remove(path)
129119
err := os.Symlink(f.LinkTarget, path)
130120
if err != nil {
131121
log.Fatal(err)
132122
}
133-
134123
return nil
135124
}
136125

137126
// write a file
138-
fmt.Println("extracting: " + f.NameInArchive)
139-
fmt.Println("targe link: " + f.LinkTarget)
140127
w, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, f.Mode())
141128
if err != nil {
142129
log.Fatal(err)
@@ -159,14 +146,12 @@ func extract(t *os.File, codacyDirectory string) {
159146
if err != nil {
160147
log.Fatal(err)
161148
}
162-
163149
}
164150

165-
func installESLint(npmExecutablePath string, ESLintversion string, codacyPath string) {
166-
167-
fmt.Println("Installing ESLint")
151+
func installESLint(npmExecutablePath string, ESLintversion string, toolsDirectory string) {
152+
log.Println("Installing ESLint")
168153

169-
eslintInstallationFolder := filepath.Join(codacyPath, "tools", ESLintversion)
154+
eslintInstallationFolder := filepath.Join(toolsDirectory, ESLintversion)
170155

171156
cmd := exec.Command(npmExecutablePath, "install", "--prefix", eslintInstallationFolder, ESLintversion, "@microsoft/eslint-formatter-sarif")
172157
// to use the chdir command we needed to create the folder before, we can change this after
@@ -181,24 +166,46 @@ func installESLint(npmExecutablePath string, ESLintversion string, codacyPath st
181166
}
182167
}
183168

184-
func fetchRuntimes(runtimes map[string]*config.Runtime) {
169+
func fetchRuntimes(runtimes map[string]*config.Runtime, runtimesDirectory string) {
185170
for _, runtime := range runtimes {
186171
switch runtime.Name() {
187172
case "node":
188-
fmt.Println("Fetching node...")
173+
// TODO should delete downloaded archive
174+
// TODO check for deflated archive
175+
log.Println("Fetching node...")
176+
downloadNodeURL := getNodeDownloadURL(runtime)
177+
nodeTar, err := downloadFile(downloadNodeURL, runtimesDirectory)
178+
if err != nil {
179+
log.Fatal(err)
180+
}
181+
182+
// deflate node archive
183+
t, err := os.Open(nodeTar)
184+
defer t.Close()
185+
if err != nil {
186+
log.Fatal(err)
187+
}
188+
extract(t, runtimesDirectory)
189189
default:
190-
fmt.Println("Unknown runtime:", runtime.Name())
190+
log.Fatal("Unknown runtime:", runtime.Name())
191191
}
192192
}
193193
}
194194

195-
func main() {
196-
_, configErr := config.ReadConfigFile(".codacy/codacy.yaml")
197-
if configErr != nil {
198-
log.Fatal(configErr)
199-
return
195+
func fetchTools(runtime *config.Runtime, runtimesDirectory string, toolsDirectory string) {
196+
for _, tool := range runtime.Tools() {
197+
switch tool.Name() {
198+
case "eslint":
199+
npmPath := filepath.Join(runtimesDirectory, getNodeFileName(runtime),
200+
"bin", "npm")
201+
installESLint(npmPath, "eslint@" + tool.Version(), toolsDirectory)
202+
default:
203+
log.Fatal("Unknown tool:", tool.Name())
204+
}
200205
}
206+
}
201207

208+
func main() {
202209
homePath, err := os.UserHomeDir()
203210
if err != nil {
204211
log.Fatal(err)
@@ -207,47 +214,30 @@ func main() {
207214
codacyDirectory := filepath.Join(homePath, ".cache", "codacy")
208215
runtimesDirectory := filepath.Join(codacyDirectory, "runtimes")
209216
toolsDirectory := filepath.Join(codacyDirectory, "tools")
210-
211-
fmt.Println("creating: " + codacyDirectory)
212-
err = os.MkdirAll(codacyDirectory, 0777)
213-
if err != nil {
217+
fmt.Println("creating: " + codacyDirectory)
218+
if os.MkdirAll(codacyDirectory, 0777) != nil {
214219
log.Fatal(err)
215220
}
216-
217-
fmt.Println("creating: " + runtimesDirectory)
218-
err = os.MkdirAll(runtimesDirectory, 0777)
219-
if err != nil {
221+
fmt.Println("creating: " + runtimesDirectory)
222+
if os.MkdirAll(runtimesDirectory, 0777) != nil {
220223
log.Fatal(err)
221224
}
222-
223-
fmt.Println("creating: " + toolsDirectory)
224-
err = os.MkdirAll(toolsDirectory, 0777)
225-
if err != nil {
225+
fmt.Println("creating: " + toolsDirectory)
226+
if os.MkdirAll(toolsDirectory, 0777) != nil {
226227
log.Fatal(err)
227228
}
228229

229-
downloadNodeURL := getNodeDownloadURL("v22.2.0")
230-
231-
nodeTar, err := downloadFile(downloadNodeURL, codacyDirectory)
232-
if err != nil {
233-
log.Fatal(err)
230+
// TODO can use a variable to stored the "local" codacy dir
231+
runtimes, configErr := config.ReadConfigFile(filepath.Join(".codacy", "codacy.yaml"))
232+
if configErr != nil {
233+
log.Fatal(configErr)
234234
}
235235

236-
fmt.Println("Downloaded node: " + nodeTar)
237-
238-
t, err := os.Open(nodeTar)
239-
defer t.Close()
240-
if err != nil {
241-
log.Fatal(err)
236+
// install runtimes
237+
fetchRuntimes(runtimes, runtimesDirectory)
238+
for _, r := range runtimes {
239+
fetchTools(r, runtimesDirectory, toolsDirectory)
242240
}
243241

244-
fmt.Println("About to extract node: " + t.Name())
245-
extract(t, codacyDirectory)
246-
247-
npmPath := filepath.Join(codacyDirectory, "runtimes", "node-v22.2.0-darwin-x64", "bin", "npm")
248-
249-
fmt.Println("About to install eslint")
250-
installESLint(npmPath, "[email protected]", codacyDirectory)
251-
252242
cmd.Execute()
253243
}

config/configFile.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"fmt"
45
"gopkg.in/yaml.v3"
56
"os"
67
)
@@ -32,6 +33,10 @@ func (r *Runtime) AddTool(tool *ConfigTool) {
3233
r.tools = append(r.tools, *tool)
3334
}
3435

36+
func (r *Runtime) FullName() string {
37+
return fmt.Sprintf("%s-%s", r.name, r.version)
38+
}
39+
3540
func parseConfigFile(configContents []byte) (map[string]*Runtime, error) {
3641
configFile := configFile{}
3742
if err := yaml.Unmarshal(configContents, &configFile); err != nil {
@@ -42,6 +47,7 @@ func parseConfigFile(configContents []byte) (map[string]*Runtime, error) {
4247
for _, rt := range configFile.RUNTIMES {
4348
ct, err := parseConfigTool(rt)
4449
if err != nil {
50+
return nil, err
4551
}
4652
runtimes[ct.name] = &Runtime{
4753
name: ct.name,
@@ -52,6 +58,7 @@ func parseConfigFile(configContents []byte) (map[string]*Runtime, error) {
5258
for _, tl := range configFile.TOOLS {
5359
ct, err := parseConfigTool(tl)
5460
if err != nil {
61+
return nil, err
5562
}
5663
switch ct.name {
5764
case "eslint":

0 commit comments

Comments
 (0)