gopkg.in/constabulary/gb.v0@v0.4.4/cmd/gb-vendor/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/constabulary/gb"
    10  	"github.com/constabulary/gb/cmd"
    11  	"github.com/constabulary/gb/internal/debug"
    12  )
    13  
    14  var (
    15  	fs          = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
    16  	projectroot = os.Getenv("GB_PROJECT_DIR")
    17  )
    18  
    19  func init() {
    20  	fs.Usage = usage
    21  }
    22  
    23  var commands = []*cmd.Command{
    24  	cmdFetch,
    25  	cmdUpdate,
    26  	cmdList,
    27  	cmdDelete,
    28  	cmdPurge,
    29  	cmdRestore,
    30  }
    31  
    32  func main() {
    33  	fatalf := func(format string, args ...interface{}) {
    34  		fmt.Fprintf(os.Stderr, "FATAL: "+format+"\n", args...)
    35  		os.Exit(1)
    36  	}
    37  
    38  	args := os.Args[1:]
    39  
    40  	switch {
    41  	case len(args) < 1, args[0] == "-h", args[0] == "-help":
    42  		printUsage(os.Stdout)
    43  		os.Exit(0)
    44  	case args[0] == "help":
    45  		help(args[1:])
    46  		return
    47  	case projectroot == "":
    48  		fatalf("don't run this binary directly, it is meant to be run as 'gb vendor ...'")
    49  	default:
    50  	}
    51  
    52  	root, err := cmd.FindProjectroot(projectroot)
    53  	if err != nil {
    54  		fatalf("could not locate project root: %v", err)
    55  	}
    56  	project := gb.NewProject(root)
    57  
    58  	debug.Debugf("project root %q", project.Projectdir())
    59  
    60  	for _, command := range commands {
    61  		if command.Name == args[0] && command.Runnable() {
    62  
    63  			// add extra flags if necessary
    64  			if command.AddFlags != nil {
    65  				command.AddFlags(fs)
    66  			}
    67  
    68  			if command.FlagParse != nil {
    69  				err = command.FlagParse(fs, args)
    70  			} else {
    71  				err = fs.Parse(args[1:])
    72  			}
    73  			if err != nil {
    74  				fatalf("could not parse flags: %v", err)
    75  			}
    76  			args = fs.Args() // reset args to the leftovers from fs.Parse
    77  			debug.Debugf("args: %v", args)
    78  
    79  			ctx, err := gb.NewContext(project, gb.GcToolchain())
    80  			if err != nil {
    81  				fatalf("unable to construct context: %v", err)
    82  			}
    83  			defer ctx.Destroy()
    84  
    85  			if err := command.Run(ctx, args); err != nil {
    86  				fatalf("command %q failed: %v", command.Name, err)
    87  			}
    88  			return
    89  		}
    90  	}
    91  	fatalf("unknown command %q ", args[0])
    92  }
    93  
    94  const manifestfile = "manifest"
    95  
    96  func manifestFile(ctx *gb.Context) string {
    97  	return filepath.Join(ctx.Projectdir(), "vendor", manifestfile)
    98  }