@@ -9,10 +9,13 @@ import (
9
9
"fmt"
10
10
"os"
11
11
"path/filepath"
12
+ "strings"
13
+
14
+ "github.com/sdboyer/gps"
12
15
)
13
16
14
- const ManifestName = "manifest.json"
15
- const LockName = "lock.json"
17
+ const manifestName = "manifest.json"
18
+ const lockName = "lock.json"
16
19
17
20
func main () {
18
21
flag .Parse ()
@@ -106,7 +109,7 @@ var initCmd = &command{
106
109
Write Manifest file in the root of the project directory.
107
110
` ,
108
111
long : `
109
- Populates Manifest file with current deps of this project.
112
+ Populates Manifest file with current deps of this project.
110
113
The specified version of each dependent repository is the version
111
114
available in the user's workspaces (as specified by GOPATH).
112
115
If the dependency is not present in any workspaces it is not be
@@ -116,57 +119,6 @@ var initCmd = &command{
116
119
` ,
117
120
}
118
121
119
- var statusCmd = & command {
120
- fn : noop ,
121
- name : "status" ,
122
- short : `[flags] [packages]
123
- Report the status of the current project's dependencies.
124
- ` ,
125
- long : `
126
- If no packages are specified, for each dependency:
127
- - root import path
128
- - (if present in lock) the currently selected version
129
- - (else) that it's missing from the lock
130
- - whether it's present in the vendor directory (or if it's in
131
- workspace, if that's a thing?)
132
- - the current aggregate constraints on that project (as specified by
133
- the Manifest)
134
- - if -u is specified, whether there are newer versions of this
135
- dependency
136
- - VCS state (uncommitted changes? pruned?)
137
-
138
- If packages are specified, or if -a is specified,
139
- for each of those dependencies:
140
- - (if present in lock) the currently selected version
141
- - (else) that it's missing from the lock
142
- - whether it's present in the vendor directory
143
- - The set of possible versions for that project
144
- - The upstream source URL(s) from which the project may be retrieved
145
- - The type of upstream source (git, hg, bzr, svn, registry)
146
- - Other versions that might work, given the current constraints
147
- - The list of all projects that import the project within the current
148
- depgraph
149
- - The current constraint. If more than one project constrains it, both
150
- the aggregate and the individual components (and which project provides
151
- that constraint) are printed
152
- - License information
153
- - Package source location, if fetched from an alternate location
154
-
155
- Flags:
156
- -json Output in json format
157
- -f [template] Output in text/template format
158
-
159
- -old Only show out of date packages and the current version
160
- -missing Only show missing packages.
161
- -unused Only show unused packages.
162
- -modified Only show modified packages.
163
-
164
- -dot Export dependency graph in GraphViz format
165
-
166
- The exit code of status is zero if all repositories are in a "good state".
167
- ` ,
168
- }
169
-
170
122
var getCmd = & command {
171
123
fn : noop ,
172
124
name : "get" ,
@@ -179,7 +131,7 @@ var getCmd = &command{
179
131
-x dry run
180
132
-f force the given package to be updated to the specified
181
133
version
182
-
134
+
183
135
Package specs:
184
136
<path>[@<version specifier>]
185
137
@@ -203,7 +155,7 @@ func findProjectRoot(from string) (string, error) {
203
155
var f func (string ) (string , error )
204
156
f = func (dir string ) (string , error ) {
205
157
206
- fullpath := filepath .Join (dir , ManifestName )
158
+ fullpath := filepath .Join (dir , manifestName )
207
159
208
160
if _ , err := os .Stat (fullpath ); err == nil {
209
161
return dir , nil
@@ -228,3 +180,83 @@ func findProjectRoot(from string) (string, error) {
228
180
}
229
181
return path , nil
230
182
}
183
+
184
+ type project struct {
185
+ // absroot is the absolute path to the root directory of the project.
186
+ absroot string
187
+ // importroot is the import path of the project's root directory.
188
+ importroot gps.ProjectRoot
189
+ m * manifest
190
+ l * lock
191
+ }
192
+
193
+ // loadProject searches for a project root from the provided path, then loads
194
+ // the manifest and lock (if any) it finds there.
195
+ //
196
+ // If the provided path is empty, it will search from the path indicated by
197
+ // os.Getwd().
198
+ func loadProject (path string ) (* project , error ) {
199
+ var err error
200
+ p := new (project )
201
+
202
+ switch path {
203
+ case "" :
204
+ p .absroot , err = findProjectRootFromWD ()
205
+ default :
206
+ p .absroot , err = findProjectRoot (path )
207
+ }
208
+
209
+ if err != nil {
210
+ return p , err
211
+ }
212
+
213
+ gopath := os .Getenv ("GOPATH" )
214
+ var match bool
215
+ for _ , gp := range filepath .SplitList (gopath ) {
216
+ srcprefix := filepath .Join (gp , "src" ) + string (filepath .Separator )
217
+ if strings .HasPrefix (p .absroot , srcprefix ) {
218
+ gopath = gp
219
+ match = true
220
+ // filepath.ToSlash because we're dealing with an import path now,
221
+ // not an fs path
222
+ p .importroot = gps .ProjectRoot (filepath .ToSlash (strings .TrimPrefix (p .absroot , srcprefix )))
223
+ break
224
+ }
225
+ }
226
+ if ! match {
227
+ return nil , fmt .Errorf ("could not determine project root - not on GOPATH" )
228
+ }
229
+
230
+ mp := filepath .Join (path , manifestName )
231
+ mf , err := os .Open (mp )
232
+ if err != nil {
233
+ // Should be impossible at this point for the manifest file not to
234
+ // exist, so this is some other kind of err
235
+ return nil , fmt .Errorf ("could not open %s: %s" , mp , err )
236
+ }
237
+ defer mf .Close ()
238
+
239
+ p .m , err = readManifest (mf )
240
+ if err != nil {
241
+ return nil , fmt .Errorf ("error while parsing %s: %s" , mp , err )
242
+ }
243
+
244
+ lp := filepath .Join (path , lockName )
245
+ lf , err := os .Open (lp )
246
+ if err != nil {
247
+ if os .IsNotExist (err ) {
248
+ // It's fine for the lock not to exist
249
+ return p , nil
250
+ }
251
+ // But if a lock does exist and we can't open it, that's a problem
252
+ return nil , fmt .Errorf ("could not open %s: %s" , lp , err )
253
+ }
254
+
255
+ defer lf .Close ()
256
+ p .l , err = readLock (lf )
257
+ if err != nil {
258
+ return nil , fmt .Errorf ("error while parsing %s: %s" , lp , err )
259
+ }
260
+
261
+ return p , nil
262
+ }
0 commit comments