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
/
importer.go
128 lines (108 loc) · 3 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
// 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 godep
import (
"encoding/json"
"io/ioutil"
"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 godepPath = "Godeps" + string(os.PathSeparator) + "Godeps.json"
// Importer imports godep configuration into the dep configuration format.
type Importer struct {
*base.Importer
json godepJSON
}
// NewImporter for godep.
func NewImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *Importer {
return &Importer{Importer: base.NewImporter(logger, verbose, sm)}
}
type godepJSON struct {
Required []string `json:"Packages"`
Imports []godepPackage `json:"Deps"`
}
type godepPackage struct {
ImportPath string `json:"ImportPath"`
Rev string `json:"Rev"`
Comment string `json:"Comment"`
}
// Name of the importer.
func (g *Importer) Name() string {
return "godep"
}
// HasDepMetadata checks if a directory contains config that the importer can handle.
func (g *Importer) HasDepMetadata(dir string) bool {
y := filepath.Join(dir, godepPath)
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
}
m, l := g.convert(pr)
return m, l, nil
}
func (g *Importer) load(projectDir string) error {
g.Logger.Println("Detected godep configuration files...")
j := filepath.Join(projectDir, godepPath)
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.json)
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) {
g.Logger.Println("Converting from Godeps.json ...")
packages := make([]base.ImportedPackage, 0, len(g.json.Imports))
for _, pkg := range g.json.Imports {
// Validate
if pkg.ImportPath == "" {
g.Logger.Println(
" Warning: Skipping project. Invalid godep configuration, ImportPath is required",
)
continue
}
if pkg.Rev == "" {
g.Logger.Printf(
" Warning: Invalid godep configuration, Rev not found for ImportPath %q\n",
pkg.ImportPath,
)
}
ip := base.ImportedPackage{
Name: pkg.ImportPath,
LockHint: pkg.Rev,
ConstraintHint: pkg.Comment,
}
packages = append(packages, ip)
}
g.ImportPackages(packages, true)
required := make([]string, 0, len(g.json.Required))
for _, req := range g.json.Required {
if !strings.HasPrefix(req, ".") { // ignore project packages
required = append(required, req)
}
}
if len(required) > 0 {
g.Manifest.Required = required
}
return g.Manifest, g.Lock
}