11package cmd
22
33import (
4+ "codacy/cli-v2/config"
45 cfg "codacy/cli-v2/config"
6+ config_file "codacy/cli-v2/config-file"
7+ "fmt"
8+ "io"
59 "log"
10+ "os"
11+ "time"
612
13+ "github.com/fatih/color"
14+ "github.com/schollz/progressbar/v3"
715 "github.com/spf13/cobra"
816)
917
@@ -19,8 +27,154 @@ var installCmd = &cobra.Command{
1927 Short : "Installs the tools specified in the project's config-file." ,
2028 Long : "Installs all runtimes and tools specified in the project's config-file file." ,
2129 Run : func (cmd * cobra.Command , args []string ) {
22- installRuntimes (& cfg .Config )
23- installTools (& cfg .Config )
30+ bold := color .New (color .Bold )
31+ green := color .New (color .FgGreen )
32+
33+ // Create necessary directories
34+ if err := config .Config .CreateCodacyDirs (); err != nil {
35+ log .Fatal (err )
36+ }
37+
38+ // Load config file
39+ if err := config_file .ReadConfigFile (cfg .Config .ProjectConfigFile ()); err != nil {
40+ fmt .Println ()
41+ color .Red ("⚠️ Warning: Could not find configuration file!" )
42+ fmt .Println ("Please run 'codacy-cli init' first to create a configuration file." )
43+ fmt .Println ()
44+ os .Exit (1 )
45+ }
46+
47+ // Check if anything needs to be installed
48+ needsInstallation := false
49+ for name , runtime := range cfg .Config .Runtimes () {
50+ if ! cfg .Config .IsRuntimeInstalled (name , runtime ) {
51+ needsInstallation = true
52+ break
53+ }
54+ }
55+ if ! needsInstallation {
56+ for name , tool := range cfg .Config .Tools () {
57+ if ! cfg .Config .IsToolInstalled (name , tool ) {
58+ needsInstallation = true
59+ break
60+ }
61+ }
62+ }
63+
64+ if ! needsInstallation {
65+ fmt .Println ()
66+ bold .Println ("✅ All components are already installed!" )
67+ return
68+ }
69+
70+ fmt .Println ()
71+ bold .Println ("🚀 Starting installation process..." )
72+ fmt .Println ()
73+
74+ // Calculate total items to install
75+ totalItems := 0
76+ for name , runtime := range cfg .Config .Runtimes () {
77+ if ! cfg .Config .IsRuntimeInstalled (name , runtime ) {
78+ totalItems ++
79+ }
80+ }
81+ for name , tool := range cfg .Config .Tools () {
82+ if ! cfg .Config .IsToolInstalled (name , tool ) {
83+ totalItems ++
84+ }
85+ }
86+
87+ if totalItems == 0 {
88+ fmt .Println ()
89+ bold .Println ("✅ All components are already installed!" )
90+ return
91+ }
92+
93+ // Print list of items to install
94+ fmt .Println ("📦 Items to install:" )
95+ for name , runtime := range cfg .Config .Runtimes () {
96+ if ! cfg .Config .IsRuntimeInstalled (name , runtime ) {
97+ fmt .Printf (" • Runtime: %s v%s\n " , name , runtime .Version )
98+ }
99+ }
100+ for name , tool := range cfg .Config .Tools () {
101+ if ! cfg .Config .IsToolInstalled (name , tool ) {
102+ fmt .Printf (" • Tool: %s v%s\n " , name , tool .Version )
103+ }
104+ }
105+ fmt .Println ()
106+
107+ // Create a single progress bar for the entire installation
108+ progressBar := progressbar .NewOptions (totalItems ,
109+ progressbar .OptionSetDescription ("Installing components..." ),
110+ progressbar .OptionSetTheme (progressbar.Theme {
111+ Saucer : "█" ,
112+ SaucerHead : "█" ,
113+ SaucerPadding : "░" ,
114+ BarStart : "│" ,
115+ BarEnd : "│" ,
116+ }),
117+ progressbar .OptionShowCount (),
118+ progressbar .OptionShowIts (),
119+ progressbar .OptionSetWidth (50 ),
120+ progressbar .OptionThrottle (100 * time .Millisecond ),
121+ progressbar .OptionSpinnerType (14 ),
122+ progressbar .OptionFullWidth (),
123+ progressbar .OptionSetRenderBlankState (true ),
124+ progressbar .OptionOnCompletion (func () {
125+ fmt .Println ()
126+ }),
127+ )
128+
129+ // Redirect all output to /dev/null during installation
130+ oldStdout := os .Stdout
131+ devNull , _ := os .Open (os .DevNull )
132+ os .Stdout = devNull
133+ log .SetOutput (io .Discard )
134+
135+ // Install runtimes first
136+ for name , runtime := range cfg .Config .Runtimes () {
137+ if ! cfg .Config .IsRuntimeInstalled (name , runtime ) {
138+ progressBar .Describe (fmt .Sprintf ("Installing runtime: %s v%s..." , name , runtime .Version ))
139+ err := cfg .InstallRuntime (name , runtime )
140+ if err != nil {
141+ log .Fatal (err )
142+ }
143+ progressBar .Add (1 )
144+ }
145+ }
146+
147+ // Install tools
148+ for name , tool := range cfg .Config .Tools () {
149+ if ! cfg .Config .IsToolInstalled (name , tool ) {
150+ progressBar .Describe (fmt .Sprintf ("Installing tool: %s v%s..." , name , tool .Version ))
151+ err := cfg .InstallTool (name , tool )
152+ if err != nil {
153+ log .Fatal (err )
154+ }
155+ progressBar .Add (1 )
156+ }
157+ }
158+
159+ // Restore output
160+ os .Stdout = oldStdout
161+ devNull .Close ()
162+ log .SetOutput (os .Stderr )
163+
164+ // Print completion status
165+ fmt .Println ()
166+ for name , runtime := range cfg .Config .Runtimes () {
167+ if ! cfg .Config .IsRuntimeInstalled (name , runtime ) {
168+ green .Printf (" ✓ Runtime: %s v%s\n " , name , runtime .Version )
169+ }
170+ }
171+ for name , tool := range cfg .Config .Tools () {
172+ if ! cfg .Config .IsToolInstalled (name , tool ) {
173+ green .Printf (" ✓ Tool: %s v%s\n " , name , tool .Version )
174+ }
175+ }
176+ fmt .Println ()
177+ bold .Println ("✅ Installation completed successfully!" )
24178 },
25179}
26180
@@ -37,4 +191,4 @@ func installTools(config *cfg.ConfigType) {
37191 if err != nil {
38192 log .Fatal (err )
39193 }
40- }
194+ }
0 commit comments