Skip to content

Commit

Permalink
refactor: os.Exit instead of panic
Browse files Browse the repository at this point in the history
  • Loading branch information
Bin-Huang committed Aug 9, 2022
1 parent 5e7f7fa commit 2e7460d
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
)

func main() {
err := makeConstructor()
if err != nil {
fmt.Printf("make-constructor: [ERROR] %v\n", err)
os.Exit(1)
}
}

func makeConstructor() error {
pkg, err := GetPackageInfo(".")
if err != nil {
panic(err)
return err
}
// skip if generated recently
genFilename := "./constructor_gen.go"
if isGeneratedRecently(genFilename) {
return
return nil
}
allImports := []ImportInfo{}
allResults := []StructInfo{}
Expand All @@ -38,17 +47,22 @@ func main() {
allResults = append(allResults, results...)
}
if len(allResults) == 0 {
return
return nil
}
code, err := GenerateCode(pkg.Name, allImports, allResults)
if err != nil {
panic(err)
return err
}
err = ioutil.WriteFile(genFilename, []byte(code), 0644)
if err != nil {
panic(err)
return err
}
genFilepath, err := filepath.Abs(genFilename)
if err != nil {
return err
}
fmt.Printf("make-constructor: %v: wrote %v\n", pkg.PkgPath, genFilename)
fmt.Printf("make-constructor: [INFO] wrote %v\n", genFilepath)
return nil
}

func isGeneratedRecently(genFilename string) bool {
Expand Down

0 comments on commit 2e7460d

Please sign in to comment.