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.
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+
}

0 commit comments

Comments
 (0)