Skip to content

Commit

Permalink
refactor(main.go): rename startLine and endLine variables to start an…
Browse files Browse the repository at this point in the history
…d end for brevity

feat(main.go): use original file permissions when writing to file for consistency
  • Loading branch information
yyoshiki41 committed May 3, 2024
1 parent c652157 commit d484a9b
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func main() {
log.Fatalf("Failed to parse file: %v", err)
}

var startLine, endLine int
var start, end int
ast.Inspect(node, func(n ast.Node) bool {
fn, ok := n.(*ast.FuncDecl)
if ok && fn.Name.Name == functionName {
startLine = fset.Position(fn.Pos()).Line
endLine = fset.Position(fn.End()).Line
start = fset.Position(fn.Pos()).Line
end = fset.Position(fn.End()).Line
return false
}
return true
Expand All @@ -54,7 +54,7 @@ func main() {
buf := new(bytes.Buffer)
scanner := bufio.NewScanner(file)
for l := 1; scanner.Scan(); {
if l < startLine || l > endLine {
if l < start || l > end {
if _, err := buf.Write(append(scanner.Bytes(), '\n')); err != nil {
log.Fatalf("Failed to write to buffer: %v", err)
}
Expand All @@ -71,7 +71,11 @@ func main() {
log.Fatalf("Failed to format source: %v", err)
}
// write to file
if err := os.WriteFile(fileName, b, 0644); err != nil {
info, err := file.Stat()
if err != nil {
log.Fatalf("Failed to get file info: %v", err)
}
if err := os.WriteFile(fileName, b, info.Mode()); err != nil {
log.Fatalf("Failed to write to file: %v", err)
}
}

0 comments on commit d484a9b

Please sign in to comment.