Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit b005525

Browse files
authored
Merge pull request #1422 from s111/glock-importer
Add importer for github.com/robfig/glock
2 parents ef6a28f + 43b1b5b commit b005525

File tree

14 files changed

+344
-3
lines changed

14 files changed

+344
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# v0.3.3 (Unreleased)
22

33
NEW FEATURES:
4+
* Add support for importing from [glock](https://github.com/robfig/glock) based projects (#1422).
45
* Add support for importing from [govendor](https://github.com/kardianos/govendor)
56
based projects (#815).
67

cmd/dep/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
// When configuration for another dependency management tool is detected, it is
3939
// imported into the initial manifest and lock. Use the -skip-tools flag to
4040
// disable this behavior. The following external tools are supported:
41-
// glide, godep, vndr, govend, gb, gvt.
41+
// glide, godep, vndr, govend, gb, gvt, glock.
4242
//
4343
// Any dependencies that are not constrained by external configuration use the
4444
// GOPATH analysis below.

cmd/dep/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ specified, use the current directory.
3030
When configuration for another dependency management tool is detected, it is
3131
imported into the initial manifest and lock. Use the -skip-tools flag to
3232
disable this behavior. The following external tools are supported:
33-
glide, godep, vndr, govend, gb, gvt, govendor.
33+
glide, godep, vndr, govend, gb, gvt, govendor, glock.
3434
3535
Any dependencies that are not constrained by external configuration use the
3636
GOPATH analysis below.

cmd/dep/testdata/harness_tests/init/glock/case1/final/Gopkg.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
[[constraint]]
3+
name = "github.com/sdboyer/deptestdos"
4+
version = "2.0.0"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cmd github.com/golang/lint
2+
github.com/sdboyer/deptest 3f4c3bea144e112a69bbe5d8d01c1b09a544253f
3+
github.com/sdboyer/deptestdos 5c607206be5decd28e6263ffffdcee067266015e
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 main
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/sdboyer/deptestdos"
11+
)
12+
13+
func main() {
14+
var x deptestdos.Bar
15+
fmt.Println(x)
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"commands": [
3+
["init", "-no-examples"]
4+
],
5+
"error-expected": "",
6+
"gopath-initial": {
7+
"github.com/sdboyer/deptest": "3f4c3bea144e112a69bbe5d8d01c1b09a544253f"
8+
},
9+
"vendor-final": [
10+
"github.com/sdboyer/deptest",
11+
"github.com/sdboyer/deptestdos"
12+
]
13+
}

docs/FAQ.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ about what's going on.
217217
During `dep init` configuration from other dependency managers is detected
218218
and imported, unless `-skip-tools` is specified.
219219

220-
The following tools are supported: `glide`, `godep`, `vndr`, `govend`, `gb`, `gvt` and `govendor`.
220+
The following tools are supported: `glide`, `godep`, `vndr`, `govend`, `gb`, `gvt`, `govendor` and `glock`.
221221

222222
See [#186](https://github.com/golang/dep/issues/186#issuecomment-306363441) for
223223
how to add support for another tool.

internal/importers/glock/importer.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Copyright 2016 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 glock
6+
7+
import (
8+
"bufio"
9+
"fmt"
10+
"log"
11+
"os"
12+
"path/filepath"
13+
"strings"
14+
15+
"github.com/golang/dep"
16+
"github.com/golang/dep/gps"
17+
"github.com/golang/dep/internal/importers/base"
18+
"github.com/pkg/errors"
19+
)
20+
21+
const glockfile = "GLOCKFILE"
22+
23+
// Importer imports glock configuration into the dep configuration format.
24+
type Importer struct {
25+
*base.Importer
26+
27+
packages []glockPackage
28+
}
29+
30+
// NewImporter for glock.
31+
func NewImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *Importer {
32+
return &Importer{Importer: base.NewImporter(logger, verbose, sm)}
33+
}
34+
35+
// Name of the importer.
36+
func (g *Importer) Name() string {
37+
return "glock"
38+
}
39+
40+
// HasDepMetadata checks if a directory contains config that the importer can handle.
41+
func (g *Importer) HasDepMetadata(dir string) bool {
42+
path := filepath.Join(dir, glockfile)
43+
if _, err := os.Stat(path); err != nil {
44+
return false
45+
}
46+
47+
return true
48+
}
49+
50+
// Import the config found in the directory.
51+
func (g *Importer) Import(dir string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
52+
err := g.load(dir)
53+
if err != nil {
54+
return nil, nil, err
55+
}
56+
57+
return g.convert(pr)
58+
}
59+
60+
type glockPackage struct {
61+
importPath string
62+
revision string
63+
}
64+
65+
func (g *Importer) load(projectDir string) error {
66+
g.Logger.Println("Detected glock configuration files...")
67+
path := filepath.Join(projectDir, glockfile)
68+
if g.Verbose {
69+
g.Logger.Printf(" Loading %s", path)
70+
}
71+
72+
f, err := os.Open(path)
73+
if err != nil {
74+
return errors.Wrapf(err, "unable to open %s", path)
75+
}
76+
defer f.Close()
77+
78+
scanner := bufio.NewScanner(f)
79+
for scanner.Scan() {
80+
pkg, err := parseGlockLine(scanner.Text())
81+
if err != nil {
82+
return err
83+
}
84+
if pkg == nil {
85+
continue
86+
}
87+
g.packages = append(g.packages, *pkg)
88+
}
89+
90+
return nil
91+
}
92+
93+
func parseGlockLine(line string) (*glockPackage, error) {
94+
fields := strings.Fields(line)
95+
switch len(fields) {
96+
case 2: // Valid.
97+
case 0: // Skip empty lines.
98+
return nil, nil
99+
default:
100+
return nil, fmt.Errorf("invalid glock configuration: %s", line)
101+
}
102+
103+
// Skip commands.
104+
if fields[0] == "cmd" {
105+
return nil, nil
106+
}
107+
return &glockPackage{
108+
importPath: fields[0],
109+
revision: fields[1],
110+
}, nil
111+
}
112+
113+
func (g *Importer) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
114+
g.Logger.Println("Converting from GLOCKFILE ...")
115+
116+
packages := make([]base.ImportedPackage, 0, len(g.packages))
117+
for _, pkg := range g.packages {
118+
// Validate
119+
if pkg.importPath == "" {
120+
return nil, nil, errors.New("invalid glock configuration, import path is required")
121+
}
122+
123+
if pkg.revision == "" {
124+
return nil, nil, errors.New("invalid glock configuration, revision is required")
125+
}
126+
127+
packages = append(packages, base.ImportedPackage{
128+
Name: pkg.importPath,
129+
LockHint: pkg.revision,
130+
})
131+
}
132+
133+
err := g.ImportPackages(packages, true)
134+
if err != nil {
135+
return nil, nil, err
136+
}
137+
138+
return g.Manifest, g.Lock, nil
139+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright 2016 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 glock
6+
7+
import (
8+
"bytes"
9+
"fmt"
10+
"log"
11+
"path/filepath"
12+
"testing"
13+
14+
"github.com/golang/dep"
15+
"github.com/golang/dep/gps"
16+
"github.com/golang/dep/internal/importers/importertest"
17+
"github.com/golang/dep/internal/test"
18+
"github.com/pkg/errors"
19+
)
20+
21+
func TestGlockConfig_Convert(t *testing.T) {
22+
testCases := map[string]struct {
23+
importertest.TestCase
24+
packages []glockPackage
25+
}{
26+
"package": {
27+
importertest.TestCase{
28+
WantConstraint: importertest.V1Constraint,
29+
WantRevision: importertest.V1Rev,
30+
WantVersion: importertest.V1Tag,
31+
},
32+
[]glockPackage{
33+
{
34+
importPath: importertest.Project,
35+
revision: importertest.V1Rev,
36+
},
37+
},
38+
},
39+
"missing package name": {
40+
importertest.TestCase{
41+
WantConvertErr: true,
42+
},
43+
[]glockPackage{{importPath: ""}},
44+
},
45+
"missing revision": {
46+
importertest.TestCase{
47+
WantConvertErr: true,
48+
},
49+
[]glockPackage{{importPath: importertest.Project}},
50+
},
51+
}
52+
53+
for name, testCase := range testCases {
54+
name := name
55+
testCase := testCase
56+
t.Run(name, func(t *testing.T) {
57+
err := testCase.Execute(t, func(logger *log.Logger, sm gps.SourceManager) (*dep.Manifest, *dep.Lock, error) {
58+
g := NewImporter(logger, true, sm)
59+
g.packages = testCase.packages
60+
return g.convert(importertest.RootProject)
61+
})
62+
if err != nil {
63+
t.Fatalf("%#v", err)
64+
}
65+
})
66+
}
67+
}
68+
69+
func TestGlockConfig_LoadInvalid(t *testing.T) {
70+
const testLine = "github.com/sdboyer/deptest 3f4c3bea144e112a69bbe5d8d01c1b09a544253f invalid"
71+
_, err := parseGlockLine(testLine)
72+
expected := fmt.Errorf("invalid glock configuration: %s", testLine)
73+
if err.Error() != expected.Error() {
74+
t.Errorf("want error %s, got %s", err, expected)
75+
}
76+
}
77+
78+
func TestGlockConfig_LoadEmptyLine(t *testing.T) {
79+
pkg, err := parseGlockLine("")
80+
if err != nil {
81+
t.Fatalf("%#v", err)
82+
}
83+
if pkg != nil {
84+
t.Errorf("want package nil, got %+v", pkg)
85+
}
86+
}
87+
88+
func TestGlockConfig_Import(t *testing.T) {
89+
h := test.NewHelper(t)
90+
defer h.Cleanup()
91+
92+
ctx := importertest.NewTestContext(h)
93+
sm, err := ctx.SourceManager()
94+
h.Must(err)
95+
defer sm.Release()
96+
97+
h.TempDir(filepath.Join("src", importertest.RootProject))
98+
h.TempCopy(filepath.Join(importertest.RootProject, glockfile), glockfile)
99+
projectRoot := h.Path(importertest.RootProject)
100+
101+
// Capture stderr so we can verify output
102+
verboseOutput := &bytes.Buffer{}
103+
ctx.Err = log.New(verboseOutput, "", 0)
104+
105+
g := NewImporter(ctx.Err, false, sm) // Disable verbose so that we don't print values that change each test run
106+
if !g.HasDepMetadata(projectRoot) {
107+
t.Fatal("Expected the importer to detect the glock configuration files")
108+
}
109+
110+
m, l, err := g.Import(projectRoot, importertest.RootProject)
111+
h.Must(err)
112+
113+
if m == nil {
114+
t.Fatal("Expected the manifest to be generated")
115+
}
116+
117+
if l == nil {
118+
t.Fatal("Expected the lock to be generated")
119+
}
120+
121+
goldenFile := "golden.txt"
122+
got := verboseOutput.String()
123+
want := h.GetTestFileString(goldenFile)
124+
if want != got {
125+
if *test.UpdateGolden {
126+
if err := h.WriteTestFile(goldenFile, got); err != nil {
127+
t.Fatalf("%+v", errors.Wrapf(err, "Unable to write updated golden file %s", goldenFile))
128+
}
129+
} else {
130+
t.Fatalf("want %s, got %s", want, got)
131+
}
132+
}
133+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cmd github.com/golang/lint
2+
github.com/sdboyer/deptest 3f4c3bea144e112a69bbe5d8d01c1b09a544253f
3+
github.com/sdboyer/deptestdos 5c607206be5decd28e6263ffffdcee067266015e
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Detected glock configuration files...
2+
Converting from GLOCKFILE ...
3+
Using ^0.8.1 as initial constraint for imported dep github.com/sdboyer/deptest
4+
Trying v0.8.1 (3f4c3be) as initial lock for imported dep github.com/sdboyer/deptest
5+
Using ^2.0.0 as initial constraint for imported dep github.com/sdboyer/deptestdos
6+
Trying v2.0.0 (5c60720) as initial lock for imported dep github.com/sdboyer/deptestdos

internal/importers/importers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/golang/dep"
1111
"github.com/golang/dep/gps"
1212
"github.com/golang/dep/internal/importers/glide"
13+
"github.com/golang/dep/internal/importers/glock"
1314
"github.com/golang/dep/internal/importers/godep"
1415
"github.com/golang/dep/internal/importers/govend"
1516
"github.com/golang/dep/internal/importers/govendor"
@@ -39,5 +40,6 @@ func BuildAll(logger *log.Logger, verbose bool, sm gps.SourceManager) []Importer
3940
govend.NewImporter(logger, verbose, sm),
4041
gvt.NewImporter(logger, verbose, sm),
4142
govendor.NewImporter(logger, verbose, sm),
43+
glock.NewImporter(logger, verbose, sm),
4244
}
4345
}

0 commit comments

Comments
 (0)