github.com/hori-ryota/glide@v0.0.0-20160621143827-dc7ca2fac035/gpm/gpm.go (about)

     1  // Package gpm reads GPM's Godeps files.
     2  //
     3  // It is not a complete implementaton of GPM.
     4  package gpm
     5  
     6  import (
     7  	"bufio"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/Masterminds/glide/cfg"
    13  	"github.com/Masterminds/glide/msg"
    14  	gpath "github.com/Masterminds/glide/path"
    15  )
    16  
    17  // Has indicates whether a Godeps file exists.
    18  func Has(dir string) bool {
    19  	path := filepath.Join(dir, "Godeps")
    20  	_, err := os.Stat(path)
    21  	return err == nil
    22  }
    23  
    24  // Parse parses a GPM-flavored Godeps file.
    25  func Parse(dir string) ([]*cfg.Dependency, error) {
    26  	path := filepath.Join(dir, "Godeps")
    27  	if i, err := os.Stat(path); err != nil {
    28  		return []*cfg.Dependency{}, nil
    29  	} else if i.IsDir() {
    30  		msg.Info("Godeps is a directory. This is probably a Godep project.\n")
    31  		return []*cfg.Dependency{}, nil
    32  	}
    33  	msg.Info("Found Godeps file in %s", gpath.StripBasepath(dir))
    34  	msg.Info("--> Parsing GPM metadata...")
    35  
    36  	buf := []*cfg.Dependency{}
    37  
    38  	file, err := os.Open(path)
    39  	if err != nil {
    40  		return buf, err
    41  	}
    42  	scanner := bufio.NewScanner(file)
    43  	for scanner.Scan() {
    44  		parts, ok := parseGodepsLine(scanner.Text())
    45  		if ok {
    46  			dep := &cfg.Dependency{Name: parts[0]}
    47  			if len(parts) > 1 {
    48  				dep.Reference = parts[1]
    49  			}
    50  			buf = append(buf, dep)
    51  		}
    52  	}
    53  	if err := scanner.Err(); err != nil {
    54  		msg.Warn("Scan failed: %s\n", err)
    55  		return buf, err
    56  	}
    57  
    58  	return buf, nil
    59  }
    60  
    61  func parseGodepsLine(line string) ([]string, bool) {
    62  	line = strings.TrimSpace(line)
    63  
    64  	if len(line) == 0 || strings.HasPrefix(line, "#") {
    65  		return []string{}, false
    66  	}
    67  
    68  	return strings.Fields(line), true
    69  }