forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importer.go
139 lines (114 loc) · 2.97 KB
/
importer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2016 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 glock
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/golang/dep"
"github.com/golang/dep/gps"
"github.com/golang/dep/internal/importers/base"
"github.com/pkg/errors"
)
const glockfile = "GLOCKFILE"
// Importer imports glock configuration into the dep configuration format.
type Importer struct {
*base.Importer
packages []glockPackage
}
// NewImporter for glock.
func NewImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *Importer {
return &Importer{Importer: base.NewImporter(logger, verbose, sm)}
}
// Name of the importer.
func (g *Importer) Name() string {
return "glock"
}
// HasDepMetadata checks if a directory contains config that the importer can handle.
func (g *Importer) HasDepMetadata(dir string) bool {
path := filepath.Join(dir, glockfile)
if _, err := os.Stat(path); 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)
}
type glockPackage struct {
importPath string
revision string
}
func (g *Importer) load(projectDir string) error {
g.Logger.Println("Detected glock configuration files...")
path := filepath.Join(projectDir, glockfile)
if g.Verbose {
g.Logger.Printf(" Loading %s", path)
}
f, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "unable to open %s", path)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
pkg, err := parseGlockLine(scanner.Text())
if err != nil {
return err
}
if pkg == nil {
continue
}
g.packages = append(g.packages, *pkg)
}
return nil
}
func parseGlockLine(line string) (*glockPackage, error) {
fields := strings.Fields(line)
switch len(fields) {
case 2: // Valid.
case 0: // Skip empty lines.
return nil, nil
default:
return nil, fmt.Errorf("invalid glock configuration: %s", line)
}
// Skip commands.
if fields[0] == "cmd" {
return nil, nil
}
return &glockPackage{
importPath: fields[0],
revision: fields[1],
}, nil
}
func (g *Importer) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
g.Logger.Println("Converting from GLOCKFILE ...")
packages := make([]base.ImportedPackage, 0, len(g.packages))
for _, pkg := range g.packages {
// Validate
if pkg.importPath == "" {
return nil, nil, errors.New("invalid glock configuration, import path is required")
}
if pkg.revision == "" {
return nil, nil, errors.New("invalid glock configuration, revision is required")
}
packages = append(packages, base.ImportedPackage{
Name: pkg.importPath,
LockHint: pkg.revision,
})
}
err := g.ImportPackages(packages, true)
if err != nil {
return nil, nil, err
}
return g.Manifest, g.Lock, nil
}