-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add repository management to PackageManager
Expand the PackageManager interface to include repository management: adding repository content, removing repository content, enabling existing repositories, and disabling existing repositories.
- Loading branch information
Showing
7 changed files
with
256 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// canonicalizeRepoName converts the string filename into a suitable filename. | ||
// It replaces all spaces with underscores and appends the given suffix. | ||
func canonicalizeRepoName(filename, suffix string) string { | ||
return strings.TrimSuffix(strings.ReplaceAll(filename, " ", "_"), suffix) + suffix | ||
} | ||
|
||
// readLines reads each line in file into a slice. | ||
func readLines(name string) ([]string, error) { | ||
file, err := os.Open(name) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot open file for reading: %w", err) | ||
} | ||
defer file.Close() | ||
|
||
var lines []string | ||
scanner := bufio.NewScanner(file) | ||
scanner.Split(bufio.ScanLines) | ||
for scanner.Scan() { | ||
lines = append(lines, scanner.Text()) | ||
} | ||
if err := scanner.Err(); err != nil { | ||
return nil, fmt.Errorf("cannot read file: %w", err) | ||
} | ||
|
||
return lines, nil | ||
} | ||
|
||
// writeLines writes lines to file name. If truncate is true, the file is | ||
// truncated before writing. | ||
func writeLines(name string, lines []string, truncate bool) error { | ||
var data []byte | ||
|
||
for _, line := range lines { | ||
data = append(data, line+"\n"...) | ||
} | ||
|
||
if truncate { | ||
return ioutil.WriteFile(name, data, 0644) | ||
} else { | ||
file, err := os.OpenFile(name, os.O_WRONLY|os.O_APPEND, 0644) | ||
if err != nil { | ||
return fmt.Errorf("cannot open file for appending: %w", err) | ||
} | ||
defer file.Close() | ||
|
||
if _, err := file.Write(data); err != nil { | ||
return fmt.Errorf("cannot write to file: %w", err) | ||
} | ||
} | ||
return nil | ||
} |