Skip to content

Commit 4cce576

Browse files
committed
chore: optimize code style
1 parent e30ab58 commit 4cce576

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

cmd/init.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,20 @@ func fixGo(data []byte, file string, srcMod, dstMod string, isRoot bool) []byte
210210
dstName := path.Base(dstMod)
211211
if isRoot {
212212
if name := f.Name.Name; name == srcName || name == srcName+"_test" {
213-
dname := dstName + strings.TrimPrefix(name, srcName)
214-
if !token.IsIdentifier(dname) {
215-
log.Fatalf("%s: cannot rename package %s to package %s: invalid package name", file, name, dname)
213+
target := dstName + strings.TrimPrefix(name, srcName)
214+
if !token.IsIdentifier(target) {
215+
log.Fatalf("%s: cannot rename package %s to package %s: invalid package name", file, name, target)
216216
}
217-
buf.Replace(at(f.Name.Pos()), at(f.Name.End()), dname)
217+
buf.Replace(at(f.Name.Pos()), at(f.Name.End()), target)
218218
}
219219
}
220220

221221
for _, spec := range f.Imports {
222-
path, err := strconv.Unquote(spec.Path.Value)
222+
pathStr, err := strconv.Unquote(spec.Path.Value)
223223
if err != nil {
224224
continue
225225
}
226-
if path == srcMod {
226+
if pathStr == srcMod {
227227
if srcName != dstName && spec.Name == nil {
228228
// Add package rename because source code uses original name.
229229
// The renaming looks strange, but template authors are unlikely to
@@ -238,9 +238,9 @@ func fixGo(data []byte, file string, srcMod, dstMod string, isRoot bool) []byte
238238
// Change import path to dstMod
239239
buf.Replace(at(spec.Path.Pos()), at(spec.Path.End()), strconv.Quote(dstMod))
240240
}
241-
if strings.HasPrefix(path, srcMod+"/") {
241+
if strings.HasPrefix(pathStr, srcMod+"/") {
242242
// Change import path to begin with dstMod
243-
buf.Replace(at(spec.Path.Pos()), at(spec.Path.End()), strconv.Quote(strings.Replace(path, srcMod, dstMod, 1)))
243+
buf.Replace(at(spec.Path.Pos()), at(spec.Path.End()), strconv.Quote(strings.Replace(pathStr, srcMod, dstMod, 1)))
244244
}
245245
}
246246
return buf.Bytes()
@@ -339,7 +339,13 @@ func generateFile(data map[string]string, fileName, content, projectDir string)
339339
if err != nil {
340340
return fmt.Errorf("error creating file %s: %v", fileName, err)
341341
}
342-
defer file.Close()
342+
343+
defer func(file *os.File) {
344+
err = file.Close()
345+
if err != nil {
346+
log.Fatal(err)
347+
}
348+
}(file)
343349

344350
// Execute the template and write to file
345351
if err := tmpl.Execute(file, data); err != nil {

0 commit comments

Comments
 (0)