@@ -15,13 +15,11 @@ import (
1515
1616 "github.com/golang/dep"
1717 "github.com/golang/dep/gps"
18- "github.com/golang/dep/gps/paths"
19- "github.com/golang/dep/gps/pkgtree"
2018 "github.com/golang/dep/internal/fs"
2119 "github.com/pkg/errors"
2220)
2321
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 `
2523const initLongHelp = `
2624Initialize the project at filepath root by parsing its dependencies, writing
2725manifest 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 {
8987 }
9088 }
9189
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 )
10891 if err != nil {
109- return errors . Wrapf ( err , "init failed: unable to check for an existing manifest at %s" , mf )
92+ return err
11093 }
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 )
12994
13095 sm , err := ctx .SourceManager ()
13196 if err != nil {
@@ -137,12 +102,13 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
137102 if ctx .Verbose {
138103 ctx .Out .Println ("Getting direct dependencies..." )
139104 }
140- pkgT , directDeps , err := getDirectDependencies (sm , p )
105+
106+ ptree , directDeps , err := p .GetDirectDependencyNames (sm )
141107 if err != nil {
142108 return errors .Wrap (err , "init failed: unable to determine direct dependencies" )
143109 }
144110 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 ))
146112 }
147113
148114 // 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 {
165131
166132 params := gps.SolveParameters {
167133 RootDir : root ,
168- RootPackageTree : pkgT ,
134+ RootPackageTree : ptree ,
169135 Manifest : p .Manifest ,
170136 Lock : p .Lock ,
171137 ProjectAnalyzer : rootAnalyzer ,
@@ -203,7 +169,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
203169 p .Lock .SolveMeta .InputsDigest = s .HashInputs ()
204170
205171 // 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" ))
207173 if err != nil {
208174 return errors .Wrap (err , "init failed: first backup vendor/, delete it, and then retry the previous command: failed to backup existing vendor directory" )
209175 }
@@ -227,32 +193,50 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
227193 return nil
228194}
229195
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 )
232212 if err != nil {
233- return pkgtree. PackageTree {}, nil , err
213+ return nil , errors . Wrapf ( err , "init failed: unable to detect the containing GOPATH" )
234214 }
235215
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 )
244225 }
245226
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+ }
248234
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 )
255238 }
239+ p .ImportRoot = gps .ProjectRoot (ip )
256240
257- return errors . Wrap ( err , "Solving failure" )
241+ return p , nil
258242}
0 commit comments