Skip to content

Commit 264982b

Browse files
committed
Change FindDependencies to use functional options
1 parent 9fab5eb commit 264982b

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

cmd/jsonnet-deps/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func main() {
193193
}
194194
}
195195

196-
dependencies, err := vm.FindDependencies("", conf.inputFiles, true)
196+
dependencies, err := vm.FindDependencies("", conf.inputFiles)
197197
if err != nil {
198198
fmt.Fprintln(os.Stderr, err.Error())
199199
os.Exit(1)

vm.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func (vm *VM) evaluateSnippet(diagnosticFileName ast.DiagnosticFileName, filenam
237237
return output, nil
238238
}
239239

240-
func getAbsPath(path string, followSymlinks bool) (string, error) {
240+
func getAbsPath(path string, canonicalPaths bool) (string, error) {
241241
var absPath string
242242

243243
var err error
@@ -252,7 +252,7 @@ func getAbsPath(path string, followSymlinks bool) (string, error) {
252252
absPath = strings.Join([]string{wd, path}, string(filepath.Separator))
253253
}
254254

255-
if followSymlinks {
255+
if canonicalPaths {
256256
absPath, err = filepath.EvalSymlinks(absPath)
257257
if err != nil {
258258
return "", err
@@ -262,7 +262,7 @@ func getAbsPath(path string, followSymlinks bool) (string, error) {
262262
return absPath, nil
263263
}
264264

265-
func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map[string]struct{}, stackTrace *[]TraceFrame, followSymlinks bool) (err error) {
265+
func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map[string]struct{}, stackTrace *[]TraceFrame, canonicalPaths bool) (err error) {
266266
var cleanedAbsPath string
267267
switch i := (*node).(type) {
268268
case *ast.Import:
@@ -273,7 +273,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
273273
}
274274
cleanedAbsPath = foundAt
275275
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
276-
cleanedAbsPath, err = getAbsPath(foundAt, followSymlinks)
276+
cleanedAbsPath, err = getAbsPath(foundAt, canonicalPaths)
277277
if err != nil {
278278
*stackTrace = append([]TraceFrame{{Loc: *i.Loc()}}, *stackTrace...)
279279
return err
@@ -284,7 +284,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
284284
return nil
285285
}
286286
dependencies[cleanedAbsPath] = struct{}{}
287-
err = vm.findDependencies(foundAt, &node, dependencies, stackTrace, followSymlinks)
287+
err = vm.findDependencies(foundAt, &node, dependencies, stackTrace, canonicalPaths)
288288
if err != nil {
289289
*stackTrace = append([]TraceFrame{{Loc: *i.Loc()}}, *stackTrace...)
290290
return err
@@ -297,7 +297,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
297297
}
298298
cleanedAbsPath = foundAt
299299
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
300-
cleanedAbsPath, err = getAbsPath(foundAt, followSymlinks)
300+
cleanedAbsPath, err = getAbsPath(foundAt, canonicalPaths)
301301
if err != nil {
302302
*stackTrace = append([]TraceFrame{{Loc: *i.Loc()}}, *stackTrace...)
303303
return err
@@ -312,7 +312,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
312312
}
313313
cleanedAbsPath = foundAt
314314
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
315-
cleanedAbsPath, err = getAbsPath(foundAt, followSymlinks)
315+
cleanedAbsPath, err = getAbsPath(foundAt, canonicalPaths)
316316
if err != nil {
317317
*stackTrace = append([]TraceFrame{{Loc: *i.Loc()}}, *stackTrace...)
318318
return err
@@ -321,7 +321,7 @@ func (vm *VM) findDependencies(filePath string, node *ast.Node, dependencies map
321321
dependencies[cleanedAbsPath] = struct{}{}
322322
default:
323323
for _, node := range parser.Children(i) {
324-
err = vm.findDependencies(filePath, &node, dependencies, stackTrace, followSymlinks)
324+
err = vm.findDependencies(filePath, &node, dependencies, stackTrace, canonicalPaths)
325325
if err != nil {
326326
return err
327327
}
@@ -462,10 +462,28 @@ func (vm *VM) EvaluateFileMulti(filename string) (files map[string]string, forma
462462
return output, nil
463463
}
464464

465+
type findDepsConfig struct {
466+
canonicalPaths bool
467+
}
468+
469+
type FindDepsOption func(c *findDepsConfig)
470+
471+
func WithCanonicalPaths(canonicalize bool) FindDepsOption {
472+
return func(c *findDepsConfig) { c.canonicalPaths = canonicalize }
473+
}
474+
465475
// FindDependencies returns a sorted array of unique transitive dependencies (via import/importstr/importbin)
466476
// from all the given `importedPaths` which are themselves excluded from the returned array.
467477
// The `importedPaths` are parsed as if they were imported from a Jsonnet file located at `importedFrom`.
468-
func (vm *VM) FindDependencies(importedFrom string, importedPaths []string, followSymlinks bool) ([]string, error) {
478+
func (vm *VM) FindDependencies(importedFrom string, importedPaths []string, opts ...FindDepsOption) ([]string, error) {
479+
config := findDepsConfig{
480+
canonicalPaths: true,
481+
}
482+
483+
for _, f := range opts {
484+
f(&config)
485+
}
486+
469487
var nodes []*ast.Node
470488
var stackTrace []TraceFrame
471489
filePaths := make([]string, len(importedPaths))
@@ -479,7 +497,7 @@ func (vm *VM) FindDependencies(importedFrom string, importedPaths []string, foll
479497
}
480498
cleanedAbsPath := foundAt
481499
if _, isFileImporter := vm.importer.(*FileImporter); isFileImporter {
482-
cleanedAbsPath, err = getAbsPath(foundAt, followSymlinks)
500+
cleanedAbsPath, err = getAbsPath(foundAt, config.canonicalPaths)
483501
if err != nil {
484502
return nil, err
485503
}
@@ -494,7 +512,7 @@ func (vm *VM) FindDependencies(importedFrom string, importedPaths []string, foll
494512
}
495513

496514
for i, filePath := range filePaths {
497-
err := vm.findDependencies(filePath, nodes[i], deps, &stackTrace, followSymlinks)
515+
err := vm.findDependencies(filePath, nodes[i], deps, &stackTrace, config.canonicalPaths)
498516
if err != nil {
499517
err = makeRuntimeError(err.Error(), stackTrace)
500518
return nil, errors.New(vm.ErrorFormatter.Format(err))

0 commit comments

Comments
 (0)