Skip to content

Commit 8aed5d8

Browse files
feat: enhance installation checks and user feedback
- Added functions to verify if runtimes and tools are already installed. - Improved installation process to skip already installed components and provide user-friendly messages. - Updated Codacy configuration to use the latest version of Trivy.
1 parent 1749745 commit 8aed5d8

File tree

2 files changed

+106
-17
lines changed

2 files changed

+106
-17
lines changed

cmd/install.go

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,66 @@ var installCmd = &cobra.Command{
3737
log.Fatalf("Failed to load config file: %v", err)
3838
}
3939

40+
// Check if anything needs to be installed
41+
needsInstallation := false
42+
for name, runtime := range cfg.Config.Runtimes() {
43+
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
44+
needsInstallation = true
45+
break
46+
}
47+
}
48+
if !needsInstallation {
49+
for name, tool := range cfg.Config.Tools() {
50+
if !cfg.Config.IsToolInstalled(name, tool) {
51+
needsInstallation = true
52+
break
53+
}
54+
}
55+
}
56+
57+
if !needsInstallation {
58+
fmt.Println()
59+
bold.Println("✅ All components are already installed!")
60+
return
61+
}
62+
4063
fmt.Println()
4164
bold.Println("🚀 Starting installation process...")
4265
fmt.Println()
4366

67+
// Calculate total items to install
68+
totalItems := 0
69+
for name, runtime := range cfg.Config.Runtimes() {
70+
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
71+
totalItems++
72+
}
73+
}
74+
for name, tool := range cfg.Config.Tools() {
75+
if !cfg.Config.IsToolInstalled(name, tool) {
76+
totalItems++
77+
}
78+
}
79+
80+
if totalItems == 0 {
81+
fmt.Println()
82+
bold.Println("✅ All components are already installed!")
83+
return
84+
}
85+
4486
// Print list of items to install
4587
fmt.Println("📦 Items to install:")
4688
for name, runtime := range cfg.Config.Runtimes() {
47-
fmt.Printf(" • Runtime: %s v%s\n", name, runtime.Version)
89+
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
90+
fmt.Printf(" • Runtime: %s v%s\n", name, runtime.Version)
91+
}
4892
}
4993
for name, tool := range cfg.Config.Tools() {
50-
fmt.Printf(" • Tool: %s v%s\n", name, tool.Version)
94+
if !cfg.Config.IsToolInstalled(name, tool) {
95+
fmt.Printf(" • Tool: %s v%s\n", name, tool.Version)
96+
}
5197
}
5298
fmt.Println()
5399

54-
// Calculate total items to install
55-
totalItems := len(cfg.Config.Runtimes()) + len(cfg.Config.Tools())
56-
57100
// Create a single progress bar for the entire installation
58101
progressBar := progressbar.NewOptions(totalItems,
59102
progressbar.OptionSetDescription("Installing components..."),
@@ -84,22 +127,26 @@ var installCmd = &cobra.Command{
84127

85128
// Install runtimes
86129
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)
130+
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
131+
progressBar.Describe(fmt.Sprintf("Installing runtime: %s v%s...", name, runtime.Version))
132+
err := cfg.InstallRuntime(name, runtime)
133+
if err != nil {
134+
log.Fatal(err)
135+
}
136+
progressBar.Add(1)
91137
}
92-
progressBar.Add(1)
93138
}
94139

95140
// Install tools
96141
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)
142+
if !cfg.Config.IsToolInstalled(name, tool) {
143+
progressBar.Describe(fmt.Sprintf("Installing tool: %s v%s...", name, tool.Version))
144+
err := cfg.InstallTool(name, tool)
145+
if err != nil {
146+
log.Fatal(err)
147+
}
148+
progressBar.Add(1)
101149
}
102-
progressBar.Add(1)
103150
}
104151

105152
// Restore output
@@ -110,10 +157,14 @@ var installCmd = &cobra.Command{
110157
// Print completion status
111158
fmt.Println()
112159
for name, runtime := range cfg.Config.Runtimes() {
113-
green.Printf(" ✓ Runtime: %s v%s\n", name, runtime.Version)
160+
if !cfg.Config.IsRuntimeInstalled(name, runtime) {
161+
green.Printf(" ✓ Runtime: %s v%s\n", name, runtime.Version)
162+
}
114163
}
115164
for name, tool := range cfg.Config.Tools() {
116-
green.Printf(" ✓ Tool: %s v%s\n", name, tool.Version)
165+
if !cfg.Config.IsToolInstalled(name, tool) {
166+
green.Printf(" ✓ Tool: %s v%s\n", name, tool.Version)
167+
}
117168
}
118169
fmt.Println()
119170
bold.Println("✅ Installation completed successfully!")

config/config.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,43 @@ func Init() {
130130
Config.tools = make(map[string]*plugins.ToolInfo)
131131
}
132132

133+
// IsRuntimeInstalled checks if a runtime is already installed
134+
func (c *ConfigType) IsRuntimeInstalled(name string, runtime *plugins.RuntimeInfo) bool {
135+
// If there are no binaries, check the install directory
136+
if len(runtime.Binaries) == 0 {
137+
_, err := os.Stat(runtime.InstallDir)
138+
return err == nil
139+
}
140+
141+
// Check if at least one binary exists
142+
for _, binaryPath := range runtime.Binaries {
143+
_, err := os.Stat(binaryPath)
144+
if err == nil {
145+
return true
146+
}
147+
}
148+
149+
return false
150+
}
151+
152+
// IsToolInstalled checks if a tool is already installed
153+
func (c *ConfigType) IsToolInstalled(name string, tool *plugins.ToolInfo) bool {
154+
// If there are no binaries, check the install directory
155+
if len(tool.Binaries) == 0 {
156+
_, err := os.Stat(tool.InstallDir)
157+
return err == nil
158+
}
159+
160+
// Check if at least one binary exists
161+
for _, binaryPath := range tool.Binaries {
162+
_, err := os.Stat(binaryPath)
163+
if err == nil {
164+
return true
165+
}
166+
}
167+
168+
return false
169+
}
170+
133171
// Global singleton config-file
134172
var Config = ConfigType{}

0 commit comments

Comments
 (0)