Skip to content

Commit e3abdbb

Browse files
committed
Added EditRepository function
1 parent c12f73b commit e3abdbb

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

repos.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,50 @@ func RemoveRepository(repo *Repository, configFolderPath string) error {
242242
return nil
243243
}
244244

245+
// EditRepository replace the an old repo configuration with a new repo
246+
// configuration found in the specified APT config folder (usually /etc/apt).
247+
func EditRepository(old *Repository, new *Repository, configFolderPath string) error {
248+
// Read all repos configurations
249+
repos, err := ParseAPTConfigFolder(configFolderPath)
250+
if err != nil {
251+
return fmt.Errorf("parsing APT config: %s", err)
252+
}
253+
254+
// Find the repo to edit
255+
repoToEdit := repos.Find(old)
256+
if repoToEdit == nil {
257+
return fmt.Errorf("Repository doesn't exist")
258+
}
259+
260+
// Read the config file that contains the repo configuration to edit
261+
fileToEdit := repoToEdit.configFile
262+
data, err := ioutil.ReadFile(fileToEdit)
263+
if err != nil {
264+
return fmt.Errorf("Reading config file %s: %s", fileToEdit, err)
265+
}
266+
267+
// Create the new version of the file
268+
scanner := bufio.NewScanner(bytes.NewReader(data))
269+
newContent := ""
270+
for scanner.Scan() {
271+
line := scanner.Text()
272+
r := parseAPTConfigLine(line)
273+
if r.Equals(old) {
274+
// Write the new config to replace the old one
275+
newContent += new.APTConfigLine() + "\n"
276+
continue
277+
}
278+
newContent += line + "\n"
279+
}
280+
281+
err = replaceFile(fileToEdit, []byte(newContent))
282+
if err != nil {
283+
return fmt.Errorf("Writing of new config: %s", err)
284+
}
285+
286+
return nil
287+
}
288+
245289
func replaceFile(path string, newContent []byte) error {
246290
newPath := path + ".new"
247291
backupPath := path + ".save"

repos_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,28 @@ func TestAddAndRemoveRepository(t *testing.T) {
9595

9696
err = RemoveRepository(repo2, "testdata/apt2")
9797
require.Error(t, err, "Removing repository again")
98+
99+
// no changes should have happened
100+
repos, err = ParseAPTConfigFolder("testdata/apt2")
101+
require.NoError(t, err, "running List command")
102+
require.True(t, repos.Contains(repo1), "Configuration contains: %#v", repo1)
103+
require.False(t, repos.Contains(repo2), "Configuration contains: %#v", repo2)
104+
105+
err = EditRepository(repo1, repo2, "testdata/apt2")
106+
require.NoError(t, err, "editing repository %#V -> %#V", repo1, repo2)
107+
108+
// repo2 should be changed to repo1
109+
repos, err = ParseAPTConfigFolder("testdata/apt2")
110+
require.NoError(t, err, "running List command")
111+
require.False(t, repos.Contains(repo1), "Configuration contains: %#v", repo1)
112+
require.True(t, repos.Contains(repo2), "Configuration contains: %#v", repo2)
113+
114+
err = EditRepository(repo1, repo2, "testdata/apt2")
115+
require.Error(t, err, "editing again repository %#v -> %#v", repo1, repo2)
116+
117+
// no changes should have happened
118+
repos, err = ParseAPTConfigFolder("testdata/apt2")
119+
require.NoError(t, err, "running List command")
120+
require.False(t, repos.Contains(repo1), "Configuration contains: %#v", repo1)
121+
require.True(t, repos.Contains(repo2), "Configuration contains: %#v", repo2)
98122
}

0 commit comments

Comments
 (0)