-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinstall.go
More file actions
245 lines (226 loc) · 7.08 KB
/
install.go
File metadata and controls
245 lines (226 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package cmd
import (
"codacy/cli-v2/config"
cfg "codacy/cli-v2/config"
config_file "codacy/cli-v2/config-file"
"codacy/cli-v2/utils/logger"
"fmt"
"io"
"log"
"os"
"time"
"github.com/fatih/color"
"github.com/schollz/progressbar/v3"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var registry string
func init() {
installCmd.Flags().StringVarP(®istry, "registry", "r", "", "Registry to use for installing tools")
rootCmd.AddCommand(installCmd)
}
var installCmd = &cobra.Command{
Use: "install",
Short: "Installs the tools specified in the project's config-file.",
Long: "Installs all runtimes and tools specified in the project's config-file file.",
Run: func(cmd *cobra.Command, args []string) {
bold := color.New(color.Bold)
green := color.New(color.FgGreen)
// Create necessary directories
if err := config.Config.CreateCodacyDirs(); err != nil {
logger.Error("Failed to create Codacy directories", logrus.Fields{
"error": err.Error(),
})
log.Fatal(err)
}
// Load config file
if err := config_file.ReadConfigFile(cfg.Config.ProjectConfigFile()); err != nil {
logger.Warn("Configuration file not found", logrus.Fields{
"error": err.Error(),
})
fmt.Println()
color.Red("⚠️ Warning: Could not find configuration file!")
fmt.Println("Please run 'codacy-cli init' first to create a configuration file.")
fmt.Println()
os.Exit(1)
}
// Check if anything needs to be installed
needsInstallation := false
for name, runtime := range cfg.Config.Runtimes() {
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
needsInstallation = true
break
}
}
if !needsInstallation {
for name, tool := range cfg.Config.Tools() {
if !cfg.Config.IsToolInstalled(name, tool) {
needsInstallation = true
break
}
}
}
if !needsInstallation {
logger.Info("All components are already installed", nil)
fmt.Println()
bold.Println("✅ All components are already installed!")
return
}
logger.Info("Starting installation process", nil)
fmt.Println()
bold.Println("🚀 Starting installation process...")
fmt.Println()
// Calculate total items to install
totalItems := 0
for name, runtime := range cfg.Config.Runtimes() {
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
totalItems++
}
}
for name, tool := range cfg.Config.Tools() {
if !cfg.Config.IsToolInstalled(name, tool) {
totalItems++
}
}
if totalItems == 0 {
logger.Info("All components are already installed", nil)
fmt.Println()
bold.Println("✅ All components are already installed!")
return
}
// Print list of items to install
fmt.Println("📦 Items to install:")
for name, runtime := range cfg.Config.Runtimes() {
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
logger.Info("Runtime scheduled for installation", logrus.Fields{
"runtime": name,
"version": runtime.Version,
})
fmt.Printf(" • Runtime: %s v%s\n", name, runtime.Version)
}
}
for name, tool := range cfg.Config.Tools() {
if !cfg.Config.IsToolInstalled(name, tool) {
logger.Info("Tool scheduled for installation", logrus.Fields{
"tool": name,
"version": tool.Version,
})
fmt.Printf(" • Tool: %s v%s\n", name, tool.Version)
}
}
fmt.Println()
// Create a single progress bar for the entire installation
progressBar := progressbar.NewOptions(totalItems,
progressbar.OptionSetDescription("Installing components..."),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "█",
SaucerHead: "█",
SaucerPadding: "░",
BarStart: "│",
BarEnd: "│",
}),
progressbar.OptionShowCount(),
progressbar.OptionShowIts(),
progressbar.OptionSetWidth(50),
progressbar.OptionThrottle(100*time.Millisecond),
progressbar.OptionSpinnerType(14),
progressbar.OptionFullWidth(),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionOnCompletion(func() {
fmt.Println()
}),
)
// Redirect all output to /dev/null during installation
oldStdout := os.Stdout
devNull, _ := os.Open(os.DevNull)
os.Stdout = devNull
log.SetOutput(io.Discard)
// Install runtimes first
for name, runtime := range cfg.Config.Runtimes() {
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
progressBar.Describe(fmt.Sprintf("Installing runtime: %s v%s...", name, runtime.Version))
logger.Info("Installing runtime", logrus.Fields{
"runtime": name,
"version": runtime.Version,
})
err := cfg.InstallRuntime(name, runtime)
if err != nil {
logger.Error("Failed to install runtime", logrus.Fields{
"runtime": name,
"version": runtime.Version,
"error": err.Error(),
})
fmt.Printf("\n⚠️ Warning: Failed to install runtime %s v%s: %v\n", name, runtime.Version, err)
// Continue with next runtime instead of fatal
progressBar.Add(1)
continue
}
logger.Info("Successfully installed runtime", logrus.Fields{
"runtime": name,
"version": runtime.Version,
})
progressBar.Add(1)
}
}
// Install tools
for name, tool := range cfg.Config.Tools() {
if !cfg.Config.IsToolInstalled(name, tool) {
progressBar.Describe(fmt.Sprintf("Installing tool: %s v%s...", name, tool.Version))
logger.Info("Installing tool", logrus.Fields{
"tool": name,
"version": tool.Version,
})
err := cfg.InstallTool(name, tool, registry)
if err != nil {
logger.Error("Failed to install tool", logrus.Fields{
"tool": name,
"version": tool.Version,
"error": err.Error(),
})
fmt.Printf("\n⚠️ Warning: Failed to install tool %s v%s: %v\n", name, tool.Version, err)
// Continue with next tool instead of fatal
progressBar.Add(1)
continue
}
logger.Info("Successfully installed tool", logrus.Fields{
"tool": name,
"version": tool.Version,
})
progressBar.Add(1)
}
}
// Restore output
os.Stdout = oldStdout
devNull.Close()
log.SetOutput(os.Stderr)
// Print completion status with warnings for failed installations
fmt.Println()
var hasFailures bool
for name, runtime := range cfg.Config.Runtimes() {
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
color.Yellow(" ⚠️ Runtime: %s v%s (installation failed)", name, runtime.Version)
hasFailures = true
} else {
green.Printf(" ✓ Runtime: %s v%s\n", name, runtime.Version)
}
}
for name, tool := range cfg.Config.Tools() {
if !cfg.Config.IsToolInstalled(name, tool) {
color.Yellow(" ⚠️ Tool: %s v%s (installation failed)", name, tool.Version)
hasFailures = true
} else {
green.Printf(" ✓ Tool: %s v%s\n", name, tool.Version)
}
}
fmt.Println()
if hasFailures {
logger.Warn("Installation completed with some failures", nil)
bold.Println("⚠️ Installation completed with some failures!")
fmt.Println("Some components failed to install. You can try installing them again with:")
fmt.Println(" codacy-cli install")
} else {
logger.Info("Installation completed successfully", nil)
bold.Println("✅ Installation completed successfully!")
}
},
}