Skip to content

Commit 50932b7

Browse files
committed
Allow specifying the path when adding a repository
There's many cases where adding repositories inconditionally to `sources.list.d/managed.list` is not ideal: it's very common to add an entry per file, ideally named after the repository to add. Configuration management tools for instance follow that pattern. It's also more consistent with other operations not enforcing having a single file for all entries.
1 parent 0d7233b commit 50932b7

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

repos.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ func ParseAPTConfigFolder(folderPath string) (RepositoryList, error) {
172172
}
173173

174174
// AddRepository adds the specified repository by changing the specified APT
175-
// config folder (usually /etc/apt). The new repository is saved into
176-
// a file named "managed.list"
177-
func AddRepository(repo *Repository, configFolderPath string) error {
175+
// config folder (usually /etc/apt).
176+
func AddRepository(repo *Repository, configFolderPath, filename string) error {
178177
repos, err := ParseAPTConfigFolder(configFolderPath)
179178
if err != nil {
180179
return fmt.Errorf("parsing APT config: %s", err)
@@ -184,7 +183,7 @@ func AddRepository(repo *Repository, configFolderPath string) error {
184183
}
185184

186185
// Add to the "managed.list" file
187-
managedPath := filepath.Join(configFolderPath, "sources.list.d", "managed.list")
186+
managedPath := filepath.Join(configFolderPath, "sources.list.d", filename)
188187
f, err := os.OpenFile(managedPath, os.O_APPEND|os.O_WRONLY, 0644)
189188
if os.IsNotExist(err) {
190189
f, err = os.OpenFile(managedPath, os.O_CREATE|os.O_WRONLY, 0644)

repos_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ func TestAddAndRemoveRepository(t *testing.T) {
6767
Components: "main",
6868
Comment: "",
6969
}
70-
err := AddRepository(repo1, "testdata/apt2")
70+
err := AddRepository(repo1, "testdata/apt2", "managed.list")
7171
require.NoError(t, err, "Adding repository")
72-
err = AddRepository(repo2, "testdata/apt2")
72+
err = AddRepository(repo2, "testdata/apt2", "managed.list")
7373
require.NoError(t, err, "Adding repository")
7474

7575
// check that we have repo1 and repo2 added
@@ -78,7 +78,7 @@ func TestAddAndRemoveRepository(t *testing.T) {
7878
require.True(t, repos.Contains(repo1), "Configuration contains: %#v", repo1)
7979
require.True(t, repos.Contains(repo2), "Configuration contains: %#v", repo2)
8080

81-
err = AddRepository(repo2, "testdata/apt2")
81+
err = AddRepository(repo2, "testdata/apt2", "managed.list")
8282
require.Error(t, err, "Adding repository again")
8383

8484
// no changes should have happened

0 commit comments

Comments
 (0)