@@ -15,13 +15,11 @@ import (
15
15
16
16
"github.com/golang/dep"
17
17
"github.com/golang/dep/gps"
18
- "github.com/golang/dep/gps/paths"
19
- "github.com/golang/dep/gps/pkgtree"
20
18
"github.com/golang/dep/internal/fs"
21
19
"github.com/pkg/errors"
22
20
)
23
21
24
- const initShortHelp = `Initialize a new project with manifest and lock files `
22
+ const initShortHelp = `Set up a new Go project, or migrate an existing one `
25
23
const initLongHelp = `
26
24
Initialize the project at filepath root by parsing its dependencies, writing
27
25
manifest and lock files, and vendoring the dependencies. If root isn't
@@ -89,43 +87,10 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
89
87
}
90
88
}
91
89
92
- var err error
93
- p := new (dep.Project )
94
- if err = p .SetRoot (root ); err != nil {
95
- return errors .Wrapf (err , "init failed: unable to set the root project to %s" , root )
96
- }
97
-
98
- ctx .GOPATH , err = ctx .DetectProjectGOPATH (p )
99
- if err != nil {
100
- return errors .Wrapf (err , "init failed: unable to detect the containing GOPATH" )
101
- }
102
-
103
- mf := filepath .Join (root , dep .ManifestName )
104
- lf := filepath .Join (root , dep .LockName )
105
- vpath := filepath .Join (root , "vendor" )
106
-
107
- mok , err := fs .IsRegular (mf )
90
+ p , err := cmd .establishProjectAt (root , ctx )
108
91
if err != nil {
109
- return errors . Wrapf ( err , "init failed: unable to check for an existing manifest at %s" , mf )
92
+ return err
110
93
}
111
- if mok {
112
- return errors .Errorf ("init aborted: manifest already exists at %s" , mf )
113
- }
114
- // Manifest file does not exist.
115
-
116
- lok , err := fs .IsRegular (lf )
117
- if err != nil {
118
- return errors .Wrapf (err , "init failed: unable to check for an existing lock at %s" , lf )
119
- }
120
- if lok {
121
- return errors .Errorf ("invalid aborted: lock already exists at %s" , lf )
122
- }
123
-
124
- ip , err := ctx .ImportForAbs (root )
125
- if err != nil {
126
- return errors .Wrapf (err , "init failed: unable to determine the import path for the root project %s" , root )
127
- }
128
- p .ImportRoot = gps .ProjectRoot (ip )
129
94
130
95
sm , err := ctx .SourceManager ()
131
96
if err != nil {
@@ -137,12 +102,13 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
137
102
if ctx .Verbose {
138
103
ctx .Out .Println ("Getting direct dependencies..." )
139
104
}
140
- pkgT , directDeps , err := getDirectDependencies (sm , p )
105
+
106
+ ptree , directDeps , err := p .GetDirectDependencyNames (sm )
141
107
if err != nil {
142
108
return errors .Wrap (err , "init failed: unable to determine direct dependencies" )
143
109
}
144
110
if ctx .Verbose {
145
- ctx .Out .Printf ("Checked %d directories for packages.\n Found %d direct dependencies.\n " , len (pkgT .Packages ), len (directDeps ))
111
+ ctx .Out .Printf ("Checked %d directories for packages.\n Found %d direct dependencies.\n " , len (ptree .Packages ), len (directDeps ))
146
112
}
147
113
148
114
// Initialize with imported data, then fill in the gaps using the GOPATH
@@ -165,7 +131,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
165
131
166
132
params := gps.SolveParameters {
167
133
RootDir : root ,
168
- RootPackageTree : pkgT ,
134
+ RootPackageTree : ptree ,
169
135
Manifest : p .Manifest ,
170
136
Lock : p .Lock ,
171
137
ProjectAnalyzer : rootAnalyzer ,
@@ -203,7 +169,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
203
169
p .Lock .SolveMeta .InputsDigest = s .HashInputs ()
204
170
205
171
// Pass timestamp (yyyyMMddHHmmss format) as suffix to backup name.
206
- vendorbak , err := dep .BackupVendor (vpath , time .Now ().Format ("20060102150405" ))
172
+ vendorbak , err := dep .BackupVendor (filepath . Join ( root , "vendor" ) , time .Now ().Format ("20060102150405" ))
207
173
if err != nil {
208
174
return errors .Wrap (err , "init failed: first backup vendor/, delete it, and then retry the previous command: failed to backup existing vendor directory" )
209
175
}
@@ -227,32 +193,50 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
227
193
return nil
228
194
}
229
195
230
- func getDirectDependencies (sm gps.SourceManager , p * dep.Project ) (pkgtree.PackageTree , map [string ]bool , error ) {
231
- pkgT , err := p .ParseRootPackageTree ()
196
+ // establishProjectAt attempts to set up the provided path as the root for the
197
+ // project to be created.
198
+ //
199
+ // It checks for being within a GOPATH, that there is no pre-existing manifest
200
+ // and lock, and that we can successfully infer the root import path from
201
+ // GOPATH.
202
+ //
203
+ // If successful, it returns a dep.Project, ready for further use.
204
+ func (cmd * initCommand ) establishProjectAt (root string , ctx * dep.Ctx ) (* dep.Project , error ) {
205
+ var err error
206
+ p := new (dep.Project )
207
+ if err = p .SetRoot (root ); err != nil {
208
+ return nil , errors .Wrapf (err , "init failed: unable to set the root project to %s" , root )
209
+ }
210
+
211
+ ctx .GOPATH , err = ctx .DetectProjectGOPATH (p )
232
212
if err != nil {
233
- return pkgtree. PackageTree {}, nil , err
213
+ return nil , errors . Wrapf ( err , "init failed: unable to detect the containing GOPATH" )
234
214
}
235
215
236
- directDeps := map [string ]bool {}
237
- rm , _ := pkgT .ToReachMap (true , true , false , nil )
238
- for _ , ip := range rm .FlattenFn (paths .IsStandardImportPath ) {
239
- pr , err := sm .DeduceProjectRoot (ip )
240
- if err != nil {
241
- return pkgtree.PackageTree {}, nil , err
242
- }
243
- directDeps [string (pr )] = true
216
+ mf := filepath .Join (root , dep .ManifestName )
217
+ lf := filepath .Join (root , dep .LockName )
218
+
219
+ mok , err := fs .IsRegular (mf )
220
+ if err != nil {
221
+ return nil , errors .Wrapf (err , "init failed: unable to check for an existing manifest at %s" , mf )
222
+ }
223
+ if mok {
224
+ return nil , errors .Errorf ("init aborted: manifest already exists at %s" , mf )
244
225
}
245
226
246
- return pkgT , directDeps , nil
247
- }
227
+ lok , err := fs .IsRegular (lf )
228
+ if err != nil {
229
+ return nil , errors .Wrapf (err , "init failed: unable to check for an existing lock at %s" , lf )
230
+ }
231
+ if lok {
232
+ return nil , errors .Errorf ("invalid aborted: lock already exists at %s" , lf )
233
+ }
248
234
249
- // TODO solve failures can be really creative - we need to be similarly creative
250
- // in handling them and informing the user appropriately
251
- func handleAllTheFailuresOfTheWorld (err error ) error {
252
- switch errors .Cause (err ) {
253
- case context .Canceled , context .DeadlineExceeded , gps .ErrSourceManagerIsReleased :
254
- return nil
235
+ ip , err := ctx .ImportForAbs (root )
236
+ if err != nil {
237
+ return nil , errors .Wrapf (err , "init failed: unable to determine the import path for the root project %s" , root )
255
238
}
239
+ p .ImportRoot = gps .ProjectRoot (ip )
256
240
257
- return errors . Wrap ( err , "Solving failure" )
241
+ return p , nil
258
242
}
0 commit comments