github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/buildtools/buck/cmd.go (about)

     1  package buck
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  
     7  	"github.com/fossas/fossa-cli/errors"
     8  	"github.com/fossas/fossa-cli/exec"
     9  )
    10  
    11  // NewCmd creates a function that executes the chosen buck executable.
    12  func NewCmd(name string) func(string, ...string) (string, error) {
    13  	return func(cmd string, args ...string) (string, error) {
    14  		out, _, err := exec.Run(exec.Cmd{
    15  			Name: name,
    16  			Argv: append([]string{cmd}, args...),
    17  		})
    18  
    19  		if err != nil {
    20  			return out, errors.Wrapf(err, "Could not run `%s %s %+v` within the current directory", name, cmd, args)
    21  		}
    22  		return out, nil
    23  	}
    24  }
    25  
    26  func cmdAudit(command func(string, ...string) (string, error), cmd string, argv ...string) (AuditOutput, error) {
    27  	var output AuditOutput
    28  	arguments := append([]string{cmd, "--json"}, argv...)
    29  	out, err := command("audit", arguments...)
    30  	if err != nil {
    31  		return output, errors.Wrapf(err, "Could not run `buck audit %s --json %+v` within the current directory", cmd, argv)
    32  	}
    33  
    34  	err = json.Unmarshal([]byte(out), &output.OutputMapping)
    35  	if err != nil {
    36  		return output, errors.Wrap(err, "Could not unmarshal `buck audit` JSON into dependency list")
    37  	}
    38  	return output, nil
    39  }
    40  
    41  func cmdTargets(command func(string, ...string) (string, error), argv ...string) ([]string, error) {
    42  	targets := []string{}
    43  	out, err := command("targets", argv...)
    44  	if err != nil {
    45  		return targets, err
    46  	}
    47  
    48  	for _, target := range strings.Split(out, "\n") {
    49  		if len(target) > 0 {
    50  			targets = append(targets, target)
    51  		}
    52  	}
    53  	return targets, nil
    54  }