1
0
mirror of https://github.com/golang/go synced 2024-11-21 21:44:40 -07:00

cmd/go: remove redundant writeFile function

Updates #63136
This commit is contained in:
goto1134 2024-08-24 10:13:41 +02:00
parent e9044a1e65
commit 2b864bee00
No known key found for this signature in database

View File

@ -134,7 +134,7 @@ func downloadModule(modulePath string, url string, dependenciesToStrip []string)
return err return err
} }
if err := writeFile(filepath.Join(modulePath, "go.sum"), sumText); err != nil { if err := os.WriteFile(filepath.Join(modulePath, "go.sum"), sumText, 0644); err != nil {
return err return err
} }
@ -145,19 +145,19 @@ func downloadModule(modulePath string, url string, dependenciesToStrip []string)
strippedModText := stripDependencies(modText, dependenciesToStrip) strippedModText := stripDependencies(modText, dependenciesToStrip)
return writeFile(filepath.Join(modulePath, "go.mod"), strippedModText) return os.WriteFile(filepath.Join(modulePath, "go.mod"), strippedModText, 0644)
} }
func stripDependencies(goModText string, dependenciesToStrip []string) string { func stripDependencies(goModText []byte, dependenciesToStrip []string) []byte {
var filteredLines []string var filteredLines []string
lines := strings.Split(goModText, "\n") lines := strings.Split(string(goModText), "\n")
for _, line := range lines { for _, line := range lines {
if !containsAny(line, dependenciesToStrip) { if !containsAny(line, dependenciesToStrip) {
filteredLines = append(filteredLines, line) filteredLines = append(filteredLines, line)
} }
} }
filteredText := strings.Join(filteredLines, "\n") filteredText := strings.Join(filteredLines, "\n")
return filteredText return []byte(filteredText)
} }
func containsAny(text string, options []string) bool { func containsAny(text string, options []string) bool {
@ -169,22 +169,12 @@ func containsAny(text string, options []string) bool {
return false return false
} }
func writeFile(filePath, content string) error { func fetchText(url string) ([]byte, error) {
out, err := os.Create(filePath)
if err != nil {
return err
}
defer out.Close()
_, err = io.WriteString(out, content)
return err
}
func fetchText(url string) (string, error) {
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
return "", err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
return string(body), err return body, err
} }