github.com/anthonymayer/glide@v0.0.0-20160224162501-bff8b50d232e/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  )
    15  
    16  // Has indicates whether a Godeps file exists.
    17  func Has(dir string) bool {
    18  	path := filepath.Join(dir, "Godeps")
    19  	_, err := os.Stat(path)
    20  	return err == nil
    21  }
    22  
    23  // Parse parses a GPM-flavored Godeps file.
    24  func Parse(dir string) ([]*cfg.Dependency, error) {
    25  	path := filepath.Join(dir, "Godeps")
    26  	if i, err := os.Stat(path); err != nil {
    27  		return []*cfg.Dependency{}, nil
    28  	} else if i.IsDir() {
    29  		msg.Info("Godeps is a directory. This is probably a Godep project.\n")
    30  		return []*cfg.Dependency{}, nil
    31  	}
    32  	msg.Info("Found Godeps file.\n")
    33  
    34  	buf := []*cfg.Dependency{}
    35  
    36  	file, err := os.Open(path)
    37  	if err != nil {
    38  		return buf, err
    39  	}
    40  	scanner := bufio.NewScanner(file)
    41  	for scanner.Scan() {
    42  		parts, ok := parseGodepsLine(scanner.Text())
    43  		if ok {
    44  			dep := &cfg.Dependency{Name: parts[0]}
    45  			if len(parts) > 1 {
    46  				dep.Reference = parts[1]
    47  			}
    48  			buf = append(buf, dep)
    49  		}
    50  	}
    51  	if err := scanner.Err(); err != nil {
    52  		msg.Warn("Scan failed: %s\n", err)
    53  		return buf, err
    54  	}
    55  
    56  	return buf, nil
    57  }
    58  
    59  func parseGodepsLine(line string) ([]string, bool) {
    60  	line = strings.TrimSpace(line)
    61  
    62  	if len(line) == 0 || strings.HasPrefix(line, "#") {
    63  		return []string{}, false
    64  	}
    65  
    66  	return strings.Fields(line), true
    67  }