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.
Add importer for github.com/LK4D4/vndr (#978)
- Loading branch information
Showing
12 changed files
with
545 additions
and
22 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
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
21 changes: 21 additions & 0 deletions
21
cmd/dep/testdata/harness_tests/init/vndr/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.
4 changes: 4 additions & 0 deletions
4
cmd/dep/testdata/harness_tests/init/vndr/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,4 @@ | ||
|
||
[[constraint]] | ||
name = "github.com/sdboyer/deptestdos" | ||
version = "2.0.0" |
16 changes: 16 additions & 0 deletions
16
cmd/dep/testdata/harness_tests/init/vndr/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,16 @@ | ||
// 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/deptestdos" | ||
) | ||
|
||
func main() { | ||
var x deptestdos.Bar | ||
fmt.Println(x) | ||
} |
3 changes: 3 additions & 0 deletions
3
cmd/dep/testdata/harness_tests/init/vndr/case1/initial/vendor.conf
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,3 @@ | ||
# Comment on its own line | ||
github.com/sdboyer/deptest 3f4c3bea144e112a69bbe5d8d01c1b09a544253f https://github.com/sdboyer/deptest.git | ||
github.com/sdboyer/deptestdos v2.0.0 |
13 changes: 13 additions & 0 deletions
13
cmd/dep/testdata/harness_tests/init/vndr/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Detected vndr configuration file... | ||
Converting from vendor.conf... | ||
Using 3f4c3bea144e112a69bbe5d8d01c1b09a544253f as initial hint for imported dep github.com/sdboyer/deptest | ||
Trying v0.8.1 (3f4c3be) as initial lock for imported dep github.com/sdboyer/deptest | ||
Using ^2.0.0 as initial constraint for imported dep github.com/sdboyer/deptestdos | ||
Trying * (v2.0.0) as initial lock for imported dep 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/sdboyer/deptest 3f4c3bea144e112a69bbe5d8d01c1b09a544253f https://github.com/sdboyer/deptest.git # trailing comment | ||
# line comment | ||
|
||
github.com/sdboyer/deptestdos v2.0.0 # trailing comment |
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,161 @@ | ||
// 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 ( | ||
"bufio" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/golang/dep" | ||
fb "github.com/golang/dep/internal/feedback" | ||
"github.com/golang/dep/internal/gps" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func vndrFile(dir string) string { | ||
return filepath.Join(dir, "vendor.conf") | ||
} | ||
|
||
type vndrImporter struct { | ||
packages []vndrPackage | ||
|
||
logger *log.Logger | ||
verbose bool | ||
sm gps.SourceManager | ||
} | ||
|
||
func newVndrImporter(log *log.Logger, verbose bool, sm gps.SourceManager) *vndrImporter { | ||
return &vndrImporter{ | ||
logger: log, | ||
verbose: verbose, | ||
sm: sm, | ||
} | ||
} | ||
|
||
func (v *vndrImporter) Name() string { return "vndr" } | ||
|
||
func (v *vndrImporter) HasDepMetadata(dir string) bool { | ||
_, err := os.Stat(vndrFile(dir)) | ||
return err == nil | ||
} | ||
|
||
func (v *vndrImporter) Import(dir string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) { | ||
v.logger.Println("Detected vndr configuration file...") | ||
|
||
err := v.loadVndrFile(dir) | ||
if err != nil { | ||
return nil, nil, errors.Wrapf(err, "unable to load vndr file") | ||
} | ||
|
||
return v.convert(pr) | ||
} | ||
|
||
func (v *vndrImporter) loadVndrFile(dir string) error { | ||
v.logger.Printf("Converting from vendor.conf...") | ||
|
||
f, err := os.Open(vndrFile(dir)) | ||
if err != nil { | ||
return errors.Wrapf(err, "Unable to open %s", vndrFile(dir)) | ||
} | ||
defer f.Close() | ||
|
||
scanner := bufio.NewScanner(f) | ||
for scanner.Scan() { | ||
pkg, err := parseVndrLine(scanner.Text()) | ||
if err != nil { | ||
return errors.Wrapf(err, "unable to parse line") | ||
} | ||
if pkg == nil { | ||
// Could be an empty line or one which is just a comment | ||
continue | ||
} | ||
v.packages = append(v.packages, *pkg) | ||
} | ||
|
||
if scanner.Err() != nil { | ||
return errors.Wrapf(err, "unable to read %s", vndrFile(dir)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (v *vndrImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) { | ||
var ( | ||
manifest = &dep.Manifest{ | ||
Constraints: make(gps.ProjectConstraints), | ||
} | ||
lock = &dep.Lock{} | ||
err error | ||
) | ||
|
||
for _, pkg := range v.packages { | ||
pc := gps.ProjectConstraint{ | ||
Ident: gps.ProjectIdentifier{ | ||
ProjectRoot: gps.ProjectRoot(pkg.importPath), | ||
Source: pkg.repository, | ||
}, | ||
} | ||
pc.Constraint, err = v.sm.InferConstraint(pkg.revision, pc.Ident) | ||
if err != nil { | ||
return nil, nil, errors.Wrapf(err, "Unable to interpret revision specifier '%s' for package %s", pkg.importPath, pkg.revision) | ||
} | ||
|
||
manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{ | ||
Source: pc.Ident.Source, | ||
Constraint: pc.Constraint, | ||
} | ||
fb.NewConstraintFeedback(pc, fb.DepTypeImported).LogFeedback(v.logger) | ||
|
||
revision := gps.Revision(pkg.revision) | ||
version, err := lookupVersionForLockedProject(pc.Ident, pc.Constraint, revision, v.sm) | ||
if err != nil { | ||
v.logger.Println(err.Error()) | ||
} | ||
|
||
lp := gps.NewLockedProject(pc.Ident, version, nil) | ||
|
||
lock.P = append(lock.P, lp) | ||
fb.NewLockedProjectFeedback(lp, fb.DepTypeImported).LogFeedback(v.logger) | ||
} | ||
|
||
return manifest, lock, nil | ||
} | ||
|
||
type vndrPackage struct { | ||
importPath string | ||
revision string | ||
repository string | ||
} | ||
|
||
func parseVndrLine(line string) (*vndrPackage, error) { | ||
commentIdx := strings.Index(line, "#") | ||
if commentIdx >= 0 { | ||
line = line[:commentIdx] | ||
} | ||
line = strings.TrimSpace(line) | ||
|
||
if line == "" { | ||
return nil, nil | ||
} | ||
|
||
parts := strings.Fields(line) | ||
|
||
if !(len(parts) == 2 || len(parts) == 3) { | ||
return nil, errors.Errorf("invalid config format: %q", line) | ||
} | ||
|
||
pkg := &vndrPackage{ | ||
importPath: parts[0], | ||
revision: parts[1], | ||
} | ||
if len(parts) == 3 { | ||
pkg.repository = parts[2] | ||
} | ||
|
||
return pkg, nil | ||
} |
Oops, something went wrong.