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