Skip to content

Commit

Permalink
Add cmd flag to choose which type of dependencies to print
Browse files Browse the repository at this point in the history
  • Loading branch information
ret2libc committed Jul 13, 2022
1 parent 5d57af5 commit 0c9c28c
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions cmd/deplist/deplist.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,56 @@ package main
import (
"flag"
"fmt"
"strings"

"github.com/RedHatProductSecurity/deplist"
purl "github.com/mcoops/packageurl-go"
log "github.com/sirupsen/logrus"
)

type searchModeFlag []string

func (i *searchModeFlag) String() string {
return strings.Join(*i, ", ")
}

func (i *searchModeFlag) Set(value string) error {
values := strings.Split(value, ",")
for _, value := range values {
switch value {
case "deps":
*i = append(*i, "deps")
case "bundled":
*i = append(*i, "bundled")
}
}
return nil
}

func (i *searchModeFlag) ShouldHandleDep(dep deplist.Dependency) bool {
res := false
for _, searchMode := range *i {
switch searchMode {
case "deps":
res = res || !dep.IsBundled
case "bundled":
res = res || dep.IsBundled
}
}
return res
}

func main() {
deptypePtr := flag.Int("deptype", -1, "golang, nodejs, python etc")
debugPtr := flag.Bool("debug", false, "debug logging (default false)")
var searchModes searchModeFlag
flag.Var(&searchModes, "modes", "search mode (bundled, deps)")

flag.Parse()

if len(searchModes) == 0 {
searchModes = []string{"deps", "bundled"}
}
if *debugPtr == true {
log.SetLevel(log.DebugLevel)
}
Expand All @@ -33,23 +71,24 @@ func main() {

if *deptypePtr == -1 {
for _, dep := range deps {
if !searchModes.ShouldHandleDep(dep) {
continue
}
version := dep.Version

inst, _ := purl.FromString(fmt.Sprintf("pkg:%s/%s@%s", deplist.GetLanguageStr(dep.DepType), dep.Path, version))
fmt.Print(inst)
if dep.IsBundled {
fmt.Print(" [bundled]")
}
fmt.Println()
}
} else {
deptype := deplist.Bitmask(*deptypePtr)
for _, dep := range deps {
if !searchModes.ShouldHandleDep(dep) {
continue
}

if (dep.DepType & deptype) == deptype {
fmt.Printf("%s@%s", dep.Path, dep.Version)
if dep.IsBundled {
fmt.Print(" [bundled]")
}
fmt.Println()
}
}
Expand Down

0 comments on commit 0c9c28c

Please sign in to comment.