This repository has been 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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gvt (and gb-vendor) importer (#1149)
- Loading branch information
1 parent
0979f44
commit 6fb987b
Showing
14 changed files
with
556 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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.