github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/buildtools/debian/apt-cache.go (about)

     1  package debian
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/fossas/fossa-cli/exec"
     7  )
     8  
     9  // Return all direct dependencies as an array of strings.
    10  // Currently we ignore all virtual dependencies but include "Suggested" deps
    11  // which are later verified before inclusion in the dep graph.
    12  func directDeps(command func(...string) (string, error), target string) ([]string, error) {
    13  	output, err := command(target)
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  	dependencies := []string{}
    18  	lines := strings.Split(output, "\n")
    19  	for _, line := range lines {
    20  		dep := strings.Split(strings.Replace(line, " ", "", -1), ":")
    21  		if len(dep) > 1 && !strings.HasPrefix(dep[1], "<") {
    22  			dependencies = append(dependencies, dep[1])
    23  		}
    24  	}
    25  	return dependencies, nil
    26  }
    27  
    28  // Return all transitive dependencies as an array of strings.
    29  // Ignore all virtual dependencies and it is important to note that this
    30  // can return duplicates.
    31  func transitiveDeps(command func(...string) (string, error), target string) (map[string]string, error) {
    32  	output, err := command("--recurse", target)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	dependencies := make(map[string]string)
    38  	lines := strings.Split(output, "\n")
    39  	for _, line := range lines {
    40  		dep := strings.Split(strings.Replace(line, " ", "", -1), ":")
    41  		if len(dep) == 1 && dep[0] != "" && !strings.HasPrefix(dep[0], "<") {
    42  			dependencies[dep[0]] = ""
    43  		}
    44  	}
    45  	return dependencies, nil
    46  }
    47  
    48  // Return the string output from running "apt-cache" with the supplied arguments.
    49  func aptCache(argv ...string) (string, error) {
    50  	out, _, err := exec.Run(exec.Cmd{
    51  		Name: "apt-cache",
    52  		Argv: append([]string{"depends"}, argv...),
    53  	})
    54  	if err != nil {
    55  		return "", nil
    56  	}
    57  	return out, nil
    58  }