github.com/kngu9/glide@v0.0.0-20160505135211-e73500c73591/gom/gom.go (about)

     1  package gom
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/Masterminds/glide/cfg"
     9  	"github.com/Masterminds/glide/msg"
    10  	gpath "github.com/Masterminds/glide/path"
    11  	"github.com/Masterminds/glide/util"
    12  )
    13  
    14  // Has returns true if this dir has a Gomfile.
    15  func Has(dir string) bool {
    16  	path := filepath.Join(dir, "Gomfile")
    17  	fi, err := os.Stat(path)
    18  	return err == nil && !fi.IsDir()
    19  }
    20  
    21  // Parse parses a Gomfile.
    22  func Parse(dir string) ([]*cfg.Dependency, error) {
    23  	path := filepath.Join(dir, "Gomfile")
    24  	if fi, err := os.Stat(path); err != nil || fi.IsDir() {
    25  		return []*cfg.Dependency{}, nil
    26  	}
    27  
    28  	msg.Info("Found Gomfile in %s", gpath.StripBasepath(dir))
    29  	buf := []*cfg.Dependency{}
    30  
    31  	goms, err := parseGomfile(path)
    32  	if err != nil {
    33  		return []*cfg.Dependency{}, err
    34  	}
    35  
    36  	for _, gom := range goms {
    37  		// Do we need to skip this dependency?
    38  		if val, ok := gom.options["skipdep"]; ok && val.(string) == "true" {
    39  			continue
    40  		}
    41  
    42  		// Check for custom cloning command
    43  		if _, ok := gom.options["command"]; ok {
    44  			return []*cfg.Dependency{}, errors.New("Glide does not support custom Gomfile commands")
    45  		}
    46  
    47  		// Check for groups/environments
    48  		if val, ok := gom.options["group"]; ok {
    49  			groups := toStringSlice(val)
    50  			if !stringsContain(groups, "development") && !stringsContain(groups, "production") {
    51  				// right now we only support development and production
    52  				msg.Info("Skipping dependency '%s' because it isn't in the development or production group", gom.name)
    53  				continue
    54  			}
    55  		}
    56  
    57  		pkg, sub := util.NormalizeName(gom.name)
    58  
    59  		dep := &cfg.Dependency{
    60  			Name: pkg,
    61  		}
    62  
    63  		if len(sub) > 0 {
    64  			dep.Subpackages = []string{sub}
    65  		}
    66  
    67  		// Check for a specific revision
    68  		if val, ok := gom.options["commit"]; ok {
    69  			dep.Reference = val.(string)
    70  		}
    71  		if val, ok := gom.options["tag"]; ok {
    72  			dep.Reference = val.(string)
    73  		}
    74  		if val, ok := gom.options["branch"]; ok {
    75  			dep.Reference = val.(string)
    76  		}
    77  
    78  		// Parse goos and goarch
    79  		if val, ok := gom.options["goos"]; ok {
    80  			dep.Os = toStringSlice(val)
    81  		}
    82  		if val, ok := gom.options["goarch"]; ok {
    83  			dep.Arch = toStringSlice(val)
    84  		}
    85  
    86  		buf = append(buf, dep)
    87  	}
    88  
    89  	return buf, nil
    90  }
    91  
    92  func stringsContain(v []string, key string) bool {
    93  	for _, s := range v {
    94  		if s == key {
    95  			return true
    96  		}
    97  	}
    98  	return false
    99  }
   100  
   101  func toStringSlice(v interface{}) []string {
   102  	if v, ok := v.(string); ok {
   103  		return []string{v}
   104  	}
   105  
   106  	if v, ok := v.([]string); ok {
   107  		return v
   108  	}
   109  
   110  	return []string{}
   111  }