Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Fargher <jfargher@gitlab.com>2023-03-14 05:21:46 +0300
committerJames Fargher <jfargher@gitlab.com>2023-03-16 23:34:49 +0300
commit8efccb9a5325525196ae8adad08f1d5eb29dc403 (patch)
treeefbe7322a25dc0c862b7ace5c2bf69a5d35079e7 /tools/module-updater/main.go
parent5c42c9509ff0fab716f7b903b9dc4be77f1c15d9 (diff)
module-updater: Extract individual file import updating
Previously there was a lot of file iteration logic mixed with error handling. Here we extract a helper so that the logic is clearly separated.
Diffstat (limited to 'tools/module-updater/main.go')
-rw-r--r--tools/module-updater/main.go58
1 files changed, 30 insertions, 28 deletions
diff --git a/tools/module-updater/main.go b/tools/module-updater/main.go
index d8cfbda1e..b7c897fc2 100644
--- a/tools/module-updater/main.go
+++ b/tools/module-updater/main.go
@@ -161,48 +161,50 @@ func rewriteImports(moduleAbsRootPath, prev, next string) error {
}
seen[file.Name()] = struct{}{}
- astFile, err := parser.ParseFile(fileSet, file.Name(), nil, parser.ParseComments)
- if err != nil {
+ if err := rewriteFileImports(fileSet, file.Name(), prev, next); err != nil {
rerr = err
return false
}
- for _, imprt := range astFile.Imports {
- oldImport, err := strconv.Unquote(imprt.Path.Value)
- if err != nil {
- rerr = fmt.Errorf("unquote import: %s :%w", imprt.Path.Value, err)
- return false
- }
+ return true
+ })
- if newImport := strings.Replace(oldImport, prev, next, 1); newImport != oldImport {
- imprt.EndPos = imprt.End()
- imprt.Path.Value = strconv.Quote(newImport)
- }
- }
+ return rerr
+}
+
+func rewriteFileImports(fileSet *token.FileSet, filename, prev, next string) (retErr error) {
+ astFile, err := parser.ParseFile(fileSet, filename, nil, parser.ParseComments)
+ if err != nil {
+ return err
+ }
- f, err := os.Create(file.Name())
+ for _, imprt := range astFile.Imports {
+ oldImport, err := strconv.Unquote(imprt.Path.Value)
if err != nil {
- rerr = fmt.Errorf("open file %q: %w", file.Name(), err)
- return false
+ return fmt.Errorf("unquote import: %s :%w", imprt.Path.Value, err)
}
- ferr := format.Node(f, fileSet, astFile)
- cerr := f.Close()
-
- if ferr != nil {
- rerr = fmt.Errorf("rewrite file %q: %w", file.Name(), err)
- return false
+ if newImport := strings.Replace(oldImport, prev, next, 1); newImport != oldImport {
+ imprt.EndPos = imprt.End()
+ imprt.Path.Value = strconv.Quote(newImport)
}
+ }
- if cerr != nil {
- rerr = fmt.Errorf("close file %q: %w", file.Name(), err)
- return false
+ f, err := os.Create(filename)
+ if err != nil {
+ return fmt.Errorf("open file %q: %w", filename, err)
+ }
+ defer func() {
+ if err := f.Close(); retErr == nil && err != nil {
+ retErr = fmt.Errorf("close file %q: %w", filename, err)
}
+ }()
- return true
- })
+ if err := format.Node(f, fileSet, astFile); err != nil {
+ return fmt.Errorf("rewrite file %q: %w", filename, err)
+ }
- return rerr
+ return nil
}
func normaliseDesiredImportReplacement(module, from, to string) (string, string, error) {