github.com/s-urbaniak/glide@v0.0.0-20160527141859-5f5e941b1fc4/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  
    35  	buf := []*cfg.Dependency{}
    36  
    37  	file, err := os.Open(path)
    38  	if err != nil {
    39  		return buf, err
    40  	}
    41  	scanner := bufio.NewScanner(file)
    42  	for scanner.Scan() {
    43  		parts, ok := parseGodepsLine(scanner.Text())
    44  		if ok {
    45  			dep := &cfg.Dependency{Name: parts[0]}
    46  			if len(parts) > 1 {
    47  				dep.Reference = parts[1]
    48  			}
    49  			buf = append(buf, dep)
    50  		}
    51  	}
    52  	if err := scanner.Err(); err != nil {
    53  		msg.Warn("Scan failed: %s\n", err)
    54  		return buf, err
    55  	}
    56  
    57  	return buf, nil
    58  }
    59  
    60  func parseGodepsLine(line string) ([]string, bool) {
    61  	line = strings.TrimSpace(line)
    62  
    63  	if len(line) == 0 || strings.HasPrefix(line, "#") {
    64  		return []string{}, false
    65  	}
    66  
    67  	return strings.Fields(line), true
    68  }