Skip to content

Commit 1749745

Browse files
feature: installation progress bar
1 parent 8724a50 commit 1749745

File tree

4 files changed

+96
-10
lines changed

4 files changed

+96
-10
lines changed

cmd/install.go

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import (
44
cfg "codacy/cli-v2/config"
55
config_file "codacy/cli-v2/config-file"
66
"fmt"
7+
"io"
78
"log"
9+
"os"
10+
"time"
811

912
"github.com/fatih/color"
13+
"github.com/schollz/progressbar/v3"
1014
"github.com/spf13/cobra"
1115
)
1216

@@ -22,8 +26,8 @@ var installCmd = &cobra.Command{
2226
Short: "Installs the tools specified in the project's config-file.",
2327
Long: "Installs all runtimes and tools specified in the project's config-file file.",
2428
Run: func(cmd *cobra.Command, args []string) {
25-
cyan := color.New(color.FgCyan)
2629
bold := color.New(color.Bold)
30+
green := color.New(color.FgGreen)
2731

2832
// Initialize config
2933
cfg.Init()
@@ -37,12 +41,80 @@ var installCmd = &cobra.Command{
3741
bold.Println("🚀 Starting installation process...")
3842
fmt.Println()
3943

40-
cyan.Println("Installing runtimes...")
41-
installRuntimes(&cfg.Config)
44+
// Print list of items to install
45+
fmt.Println("📦 Items to install:")
46+
for name, runtime := range cfg.Config.Runtimes() {
47+
fmt.Printf(" • Runtime: %s v%s\n", name, runtime.Version)
48+
}
49+
for name, tool := range cfg.Config.Tools() {
50+
fmt.Printf(" • Tool: %s v%s\n", name, tool.Version)
51+
}
52+
fmt.Println()
53+
54+
// Calculate total items to install
55+
totalItems := len(cfg.Config.Runtimes()) + len(cfg.Config.Tools())
4256

43-
cyan.Println("\nInstalling tools...")
44-
installTools(&cfg.Config)
57+
// Create a single progress bar for the entire installation
58+
progressBar := progressbar.NewOptions(totalItems,
59+
progressbar.OptionSetDescription("Installing components..."),
60+
progressbar.OptionSetTheme(progressbar.Theme{
61+
Saucer: "█",
62+
SaucerHead: "█",
63+
SaucerPadding: "░",
64+
BarStart: "│",
65+
BarEnd: "│",
66+
}),
67+
progressbar.OptionShowCount(),
68+
progressbar.OptionShowIts(),
69+
progressbar.OptionSetWidth(50),
70+
progressbar.OptionThrottle(100*time.Millisecond),
71+
progressbar.OptionSpinnerType(14),
72+
progressbar.OptionFullWidth(),
73+
progressbar.OptionSetRenderBlankState(true),
74+
progressbar.OptionOnCompletion(func() {
75+
fmt.Println()
76+
}),
77+
)
78+
79+
// Redirect all output to /dev/null during installation
80+
oldStdout := os.Stdout
81+
devNull, _ := os.Open(os.DevNull)
82+
os.Stdout = devNull
83+
log.SetOutput(io.Discard)
84+
85+
// Install runtimes
86+
for name, runtime := range cfg.Config.Runtimes() {
87+
progressBar.Describe(fmt.Sprintf("Installing runtime: %s v%s...", name, runtime.Version))
88+
err := cfg.InstallRuntime(name, runtime)
89+
if err != nil {
90+
log.Fatal(err)
91+
}
92+
progressBar.Add(1)
93+
}
4594

95+
// Install tools
96+
for name, tool := range cfg.Config.Tools() {
97+
progressBar.Describe(fmt.Sprintf("Installing tool: %s v%s...", name, tool.Version))
98+
err := cfg.InstallTool(name, tool)
99+
if err != nil {
100+
log.Fatal(err)
101+
}
102+
progressBar.Add(1)
103+
}
104+
105+
// Restore output
106+
os.Stdout = oldStdout
107+
devNull.Close()
108+
log.SetOutput(os.Stderr)
109+
110+
// Print completion status
111+
fmt.Println()
112+
for name, runtime := range cfg.Config.Runtimes() {
113+
green.Printf(" ✓ Runtime: %s v%s\n", name, runtime.Version)
114+
}
115+
for name, tool := range cfg.Config.Tools() {
116+
green.Printf(" ✓ Tool: %s v%s\n", name, tool.Version)
117+
}
46118
fmt.Println()
47119
bold.Println("✅ Installation completed successfully!")
48120
},

config/runtimes-installer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func isRuntimeInstalled(runtimeInfo *plugins.RuntimeInfo) bool {
5757
return false
5858
}
5959

60-
// downloadAndExtractRuntime downloads and extracts a runtime
60+
// downloadAndExtractRuntime downloads and extracts a runtime
6161
func downloadAndExtractRuntime(runtimeInfo *plugins.RuntimeInfo) error {
6262
// Create a file name for the downloaded archive
6363
fileName := filepath.Base(runtimeInfo.DownloadURL)
@@ -67,7 +67,7 @@ func downloadAndExtractRuntime(runtimeInfo *plugins.RuntimeInfo) error {
6767
_, err := os.Stat(downloadPath)
6868
if os.IsNotExist(err) {
6969
// Download the file
70-
log.Printf("Downloading %s v%s...\n", runtimeInfo.Name, runtimeInfo.Version)
70+
// log.Printf("Downloading %s v%s...\n", runtimeInfo.Name, runtimeInfo.Version)
7171
downloadPath, err = utils.DownloadFile(runtimeInfo.DownloadURL, Config.RuntimesDirectory())
7272
if err != nil {
7373
return fmt.Errorf("failed to download runtime: %w", err)
@@ -86,7 +86,7 @@ func downloadAndExtractRuntime(runtimeInfo *plugins.RuntimeInfo) error {
8686
defer file.Close()
8787

8888
// Extract based on file extension
89-
log.Printf("Extracting %s v%s...\n", runtimeInfo.Name, runtimeInfo.Version)
89+
// log.Printf("Extracting %s v%s...\n", runtimeInfo.Name, runtimeInfo.Version)
9090
if strings.HasSuffix(fileName, ".zip") {
9191
err = utils.ExtractZip(file.Name(), Config.RuntimesDirectory())
9292
} else {
@@ -99,4 +99,4 @@ func downloadAndExtractRuntime(runtimeInfo *plugins.RuntimeInfo) error {
9999

100100
log.Printf("Successfully installed %s v%s\n", runtimeInfo.Name, runtimeInfo.Version)
101101
return nil
102-
}
102+
}

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ require (
99
github.com/fatih/color v1.18.0 // indirect
1010
github.com/mattn/go-colorable v0.1.13 // indirect
1111
github.com/mattn/go-isatty v0.0.20 // indirect
12+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
1213
github.com/pmezard/go-difflib v1.0.0 // indirect
13-
golang.org/x/sys v0.25.0 // indirect
14+
github.com/rivo/uniseg v0.4.7 // indirect
15+
github.com/schollz/progressbar/v3 v3.18.0 // indirect
16+
golang.org/x/sys v0.29.0 // indirect
17+
golang.org/x/term v0.28.0 // indirect
1418
)
1519

1620
require (

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,22 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
104104
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
105105
github.com/mholt/archiver/v4 v4.0.0-alpha.8 h1:tRGQuDVPh66WCOelqe6LIGh0gwmfwxUrSSDunscGsRM=
106106
github.com/mholt/archiver/v4 v4.0.0-alpha.8/go.mod h1:5f7FUYGXdJWUjESffJaYR4R60VhnHxb2X3T1teMyv5A=
107+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
108+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
107109
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 h1:e3mzJFJs4k83GXBEiTaQ5HgSc/kOK8q0rDaRO0MPaOk=
108110
github.com/nwaples/rardecode/v2 v2.0.0-beta.2/go.mod h1:yntwv/HfMc/Hbvtq9I19D1n58te3h6KsqCf3GxyfBGY=
109111
github.com/pierrec/lz4/v4 v4.1.15 h1:MO0/ucJhngq7299dKLwIMtgTfbkoSPF6AoMYDd8Q4q0=
110112
github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
111113
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
112114
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
113115
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
116+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
117+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
114118
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
115119
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
116120
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
121+
github.com/schollz/progressbar/v3 v3.18.0 h1:uXdoHABRFmNIjUfte/Ex7WtuyVslrw2wVPQmCN62HpA=
122+
github.com/schollz/progressbar/v3 v3.18.0/go.mod h1:IsO3lpbaGuzh8zIMzgY3+J8l4C8GjO0Y9S69eFvNsec=
117123
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
118124
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
119125
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
@@ -205,6 +211,10 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
205211
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
206212
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
207213
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
214+
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
215+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
216+
golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg=
217+
golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek=
208218
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
209219
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
210220
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 commit comments

Comments
 (0)