github.com/golang/dep@v0.5.4/internal/importers/govend/importer.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package govend
     6  
     7  import (
     8  	"io/ioutil"
     9  	"log"
    10  	"os"
    11  	"path/filepath"
    12  
    13  	"github.com/golang/dep"
    14  	"github.com/golang/dep/gps"
    15  	"github.com/golang/dep/internal/importers/base"
    16  	"github.com/pkg/errors"
    17  	"gopkg.in/yaml.v2"
    18  )
    19  
    20  // ToDo: govend supports json and xml formats as well and we will add support for other formats in next PR - @RaviTezu
    21  // govend don't have a separate lock file.
    22  const govendYAMLName = "vendor.yml"
    23  
    24  // Importer imports govend configuration in to the dep configuration format.
    25  type Importer struct {
    26  	*base.Importer
    27  	yaml govendYAML
    28  }
    29  
    30  // NewImporter for govend.
    31  func NewImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *Importer {
    32  	return &Importer{Importer: base.NewImporter(logger, verbose, sm)}
    33  }
    34  
    35  type govendYAML struct {
    36  	Imports []govendPackage `yaml:"vendors"`
    37  }
    38  
    39  type govendPackage struct {
    40  	Path     string `yaml:"path"`
    41  	Revision string `yaml:"rev"`
    42  }
    43  
    44  // Name of the importer.
    45  func (g *Importer) Name() string {
    46  	return "govend"
    47  }
    48  
    49  // HasDepMetadata checks if a directory contains config that the importer can handle.
    50  func (g *Importer) HasDepMetadata(dir string) bool {
    51  	y := filepath.Join(dir, govendYAMLName)
    52  	if _, err := os.Stat(y); err != nil {
    53  		return false
    54  	}
    55  
    56  	return true
    57  }
    58  
    59  // Import the config found in the directory.
    60  func (g *Importer) Import(dir string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
    61  	err := g.load(dir)
    62  	if err != nil {
    63  		return nil, nil, err
    64  	}
    65  
    66  	m, l := g.convert(pr)
    67  	return m, l, nil
    68  }
    69  
    70  // load the govend configuration files.
    71  func (g *Importer) load(projectDir string) error {
    72  	g.Logger.Println("Detected govend configuration files...")
    73  	y := filepath.Join(projectDir, govendYAMLName)
    74  	if g.Verbose {
    75  		g.Logger.Printf("  Loading %s", y)
    76  	}
    77  	yb, err := ioutil.ReadFile(y)
    78  	if err != nil {
    79  		return errors.Wrapf(err, "unable to read %s", y)
    80  	}
    81  	err = yaml.Unmarshal(yb, &g.yaml)
    82  	if err != nil {
    83  		return errors.Wrapf(err, "unable to parse %s", y)
    84  	}
    85  	return nil
    86  }
    87  
    88  // convert the govend configuration files into dep configuration files.
    89  func (g *Importer) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock) {
    90  	g.Logger.Println("Converting from vendor.yaml...")
    91  
    92  	packages := make([]base.ImportedPackage, 0, len(g.yaml.Imports))
    93  	for _, pkg := range g.yaml.Imports {
    94  		// Path must not be empty
    95  		if pkg.Path == "" {
    96  			g.Logger.Println(
    97  				"  Warning: Skipping project. Invalid govend configuration, path is required",
    98  			)
    99  			continue
   100  		}
   101  
   102  		if pkg.Revision == "" {
   103  			// Do not add 'empty constraints' to the manifest. Solve will add to lock if required.
   104  			g.Logger.Printf(
   105  				"  Warning: Skipping import with empty constraints. "+
   106  					"The solve step will add the dependency to the lock if needed: %q\n",
   107  				pkg.Path,
   108  			)
   109  			continue
   110  		}
   111  
   112  		ip := base.ImportedPackage{
   113  			Name:     pkg.Path,
   114  			LockHint: pkg.Revision,
   115  		}
   116  		packages = append(packages, ip)
   117  	}
   118  
   119  	g.ImportPackages(packages, true)
   120  	return g.Manifest, g.Lock
   121  }