Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Commit

Permalink
cmd/gb: break some of the main.main logic into helpers (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
davecheney committed Jun 21, 2016
1 parent 2515c3a commit 0c7ad54
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 50 deletions.
2 changes: 0 additions & 2 deletions cmd/gb/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ For more about where packages and binaries are installed, run 'gb help project'.

// Resolver resolves packages.
type Resolver interface {
Projectdir() string
// ResolvePackage resolves the import path to a *Package
ResolvePackage(path string) (*gb.Package, error)
}

Expand Down
104 changes: 56 additions & 48 deletions cmd/gb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,58 @@ func main() {
exit(0)
}

command := lookupCommand(name)

// add extra flags if necessary
if command.AddFlags != nil {
command.AddFlags(fs)
}

var err error
if command.FlagParse != nil {
err = command.FlagParse(fs, args)
} else {
err = fs.Parse(args[2:])
}
if err != nil {
fatalf("could not parse flags: %v", err)
}

args = fs.Args() // reset args to the leftovers from fs.Parse

debug.Debugf("args: %v", args)

if command == commands["plugin"] {
args = append([]string{name}, args...)
}
cwd, err := filepath.Abs(cwd) // if cwd was passed in via -R, make sure it is absolute
if err != nil {
fatalf("could not make project root absolute: %v", err)
}

ctx, err := newContext(cwd)
if err != nil {
fatalf("unable to construct context: %v", err)
}

if !command.SkipParseArgs {
srcdir := filepath.Join(ctx.Projectdir(), "src")
args = match.ImportPaths(srcdir, cwd, args)
}

debug.Debugf("args: %v", args)

if destroyContext {
atExit = append(atExit, ctx.Destroy)
}

if err := command.Run(ctx, args); err != nil {
fatalf("command %q failed: %v", name, err)
}
exit(0)
}

func lookupCommand(name string) *cmd.Command {
command, ok := commands[name]
if (command != nil && !command.Runnable()) || !ok {
plugin, err := lookupPlugin(name)
Expand Down Expand Up @@ -95,35 +147,11 @@ func main() {
SkipParseArgs: true,
}
}
return command
}

// add extra flags if necessary
if command.AddFlags != nil {
command.AddFlags(fs)
}

var err error
if command.FlagParse != nil {
err = command.FlagParse(fs, args)
} else {
err = fs.Parse(args[2:])
}
if err != nil {
fatalf("could not parse flags: %v", err)
}

args = fs.Args() // reset args to the leftovers from fs.Parse

debug.Debugf("args: %v", args)

if command == commands["plugin"] {
args = append([]string{name}, args...)
}
cwd, err := filepath.Abs(cwd) // if cwd was passed in via -R, make sure it is absolute
if err != nil {
fatalf("could not make project root absolute: %v", err)
}

ctx, err := cmd.NewContext(
func newContext(cwd string) (*gb.Context, error) {
return cmd.NewContext(
cwd, // project root
gb.GcToolchain(),
gb.Gcflags(gcflags...),
Expand Down Expand Up @@ -154,24 +182,4 @@ func main() {
return gb.WithRace(c)
},
)

if err != nil {
fatalf("unable to construct context: %v", err)
}

if !command.SkipParseArgs {
srcdir := filepath.Join(ctx.Projectdir(), "src")
args = match.ImportPaths(srcdir, cwd, args)
}

debug.Debugf("args: %v", args)

if destroyContext {
atExit = append(atExit, ctx.Destroy)
}

if err := command.Run(ctx, args); err != nil {
fatalf("command %q failed: %v", name, err)
}
exit(0)
}

0 comments on commit 0c7ad54

Please sign in to comment.