podman
1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package packages6
7import (8"fmt"9"os"10"sort"11)
12
13// Visit visits all the packages in the import graph whose roots are
14// pkgs, calling the optional pre function the first time each package
15// is encountered (preorder), and the optional post function after a
16// package's dependencies have been visited (postorder).
17// The boolean result of pre(pkg) determines whether
18// the imports of package pkg are visited.
19func Visit(pkgs []*Package, pre func(*Package) bool, post func(*Package)) {20seen := make(map[*Package]bool)21var visit func(*Package)22visit = func(pkg *Package) {23if !seen[pkg] {24seen[pkg] = true25
26if pre == nil || pre(pkg) {27paths := make([]string, 0, len(pkg.Imports))28for path := range pkg.Imports {29paths = append(paths, path)30}31sort.Strings(paths) // Imports is a map, this makes visit stable32for _, path := range paths {33visit(pkg.Imports[path])34}35}36
37if post != nil {38post(pkg)39}40}41}42for _, pkg := range pkgs {43visit(pkg)44}45}
46
47// PrintErrors prints to os.Stderr the accumulated errors of all
48// packages in the import graph rooted at pkgs, dependencies first.
49// PrintErrors returns the number of errors printed.
50func PrintErrors(pkgs []*Package) int {51var n int52Visit(pkgs, nil, func(pkg *Package) {53for _, err := range pkg.Errors {54fmt.Fprintln(os.Stderr, err)55n++56}57})58return n59}
60