Skip to content

Commit

Permalink
optimize: simplify hz tool write file
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean committed Jan 22, 2025
1 parent e504a55 commit 09c914e
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions cmd/hz/generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,13 @@ func (tg *TemplateGenerator) Persist() error {
}

err = func() error {
fileMode := 0o644
fileMode := os.FileMode(0o644)
if strings.HasSuffix(abPath, ".sh") {
fileMode = 0o755
fileMode = os.FileMode(0o755)
}
file, err := os.OpenFile(abPath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, os.FileMode(fileMode))
defer file.Close()
if err != nil {
return fmt.Errorf("open file '%s' failed, err: %v", abPath, err.Error())
if err := os.WriteFile(abPath, []byte(data.Content), fileMode); err != nil {
return fmt.Errorf("write file '%s' failed, err: %v", abPath, err)
}
if _, err = file.WriteString(data.Content); err != nil {
return fmt.Errorf("write file '%s' failed, err: %v", abPath, err.Error())
}

return nil
}()
if err != nil {
Expand Down

0 comments on commit 09c914e

Please sign in to comment.