Skip to content

Commit b1f3a84

Browse files
committed
Factored function for cloning libraries repositories
1 parent a8423ab commit b1f3a84

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

commands/service_library_install.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,8 @@ func (s *arduinoCoreServerImpl) GitLibraryInstall(req *rpc.GitLibraryInstallRequ
267267
lmi, release := lm.NewInstaller()
268268
defer release()
269269

270-
// TODO: pass context
271-
// ctx := stream.Context()
272-
if err := lmi.InstallGitLib(req.GetUrl(), req.GetOverwrite()); err != nil {
270+
ctx := stream.Context()
271+
if err := lmi.InstallGitLib(ctx, req.GetUrl(), req.GetOverwrite()); err != nil {
273272
return &cmderrors.FailedLibraryInstallError{Cause: err}
274273
}
275274
taskCB(&rpc.TaskProgress{Message: i18n.Tr("Library installed"), Completed: true})

internal/arduino/libraries/librariesmanager/install.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,54 +200,64 @@ func (lmi *Installer) InstallZipLib(ctx context.Context, archivePath *paths.Path
200200
}
201201

202202
// InstallGitLib installs a library hosted on a git repository on the specified path.
203-
func (lmi *Installer) InstallGitLib(argURL string, overwrite bool) error {
204-
libraryName, gitURL, ref, err := parseGitArgURL(argURL)
203+
func (lmi *Installer) InstallGitLib(ctx context.Context, argURL string, overwrite bool) error {
204+
tmpInstallPath, err := CloneLibraryGitRepository(ctx, argURL)
205205
if err != nil {
206206
return err
207207
}
208+
defer tmpInstallPath.RemoveAll()
209+
210+
// Install extracted library in the destination directory
211+
if err := lmi.importLibraryFromDirectory(tmpInstallPath, overwrite); err != nil {
212+
return errors.New(i18n.Tr("moving extracted archive to destination dir: %s", err))
213+
}
214+
215+
return nil
216+
}
217+
218+
// CloneLibraryGitRepository clones a git repository containing a library
219+
// into a temporary directory and returns the path to the cloned library.
220+
func CloneLibraryGitRepository(ctx context.Context, argURL string) (*paths.Path, error) {
221+
libraryName, gitURL, ref, err := parseGitArgURL(argURL)
222+
if err != nil {
223+
return nil, err
224+
}
208225

209226
// Clone library in a temporary directory
210227
tmp, err := paths.MkTempDir("", "")
211228
if err != nil {
212-
return err
229+
return nil, err
213230
}
214-
defer tmp.RemoveAll()
215231
tmpInstallPath := tmp.Join(libraryName)
216232

217-
if _, err := git.PlainClone(tmpInstallPath.String(), false, &git.CloneOptions{
233+
if _, err := git.PlainCloneContext(ctx, tmpInstallPath.String(), false, &git.CloneOptions{
218234
URL: gitURL,
219235
ReferenceName: plumbing.ReferenceName(ref),
220236
}); err != nil {
221237
if err.Error() != "reference not found" {
222-
return err
238+
return nil, err
223239
}
224240

225241
// We did not find the requested reference, let's do a PlainClone and use
226242
// "ResolveRevision" to find and checkout the requested revision
227-
if repo, err := git.PlainClone(tmpInstallPath.String(), false, &git.CloneOptions{
243+
if repo, err := git.PlainCloneContext(ctx, tmpInstallPath.String(), false, &git.CloneOptions{
228244
URL: gitURL,
229245
}); err != nil {
230-
return err
246+
return nil, err
231247
} else if h, err := repo.ResolveRevision(plumbing.Revision(ref)); err != nil {
232-
return err
248+
return nil, err
233249
} else if w, err := repo.Worktree(); err != nil {
234-
return err
250+
return nil, err
235251
} else if err := w.Checkout(&git.CheckoutOptions{
236252
Force: true, // workaround for: https://github.com/go-git/go-git/issues/1411
237253
Hash: plumbing.NewHash(h.String())}); err != nil {
238-
return err
254+
return nil, err
239255
}
240256
}
241257

242258
// We don't want the installed library to be a git repository thus we delete this folder
243259
tmpInstallPath.Join(".git").RemoveAll()
244-
245-
// Install extracted library in the destination directory
246-
if err := lmi.importLibraryFromDirectory(tmpInstallPath, overwrite); err != nil {
247-
return errors.New(i18n.Tr("moving extracted archive to destination dir: %s", err))
248-
}
249-
250-
return nil
260+
return tmpInstallPath, nil
251261
}
252262

253263
// parseGitArgURL tries to recover a library name from a git URL.

0 commit comments

Comments
 (0)