func main() { args := os.Args[1:] switch { case len(args) < 1, args[0] == "-h", args[0] == "-help": fs.Usage() os.Exit(1) case args[0] == "help": help(args[1:]) return case projectroot == "": gb.Fatalf("don't run this binary directly, it is meant to be run as 'gb vendor ...'") default: } root, err := cmd.FindProjectroot(projectroot) if err != nil { gb.Fatalf("could not locate project root: %v", err) } project := gb.NewProject(root, gb.SourceDir(filepath.Join(root, "src")), gb.SourceDir(filepath.Join(root, "vendor", "src"))) gb.Debugf("project root %q", project.Projectdir()) for _, command := range commands { if command.Name == args[0] && command.Runnable() { // add extra flags if necessary if command.AddFlags != nil { command.AddFlags(fs) } if command.FlagParse != nil { err = command.FlagParse(fs, args) } else { err = fs.Parse(args[1:]) } if err != nil { gb.Fatalf("could not parse flags: %v", err) } args = fs.Args() // reset args to the leftovers from fs.Parse gb.Debugf("args: %v", args) ctx, err := project.NewContext( gb.GcToolchain(), ) if err != nil { gb.Fatalf("unable to construct context: %v", err) } if err := command.Run(ctx, args); err != nil { gb.Fatalf("command %q failed: %v", command.Name, err) } return } } gb.Fatalf("unknown command %q ", args[0]) }
func testContext(t *testing.T, opts ...func(*gb.Context) error) *gb.Context { prj := testProject(t) opts = append([]func(*gb.Context) error{gb.GcToolchain()}, opts...) ctx, err := prj.NewContext(opts...) if err != nil { t.Fatal(err) } ctx.Force = true ctx.SkipInstall = true return ctx }
// RunCommand detects the project root, parses flags and runs the Command. func RunCommand(fs *flag.FlagSet, cmd *Command, projectroot, goroot string, args []string) error { if cmd.AddFlags != nil { cmd.AddFlags(fs) } if err := fs.Parse(args); err != nil { fs.Usage() os.Exit(1) } args = fs.Args() // reset to the remaining arguments ctx, err := NewContext(projectroot, gb.GcToolchain()) if err != nil { return fmt.Errorf("unable to construct context: %v", err) } gb.Debugf("args: %v", args) return cmd.Run(ctx, args) }
func main() { args := os.Args if len(args) < 2 || args[1] == "-h" { fs.Usage() os.Exit(1) } name := args[1] if name == "help" { help(args[2:]) return } command, ok := commands[name] if (command != nil && !command.Runnable()) || !ok { if _, err := lookupPlugin(name); err != nil { gb.Errorf("unknown command %q", name) fs.Usage() os.Exit(1) } command = commands["plugin"] } // 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 { gb.Fatalf("could not parse flags: %v", err) } args = fs.Args() // reset args to the leftovers from fs.Parse 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 { gb.Fatalf("could not make project root absolute: %v", err) } ctx, err := cmd.NewContext( cwd, // project root gb.GcToolchain(), gb.Gcflags(gcflags), gb.Ldflags(ldflags), ) if err != nil { gb.Fatalf("unable to construct context: %v", err) } if command.ParseArgs != nil { args = command.ParseArgs(ctx, ctx.Projectdir(), args) } else { args = cmd.ImportPaths(ctx, ctx.Projectdir(), args) } gb.Debugf("args: %v", args) if err := command.Run(ctx, args); err != nil { gb.Fatalf("command %q failed: %v", name, err) } }