github.com/roscopecoltran/glide@v0.12.3/importer/importer.go (about)

     1  // Package importer imports dependency configuration from Glide, Godep, GPM, GB and gom
     2  package importer
     3  
     4  import (
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/Masterminds/glide/cfg"
    10  	"github.com/Masterminds/glide/gb"
    11  	"github.com/Masterminds/glide/godep"
    12  	"github.com/Masterminds/glide/gom"
    13  	"github.com/Masterminds/glide/gpm"
    14  )
    15  
    16  var i = &DefaultImporter{}
    17  
    18  // Import uses the DefaultImporter to import from Glide, Godep, GPM, GB and gom.
    19  func Import(path string) (bool, []*cfg.Dependency, error) {
    20  	return i.Import(path)
    21  }
    22  
    23  // Importer enables importing depenency configuration.
    24  type Importer interface {
    25  
    26  	// Import imports dependency configuration. It returns:
    27  	// - A bool if any configuration was found.
    28  	// - []*cfg.Dependency containing dependency configuration if any is found.
    29  	// - An error if one was reported.
    30  	Import(path string) (bool, []*cfg.Dependency, error)
    31  }
    32  
    33  // DefaultImporter imports from Glide, Godep, GPM, GB and gom.
    34  type DefaultImporter struct{}
    35  
    36  // Import tries to import configuration from Glide, Godep, GPM, GB and gom.
    37  func (d *DefaultImporter) Import(path string) (bool, []*cfg.Dependency, error) {
    38  
    39  	// Try importing from Glide first.
    40  	p := filepath.Join(path, "glide.yaml")
    41  	if _, err := os.Stat(p); err == nil {
    42  		// We found glide configuration.
    43  		yml, err := ioutil.ReadFile(p)
    44  		if err != nil {
    45  			return false, []*cfg.Dependency{}, err
    46  		}
    47  		conf, err := cfg.ConfigFromYaml(yml)
    48  		if err != nil {
    49  			return false, []*cfg.Dependency{}, err
    50  		}
    51  		return true, conf.Imports, nil
    52  	}
    53  
    54  	// Try importing from Godep
    55  	if godep.Has(path) {
    56  		deps, err := godep.Parse(path)
    57  		if err != nil {
    58  			return false, []*cfg.Dependency{}, err
    59  		}
    60  		return true, deps, nil
    61  	}
    62  
    63  	// Try importing from GPM
    64  	if gpm.Has(path) {
    65  		deps, err := gpm.Parse(path)
    66  		if err != nil {
    67  			return false, []*cfg.Dependency{}, err
    68  		}
    69  		return true, deps, nil
    70  	}
    71  
    72  	// Try importin from GB
    73  	if gb.Has(path) {
    74  		deps, err := gb.Parse(path)
    75  		if err != nil {
    76  			return false, []*cfg.Dependency{}, err
    77  		}
    78  		return true, deps, nil
    79  	}
    80  
    81  	// Try importing from gom
    82  	if gom.Has(path) {
    83  		deps, err := gom.Parse(path)
    84  		if err != nil {
    85  			return false, []*cfg.Dependency{}, err
    86  		}
    87  		return true, deps, nil
    88  	}
    89  
    90  	// When none are found.
    91  	return false, []*cfg.Dependency{}, nil
    92  }