@@ -94,7 +94,7 @@ func (lm *LibrariesManager) Uninstall(lib *libraries.Library) error {
9494}
9595
9696//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 {
9898 libsDir := lm .getUserLibrariesDir ()
9999 if libsDir == nil {
100100 return fmt .Errorf ("User directory not set" )
@@ -104,6 +104,8 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
104104 if err != nil {
105105 return err
106106 }
107+ // Deletes temp dir used to extract archive when finished
108+ defer tmpDir .RemoveAll ()
107109
108110 file , err := os .Open (archivePath )
109111 if err != nil {
@@ -126,11 +128,26 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
126128 return fmt .Errorf ("archive is not valid: multiple files found in zip file top level" )
127129 }
128130
129- libraryName := paths [0 ].Base ()
131+ extractionPath := paths [0 ]
132+ libraryName := extractionPath .Base ()
130133 installPath := libsDir .Join (libraryName )
131134
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+ }
134151 logrus .
135152 WithField ("library name" , libraryName ).
136153 WithField ("install path" , installPath ).
@@ -144,15 +161,16 @@ func (lm *LibrariesManager) InstallZipLib(ctx context.Context, archivePath strin
144161 WithField ("zip file" , archivePath ).
145162 Trace ("Installing library" )
146163
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 )
149167 }
150168
151169 return nil
152170}
153171
154172//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 {
156174 libsDir := lm .getUserLibrariesDir ()
157175 if libsDir == nil {
158176 return fmt .Errorf ("User directory not set" )
@@ -170,6 +188,9 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string) error {
170188
171189 // Deletes libraries folder if already installed
172190 if _ , ok := lm .Libraries [libraryName ]; ok {
191+ if ! overwrite {
192+ return fmt .Errorf ("library %s already installed" , libraryName )
193+ }
173194 logrus .
174195 WithField ("library name" , libraryName ).
175196 WithField ("install path" , installPath ).
0 commit comments