1+ package config
2+
3+ import (
4+ "codacy/cli-v2/plugins"
5+ "codacy/cli-v2/utils"
6+ "fmt"
7+ "log"
8+ "os"
9+ "path/filepath"
10+ "strings"
11+ )
12+
13+ // InstallRuntimes installs all runtimes defined in the configuration
14+ func InstallRuntimes () error {
15+ for name , runtimeInfo := range Config .Runtimes () {
16+ err := InstallRuntime (name , runtimeInfo )
17+ if err != nil {
18+ return fmt .Errorf ("failed to install runtime %s: %w" , name , err )
19+ }
20+ }
21+ return nil
22+ }
23+
24+ // InstallRuntime installs a specific runtime
25+ func InstallRuntime (name string , runtimeInfo * plugins.RuntimeInfo ) error {
26+ // Check if the runtime is already installed
27+ if isRuntimeInstalled (runtimeInfo ) {
28+ fmt .Printf ("Runtime %s v%s is already installed\n " , name , runtimeInfo .Version )
29+ return nil
30+ }
31+
32+ // Download and extract the runtime
33+ err := downloadAndExtractRuntime (runtimeInfo )
34+ if err != nil {
35+ return fmt .Errorf ("failed to download and extract runtime %s: %w" , name , err )
36+ }
37+
38+ return nil
39+ }
40+
41+ // isRuntimeInstalled checks if a runtime is already installed by checking for the binary
42+ func isRuntimeInstalled (runtimeInfo * plugins.RuntimeInfo ) bool {
43+ // If there are no binaries, check the install directory
44+ if len (runtimeInfo .Binaries ) == 0 {
45+ _ , err := os .Stat (runtimeInfo .InstallDir )
46+ return err == nil
47+ }
48+
49+ // Check if at least one binary exists
50+ for _ , binaryPath := range runtimeInfo .Binaries {
51+ _ , err := os .Stat (binaryPath )
52+ if err == nil {
53+ return true
54+ }
55+ }
56+
57+ return false
58+ }
59+
60+ // downloadAndExtractRuntime downloads and extracts a runtime
61+ func downloadAndExtractRuntime (runtimeInfo * plugins.RuntimeInfo ) error {
62+ // Create a file name for the downloaded archive
63+ fileName := filepath .Base (runtimeInfo .DownloadURL )
64+ downloadPath := filepath .Join (Config .RuntimesDirectory (), fileName )
65+
66+ // Check if the file already exists
67+ _ , err := os .Stat (downloadPath )
68+ if os .IsNotExist (err ) {
69+ // Download the file
70+ log .Printf ("Downloading %s v%s...\n " , runtimeInfo .Name , runtimeInfo .Version )
71+ downloadPath , err = utils .DownloadFile (runtimeInfo .DownloadURL , Config .RuntimesDirectory ())
72+ if err != nil {
73+ return fmt .Errorf ("failed to download runtime: %w" , err )
74+ }
75+ } else if err != nil {
76+ return fmt .Errorf ("error checking for existing download: %w" , err )
77+ } else {
78+ log .Printf ("Using existing download for %s v%s\n " , runtimeInfo .Name , runtimeInfo .Version )
79+ }
80+
81+ // Open the downloaded file
82+ file , err := os .Open (downloadPath )
83+ if err != nil {
84+ return fmt .Errorf ("failed to open downloaded file: %w" , err )
85+ }
86+ defer file .Close ()
87+
88+ // Extract based on file extension
89+ log .Printf ("Extracting %s v%s...\n " , runtimeInfo .Name , runtimeInfo .Version )
90+ if strings .HasSuffix (fileName , ".zip" ) {
91+ err = utils .ExtractZip (file .Name (), Config .RuntimesDirectory ())
92+ } else {
93+ err = utils .ExtractTarGz (file , Config .RuntimesDirectory ())
94+ }
95+
96+ if err != nil {
97+ return fmt .Errorf ("failed to extract runtime: %w" , err )
98+ }
99+
100+ log .Printf ("Successfully installed %s v%s\n " , runtimeInfo .Name , runtimeInfo .Version )
101+ return nil
102+ }
0 commit comments