Skip to content

Commit

Permalink
add printUsageAndExit function
Browse files Browse the repository at this point in the history
  • Loading branch information
n-oden committed Nov 21, 2024
1 parent 70b0faf commit add3bec
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ func printUsage() {
flag.PrintDefaults()
}

func printUsageAndExit(err error) {
printUsage()
if err != nil {
log.Fatalf("%s", err)
}
os.Exit(0)

Check warning on line 214 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L209-L214

Added lines #L209 - L214 were not covered by tests
}

func getBuildInfo() *debug.BuildInfo {
bi, ok := debug.ReadBuildInfo()
if !ok {
Expand Down Expand Up @@ -295,9 +303,7 @@ func main() {
if len(originPaths) == 0 || (len(originPaths) == 1 && originPaths[0] == "-") {
originPaths[0] = reviser.StandardInput
if err := validateRequiredParam(originPaths[0]); err != nil {
log.Printf("%s\n\n", err)
printUsage()
os.Exit(1)
printUsageAndExit(err)
}

Check warning on line 307 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L303-L307

Added lines #L303 - L307 were not covered by tests
}

Expand Down Expand Up @@ -336,9 +342,7 @@ func main() {
if importsOrder != "" {
order, err := reviser.StringToImportsOrders(importsOrder)
if err != nil {
fmt.Printf("%s\n\n", err)
printUsage()
os.Exit(1)
printUsageAndExit(err)

Check warning on line 345 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L345

Added line #L345 was not covered by tests
}
options = append(options, reviser.WithImportsOrder(order))
}
Expand All @@ -350,9 +354,7 @@ func main() {
log.Printf("Processing %s\n", originPath)
originProjectName, err := helper.DetermineProjectName(projectName, originPath, helper.OSGetwdOption)
if err != nil {
log.Printf("Could not determine project name for path %s: %s\n\n", originPath, err)
printUsage()
os.Exit(1)
printUsageAndExit(fmt.Errorf("Could not determine project name for path %s: %s", originPath, err))
}
if _, ok := reviser.IsDir(originPath); ok {
if *listFileName {
Expand Down

0 comments on commit add3bec

Please sign in to comment.