@@ -94,7 +94,7 @@ func (lm *LibrariesManager) Uninstall(lib *libraries.Library) error {
94
94
}
95
95
96
96
//InstallZipLib installs a Zip library on the specified path.
97
- func (lm * LibrariesManager ) InstallZipLib (ctx context.Context , archivePath string ) error {
97
+ func (lm * LibrariesManager ) InstallZipLib (ctx context.Context , archivePath string , overwrite bool ) error {
98
98
libsDir := lm .getUserLibrariesDir ()
99
99
if libsDir == nil {
100
100
return fmt .Errorf ("User directory not set" )
@@ -104,6 +104,8 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
104
104
if err != nil {
105
105
return err
106
106
}
107
+ // Deletes temp dir used to extract archive when finished
108
+ defer tmpDir .RemoveAll ()
107
109
108
110
file , err := os .Open (archivePath )
109
111
if err != nil {
@@ -126,11 +128,26 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
126
128
return fmt .Errorf ("archive is not valid: multiple files found in zip file top level" )
127
129
}
128
130
129
- libraryName := paths [0 ].Base ()
131
+ extractionPath := paths [0 ]
132
+ libraryName := extractionPath .Base ()
130
133
installPath := libsDir .Join (libraryName )
131
134
132
- // Deletes libraries folder if already installed
133
- if _ , ok := lm .Libraries [libraryName ]; ok {
135
+ if err := libsDir .MkdirAll (); err != nil {
136
+ return err
137
+ }
138
+ defer func () {
139
+ // Clean up install dir if installation failed
140
+ files , err := installPath .ReadDir ()
141
+ if err == nil && len (files ) == 0 {
142
+ installPath .RemoveAll ()
143
+ }
144
+ }()
145
+
146
+ // Delete library folder if already installed
147
+ if installPath .IsDir () {
148
+ if ! overwrite {
149
+ return fmt .Errorf ("library %s already installed" , libraryName )
150
+ }
134
151
logrus .
135
152
WithField ("library name" , libraryName ).
136
153
WithField ("install path" , installPath ).
@@ -144,15 +161,16 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
144
161
WithField ("zip file" , archivePath ).
145
162
Trace ("Installing library" )
146
163
147
- if err := tmpDir .Join (libraryName ).CopyDirTo (installPath ); err != nil {
148
- return fmt .Errorf ("copying library: %w" , err )
164
+ // Copy extracted library in the destination directory
165
+ if err := extractionPath .CopyDirTo (installPath ); err != nil {
166
+ return fmt .Errorf ("moving extracted archive to destination dir: %s" , err )
149
167
}
150
168
151
169
return nil
152
170
}
153
171
154
172
//InstallGitLib installs a library hosted on a git repository on the specified path.
155
- func (lm * LibrariesManager ) InstallGitLib (gitURL string ) error {
173
+ func (lm * LibrariesManager ) InstallGitLib (gitURL string , overwrite bool ) error {
156
174
libsDir := lm .getUserLibrariesDir ()
157
175
if libsDir == nil {
158
176
return fmt .Errorf ("User directory not set" )
@@ -170,6 +188,9 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string) error {
170
188
171
189
// Deletes libraries folder if already installed
172
190
if _ , ok := lm .Libraries [libraryName ]; ok {
191
+ if ! overwrite {
192
+ return fmt .Errorf ("library %s already installed" , libraryName )
193
+ }
173
194
logrus .
174
195
WithField ("library name" , libraryName ).
175
196
WithField ("install path" , installPath ).
0 commit comments