Skip to content

Commit

Permalink
Clean up main function
Browse files Browse the repository at this point in the history
Use switch instead of nested if-elses to enhance readability.
  • Loading branch information
sywhang committed Nov 21, 2023
1 parent d619fd7 commit 1d18764
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ go install go.uber.org/mock/mockgen@latest
### Archive mode

Archive mode generates mock interfaces from a package archive
file (.a). It is enabled by using the -archive flag and two
non-flag arguments: an import path, and a comma-separated
list of symbols.
file (.a). It is enabled by using the -archive flag. An import
path and a comma-separated list of symbols should be provided
as a non-flag argument to the command.

Example:

Expand Down
5 changes: 3 additions & 2 deletions mockgen/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func archiveMode(importPath string, symbols []string, archive string) (*model.Pa
}

pkg := &model.Package{
Name: tp.Name(),
PkgPath: tp.Path(),
Name: tp.Name(),
PkgPath: tp.Path(),
Interfaces: make([]*model.Interface, 0, len(symbols)),
}
for _, name := range symbols {
m := tp.Scope().Lookup(name)
Expand Down
49 changes: 30 additions & 19 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,35 @@ func main() {
var pkg *model.Package
var err error
var packageName string
if *source != "" {

// Switch between modes
switch {
case *source != "": // source mode
pkg, err = sourceMode(*source)
} else {
if flag.NArg() != 2 {
usage()
log.Fatal("Expected exactly two arguments")
}
case *archive != "": // archive mode
checkArgs()
packageName = flag.Arg(0)
interfaces := strings.Split(flag.Arg(1), ",")
if *archive != "" {
pkg, err = archiveMode(packageName, interfaces, *archive)
} else {
if packageName == "." {
dir, err := os.Getwd()
if err != nil {
log.Fatalf("Get current directory failed: %v", err)
}
packageName, err = packageNameOfDir(dir)
if err != nil {
log.Fatalf("Parse package name failed: %v", err)
}
pkg, err = archiveMode(packageName, interfaces, *archive)

default: // reflect mode
checkArgs()
packageName = flag.Arg(0)
interfaces := strings.Split(flag.Arg(1), ",")

if packageName == "." {
dir, err := os.Getwd()
if err != nil {
log.Fatalf("Get current directory failed: %v", err)
}
packageName, err = packageNameOfDir(dir)
if err != nil {
log.Fatalf("Parse package name failed: %v", err)
}
pkg, err = reflectMode(packageName, interfaces)
}
pkg, err = reflectMode(packageName, interfaces)
}

if err != nil {
log.Fatalf("Loading input failed: %v", err)
}
Expand Down Expand Up @@ -225,6 +229,13 @@ func parseExcludeInterfaces(names string) map[string]struct{} {
return namesSet
}

func checkArgs() {
if flag.NArg() != 2 {
usage()
log.Fatal("Expected exactly two arguments")
}
}

func usage() {
_, _ = io.WriteString(os.Stderr, usageText)
flag.PrintDefaults()
Expand Down

0 comments on commit 1d18764

Please sign in to comment.