This repository was archived by the owner on Sep 9, 2020. It is now read-only.
  
  
  - 
                Notifications
    
You must be signed in to change notification settings  - Fork 1k
 
gvt (and gb-vendor) importer #1149
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      34b8ee6
              
                import gvt manifest on init
              
              
                michael-go 28f55f7
              
                add gvt importer integration test and update docs
              
              
                michael-go 956f34d
              
                gvt importer imports gb-vendor too
              
              
                michael-go 1cfc27f
              
                import alternate repo url from gb/gvt manifest
              
              
                michael-go cbf282b
              
                gvt/gb importer PR fixes
              
              
                michael-go File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
  
    
      
          
            28 changes: 28 additions & 0 deletions
          
          28 
        
  cmd/dep/testdata/harness_tests/init/gvt/case1/final/Gopkg.lock
  
  
      
      
   
        
      
      
    Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
      
      Oops, something went wrong.
      
    
  
        
          
  
    
      
          
            11 changes: 11 additions & 0 deletions
          
          11 
        
  cmd/dep/testdata/harness_tests/init/gvt/case1/final/Gopkg.toml
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| 
     | 
||
| [[constraint]] | ||
| name = "github.com/sdboyer/deptest" | ||
| source = "https://github.com/carolynvs/deptest" | ||
| 
     | 
||
| [[constraint]] | ||
| name = "github.com/sdboyer/deptestdos" | ||
| 
     | 
||
| [[constraint]] | ||
| branch = "v2" | ||
| name = "gopkg.in/yaml.v2" | 
        
          
  
    
      
          
            20 changes: 20 additions & 0 deletions
          
          20 
        
  cmd/dep/testdata/harness_tests/init/gvt/case1/initial/main.go
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Copyright 2017 The Go Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
| 
     | 
||
| package main | ||
| 
     | 
||
| import ( | ||
| "fmt" | ||
| 
     | 
||
| "github.com/sdboyer/deptest" | ||
| "github.com/sdboyer/deptestdos" | ||
| "gopkg.in/yaml.v2" | ||
| ) | ||
| 
     | 
||
| func main() { | ||
| var a deptestdos.Bar | ||
| var b yaml.MapItem | ||
| var c deptest.Foo | ||
| fmt.Println(a, b, c) | ||
| } | 
        
          
  
    
      
          
            23 changes: 23 additions & 0 deletions
          
          23 
        
  cmd/dep/testdata/harness_tests/init/gvt/case1/initial/vendor/manifest
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "version": "0", | ||
| "dependencies": [ | ||
| { | ||
| "importpath": "github.com/sdboyer/deptest", | ||
| "repository": "https://github.com/carolynvs/deptest", | ||
| "revision": "3f4c3bea144e112a69bbe5d8d01c1b09a544253f", | ||
| "branch": "HEAD" | ||
| }, | ||
| { | ||
| "importpath": "github.com/sdboyer/deptestdos", | ||
| "repository": "https://github.com/sdboyer/deptestdos", | ||
| "revision": "5c607206be5decd28e6263ffffdcee067266015eXXX", | ||
| "branch": "master" | ||
| }, | ||
| { | ||
| "importpath": "gopkg.in/yaml.v2", | ||
| "repository": "https://gopkg.in/yaml.v2", | ||
| "revision": "f7716cbe52baa25d2e9b0d0da546fcf909fc16b4", | ||
| "branch": "v2" | ||
| } | ||
| ] | ||
| } | 
        
          
  
    
      
          
            13 changes: 13 additions & 0 deletions
          
          13 
        
  cmd/dep/testdata/harness_tests/init/gvt/case1/testcase.json
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "commands": [ | ||
| ["init", "-no-examples"] | ||
| ], | ||
| "error-expected": "", | ||
| "gopath-initial": { | ||
| "github.com/sdboyer/deptest": "3f4c3bea144e112a69bbe5d8d01c1b09a544253f" | ||
| }, | ||
| "vendor-final": [ | ||
| "github.com/sdboyer/deptest", | ||
| "github.com/sdboyer/deptestdos" | ||
| ] | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // Copyright 2017 The Go Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
| 
     | 
||
| package gvt | ||
| 
     | 
||
| import ( | ||
| "encoding/json" | ||
| "io/ioutil" | ||
| "log" | ||
| "os" | ||
| "path/filepath" | ||
| 
     | 
||
| "github.com/golang/dep" | ||
| "github.com/golang/dep/internal/gps" | ||
| "github.com/golang/dep/internal/importers/base" | ||
| "github.com/pkg/errors" | ||
| ) | ||
| 
     | 
||
| const gvtPath = "vendor" + string(os.PathSeparator) + "manifest" | ||
| 
     | 
||
| // Importer imports gvt configuration into the dep configuration format. | ||
| type Importer struct { | ||
| *base.Importer | ||
| gvtConfig gvtManifest | ||
| } | ||
| 
     | 
||
| // NewImporter for gvt. It handles gb (gb-vendor) too as they share a common manifest file & format | ||
| func NewImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *Importer { | ||
| return &Importer{Importer: base.NewImporter(logger, verbose, sm)} | ||
| } | ||
| 
     | 
||
| type gvtManifest struct { | ||
| Deps []gvtPkg `json:"dependencies"` | ||
| } | ||
| 
     | 
||
| type gvtPkg struct { | ||
| ImportPath string | ||
| Repository string | ||
| Revision string | ||
| Branch string | ||
| } | ||
| 
     | 
||
| // Name of the importer. | ||
| func (g *Importer) Name() string { | ||
| return "gvt" | ||
| } | ||
| 
     | 
||
| // HasDepMetadata checks if a directory contains config that the importer can handle. | ||
| func (g *Importer) HasDepMetadata(dir string) bool { | ||
| y := filepath.Join(dir, gvtPath) | ||
| if _, err := os.Stat(y); err != nil { | ||
| return false | ||
| } | ||
| 
     | 
||
| return true | ||
| } | ||
| 
     | 
||
| // Import the config found in the directory. | ||
| func (g *Importer) Import(dir string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) { | ||
| err := g.load(dir) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| 
     | 
||
| return g.convert(pr) | ||
| } | ||
| 
     | 
||
| func (g *Importer) load(projectDir string) error { | ||
| g.Logger.Println("Detected gb/gvt configuration files...") | ||
| j := filepath.Join(projectDir, gvtPath) | ||
| if g.Verbose { | ||
| g.Logger.Printf(" Loading %s", j) | ||
| } | ||
| jb, err := ioutil.ReadFile(j) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "unable to read %s", j) | ||
| } | ||
| err = json.Unmarshal(jb, &g.gvtConfig) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "unable to parse %s", j) | ||
| } | ||
| 
     | 
||
| return nil | ||
| } | ||
| 
     | 
||
| func (g *Importer) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) { | ||
| g.Logger.Println("Converting from vendor/manifest ...") | ||
| 
     | 
||
| packages := make([]base.ImportedPackage, 0, len(g.gvtConfig.Deps)) | ||
| for _, pkg := range g.gvtConfig.Deps { | ||
| // Validate | ||
| if pkg.ImportPath == "" { | ||
| err := errors.New("invalid gvt configuration, ImportPath is required") | ||
| return nil, nil, err | ||
| } | ||
| 
     | 
||
| if pkg.Revision == "" { | ||
| err := errors.New("invalid gvt configuration, Revision is required") | ||
| return nil, nil, err | ||
| } | ||
| 
     | 
||
| var contstraintHint = "" | ||
| if pkg.Branch == "HEAD" { | ||
| // gb-vendor sets "branch" to "HEAD", if the package was feteched via -tag or -revision, | ||
| // we pass the revision as the constraint hint | ||
| contstraintHint = pkg.Revision | ||
| } else if pkg.Branch != "master" { | ||
| // both gvt & gb-vendor set "branch" to "master" unless a different branch was requested. | ||
| // so it's not realy a constraint unless it's a different branch | ||
| contstraintHint = pkg.Branch | ||
| } | ||
| 
     | 
||
| ip := base.ImportedPackage{ | ||
| Name: pkg.ImportPath, | ||
| Source: pkg.Repository, | ||
| LockHint: pkg.Revision, | ||
| ConstraintHint: contstraintHint, | ||
| } | ||
| packages = append(packages, ip) | ||
| } | ||
| 
     | 
||
| err := g.ImportPackages(packages, true) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| 
     | 
||
| return g.Manifest, g.Lock, nil | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the first entry in
sourceURLsdoesn't exist, does dep try the other ones? e.g. If the imported source matched the second entry, but we don't import it and left the source blank, would dep automatically try the first, realize it doesn't exist and fall back to that second entry?I am a bit confused as to why only the first entry is the only "default". I thought they all were defaults that dep would try?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
regarding you first question, yes, I believe dep will try the next URL in order, if one doesn't exist.
I compare only to the first one following the guidance of @sdboyer - good chance I misunderstood something though. My guess for this, is that if a tool choose not the
https:URL, but thesshone for example, we better respect that and not try fetching viahttpseven if it works.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me, thanks!