@@ -19,6 +19,35 @@ import (
1919 "github.com/two-tech-dev/endgit-cli/internal/common"
2020)
2121
22+ func downloadAndSave (s * spinner.Spinner , client * api.Client , url string , filename string ) {
23+ if err := os .MkdirAll ("plugins" , 0755 ); err != nil {
24+ s .Stop ()
25+ color .Red ("Failed to create plugins directory: %v" , err )
26+ os .Exit (1 )
27+ }
28+
29+ file := filepath .Join ("plugins" , filename )
30+ baseSuffix := s .Suffix
31+
32+ onProgress := func (downloaded , total int64 ) {
33+ if total > 0 {
34+ percent := float64 (downloaded ) / float64 (total ) * 100
35+ s .Suffix = fmt .Sprintf ("%s (%.1f%% of %.2fMB)" , baseSuffix , percent , float64 (total )/ (1024 * 1024 ))
36+ } else {
37+ s .Suffix = fmt .Sprintf ("%s (%.2fMB)" , baseSuffix , float64 (downloaded )/ (1024 * 1024 ))
38+ }
39+ }
40+
41+ if err := client .DownloadFile (url , file , onProgress ); err != nil {
42+ s .Stop ()
43+ color .Red ("Download failed: %v" , err )
44+ os .Exit (1 )
45+ }
46+
47+ s .Stop ()
48+ color .HiBlack ("Saved to: %s" , file )
49+ }
50+
2251var installCmd = & cobra.Command {
2352 Use : "install <plugin[@version|@commit]>" ,
2453 Short : "Download and install a plugin to the current directory" ,
@@ -49,6 +78,8 @@ var installCmd = &cobra.Command{
4978 ext := ".so"
5079 if runtime .GOOS == "windows" {
5180 ext = ".dll"
81+ } else if runtime .GOOS == "darwin" {
82+ ext = ".dylib"
5283 }
5384
5485 // =========================
@@ -83,28 +114,11 @@ var installCmd = &cobra.Command{
83114
84115 s .Suffix = fmt .Sprintf (" Downloading build #%d..." , target .BuildNumber )
85116
86- url := common .ResolveArtifactURL (target )
87-
88- data , err := common .DownloadFile (url )
89- if err != nil {
90- s .Stop ()
91- color .Red ("Download failed: %v" , err )
92- return
93- }
94-
95- _ = os .MkdirAll ("plugins" , 0755 )
96-
97- file := filepath .Join (
98- "plugins" ,
99- fmt .Sprintf ("%s-build%d-%s%s" , plugin , target .BuildNumber , commit [:7 ], ext ),
100- )
101-
102- _ = os .WriteFile (file , data , 0644 )
103-
104- s .Stop ()
105-
117+ url := target .ResolveArtifactURL ()
118+ filename := fmt .Sprintf ("%s-build%d-%s%s" , plugin , target .BuildNumber , commit [:7 ], ext )
119+
120+ downloadAndSave (s , client , url , filename )
106121 color .Green ("Installed dev build %s #%d" , plugin , target .BuildNumber )
107- color .HiBlack ("Saved to: %s" , file )
108122 return
109123 }
110124
@@ -124,26 +138,9 @@ var installCmd = &cobra.Command{
124138 runtime .GOOS ,
125139 )
126140
127- data , err := common .DownloadFile (downloadURL )
128- if err != nil {
129- s .Stop ()
130- color .Red ("Download failed: %v" , err )
131- return
132- }
133-
134- _ = os .MkdirAll ("plugins" , 0755 )
135-
136- file := filepath .Join (
137- "plugins" ,
138- fmt .Sprintf ("%s-%s%s" , plugin , version , ext ),
139- )
140-
141- _ = os .WriteFile (file , data , 0644 )
142-
143- s .Stop ()
144-
141+ filename := fmt .Sprintf ("%s-%s%s" , plugin , version , ext )
142+ downloadAndSave (s , client , downloadURL , filename )
145143 color .Green ("Installed %s@%s" , plugin , version )
146- color .HiBlack ("Saved to: %s" , file )
147144 return
148145 }
149146
@@ -166,6 +163,8 @@ var installCmd = &cobra.Command{
166163 return
167164 }
168165
166+ s .Suffix = fmt .Sprintf (" Downloading %s@%s..." , plugin , p .LatestVersion )
167+
169168 downloadURL := fmt .Sprintf (
170169 "%s/download/%s/%s?platform=%s" ,
171170 client .BaseURL ,
@@ -174,26 +173,9 @@ var installCmd = &cobra.Command{
174173 runtime .GOOS ,
175174 )
176175
177- data , err := common .DownloadFile (downloadURL )
178- if err != nil {
179- s .Stop ()
180- color .Red ("Download failed: %v" , err )
181- return
182- }
183-
184- _ = os .MkdirAll ("plugins" , 0755 )
185-
186- file := filepath .Join (
187- "plugins" ,
188- fmt .Sprintf ("%s-%s%s" , plugin , p .LatestVersion , ext ),
189- )
190-
191- _ = os .WriteFile (file , data , 0644 )
192-
193- s .Stop ()
194-
176+ filename := fmt .Sprintf ("%s-%s%s" , plugin , p .LatestVersion , ext )
177+ downloadAndSave (s , client , downloadURL , filename )
195178 color .Green ("Installed %s@%s" , plugin , p .LatestVersion )
196- color .HiBlack ("Saved to: %s" , file )
197179 },
198180}
199181
0 commit comments