Skip to content

Commit a76f7ee

Browse files
authored
Merge pull request #125 from per1234/handle-missing-clone
Don't error maintenance commands on missing library clone folder
2 parents 4fd9cf7 + 893e86f commit a76f7ee

File tree

4 files changed

+72
-20
lines changed

4 files changed

+72
-20
lines changed

internal/command/modify/modify.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,9 @@ func modifyRepositoryURL(newRepositoryURL string) error {
164164
fmt.Printf("Changing URL of library %s from %s to %s\n", libraryName, oldRepositoryURL, newRepositoryURL)
165165

166166
// Remove the library Git clone folder. It will be cloned from the new URL on the next sync.
167-
libraryRegistration := libraries.Repo{URL: libraryData.Repository}
168-
gitCloneSubfolder, err := libraryRegistration.AsFolder()
169-
if err != nil {
167+
if err := libraries.BackupAndDeleteGitClone(config, &libraries.Repo{URL: libraryData.Repository}); err != nil {
170168
return err
171169
}
172-
gitClonePath := paths.New(config.GitClonesFolder, gitCloneSubfolder)
173-
if err := backup.Backup(gitClonePath); err != nil {
174-
return fmt.Errorf("While backing up library's Git clone: %w", err)
175-
}
176-
if err := gitClonePath.RemoveAll(); err != nil {
177-
return fmt.Errorf("While removing library's Git clone: %w", err)
178-
}
179170

180171
// Update the library repository URL in the database.
181172
libraryData.Repository = newRepositoryURL

internal/command/remove/remove.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,9 @@ func removeLibrary(libraryName string) error {
160160
}
161161

162162
// Remove the library Git clone folder.
163-
libraryRegistration := libraries.Repo{URL: libraryData.Repository}
164-
gitCloneSubfolder, err := libraryRegistration.AsFolder()
165-
if err != nil {
163+
if err := libraries.BackupAndDeleteGitClone(config, &libraries.Repo{URL: libraryData.Repository}); err != nil {
166164
return err
167165
}
168-
gitClonePath := paths.New(config.GitClonesFolder, gitCloneSubfolder)
169-
if err := backup.Backup(gitClonePath); err != nil {
170-
return fmt.Errorf("While backing up library's Git clone: %w", err)
171-
}
172-
if err := gitClonePath.RemoveAll(); err != nil {
173-
return fmt.Errorf("While removing library Git clone: %s", err)
174-
}
175166

176167
return nil
177168
}

internal/libraries/repoclone.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import (
2828
"os"
2929
"path/filepath"
3030

31+
"github.com/arduino/go-paths-helper"
32+
"github.com/arduino/libraries-repository-engine/internal/backup"
33+
"github.com/arduino/libraries-repository-engine/internal/configuration"
34+
"github.com/arduino/libraries-repository-engine/internal/feedback"
3135
"github.com/arduino/libraries-repository-engine/internal/libraries/db"
3236

3337
"fmt"
@@ -134,3 +138,30 @@ func UpdateLibrary(release *db.Release, repoURL string, libraryDb *db.DB) error
134138

135139
return nil
136140
}
141+
142+
// BackupAndDeleteGitClone backs up and then deletes the library's Git clone folder.
143+
func BackupAndDeleteGitClone(config *configuration.Config, repoMeta *Repo) error {
144+
gitCloneSubfolder, err := repoMeta.AsFolder()
145+
if err != nil {
146+
return err
147+
}
148+
gitClonePath := paths.New(config.GitClonesFolder, gitCloneSubfolder)
149+
// The library's clone folder may be removed by the sync process under expected circumstances.
150+
// So its absence does not necessarily imply a problem.
151+
gitClonePathExists, err := gitClonePath.ExistCheck()
152+
if err != nil {
153+
return err
154+
}
155+
if gitClonePathExists {
156+
if err := backup.Backup(gitClonePath); err != nil {
157+
return fmt.Errorf("While backing up library's Git clone: %w", err)
158+
}
159+
if err := gitClonePath.RemoveAll(); err != nil {
160+
return fmt.Errorf("While removing library Git clone: %s", err)
161+
}
162+
} else {
163+
feedback.Warningf("Library Git clone folder %s not present", gitClonePath)
164+
}
165+
166+
return nil
167+
}

internal/libraries/repoclone_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import (
2828
"path/filepath"
2929
"testing"
3030

31+
"github.com/arduino/go-paths-helper"
32+
"github.com/arduino/libraries-repository-engine/internal/backup"
33+
"github.com/arduino/libraries-repository-engine/internal/configuration"
34+
"github.com/stretchr/testify/assert"
3135
"github.com/stretchr/testify/require"
3236
)
3337

@@ -48,3 +52,38 @@ func TestCloneRepos(t *testing.T) {
4852
_, err = os.Stat(repo.FolderPath)
4953
require.NoError(t, err)
5054
}
55+
56+
func TestBackupAndDeleteGitClone(t *testing.T) {
57+
var err error
58+
59+
gitClonesFolder, err := paths.MkTempDir("", "libraries-test-testbackupanddeletegitclone")
60+
require.NoError(t, err)
61+
config := configuration.Config{
62+
GitClonesFolder: gitClonesFolder.String(),
63+
}
64+
repoMeta := Repo{
65+
URL: "https://github.com/Foo/Bar.git",
66+
}
67+
68+
assert.Nil(t, BackupAndDeleteGitClone(&config, &repoMeta), "Return nil if library clone folder did not exist.")
69+
70+
gitCloneSubfolder, err := repoMeta.AsFolder()
71+
require.NoError(t, err)
72+
gitClonePath := paths.New(config.GitClonesFolder, gitCloneSubfolder)
73+
err = gitClonePath.MkdirAll()
74+
require.NoError(t, err)
75+
76+
assert.Nil(t, BackupAndDeleteGitClone(&config, &repoMeta), "Return nil if library clone folder did exist.")
77+
78+
exist, err := gitClonePath.ExistCheck()
79+
require.NoError(t, err)
80+
81+
assert.False(t, exist, "Library clone folder was deleted.")
82+
83+
err = backup.Restore()
84+
require.NoError(t, err)
85+
exist, err = gitClonePath.ExistCheck()
86+
require.NoError(t, err)
87+
88+
assert.True(t, exist, "Library clone folder was backed up.")
89+
}

0 commit comments

Comments
 (0)