Skip to content

Commit

Permalink
ruby manifesting v3 (hotfix for rbenv) (#27)
Browse files Browse the repository at this point in the history
* Better error handling manifesting ruby gems with rbenv
  • Loading branch information
cebarks authored Sep 12, 2022
1 parent 87e40c5 commit ba88cdd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
2 changes: 2 additions & 0 deletions cmd/deplist/deplist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/RedHatProductSecurity/deplist"
"github.com/RedHatProductSecurity/deplist/internal/scan"
purl "github.com/mcoops/packageurl-go"
log "github.com/sirupsen/logrus"
)
Expand All @@ -17,6 +18,7 @@ func main() {

if *debugPtr == true {
log.SetLevel(log.DebugLevel)
fmt.Printf("installed ruby versions: %+v", scan.GetInstalledRubyVersions())
}

if flag.Args() == nil || len(flag.Args()) == 0 {
Expand Down
4 changes: 4 additions & 0 deletions deplist.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,7 @@ func GetDeps(fullPath string) ([]Dependency, Bitmask, error) {

return deps, foundTypes, err
}

func GetInstalledRubyVersions() []string {
return scan.GetInstalledRubyVersions()
}
59 changes: 33 additions & 26 deletions internal/scan/ruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,24 @@ import (

var RubyVersions []string = []string{"system"}

func init() {
RubyVersions = append(RubyVersions, getRubyVersions()...)

if len(RubyVersions) == 1 {
log.Debug("rbenv not detected, falling back to system ruby ONLY. Please ensure that bundler is installed and available in your path.")
return
}

log.Debugf("Ruby versions detected: %+v\n", RubyVersions)

for _, version := range RubyVersions {
cmd := exec.Command("gem", "install", "bundler")
setRubyVersion(version, cmd)
data, err := cmd.CombinedOutput()
if err != nil {
log.Debugf("couldn't install bundler: %v", string(data))
}
log.Debugf("Installed bundler for ruby %v\n", version)
}
}

func getRubyVersions() []string {
func GetInstalledRubyVersions() []string {
cmd := exec.Command("rbenv", "versions", "--bare")
data, err := cmd.Output()
if err != nil {
return nil
log.Errorf("error detecting rbenv versions: %v", err)
return RubyVersions
}

versions := strings.Split(string(data), "\n")
versions = versions[:len(versions)-1]
sort.Sort(sort.Reverse(sort.StringSlice(versions)))

RubyVersions = append(RubyVersions, versions...)

if len(RubyVersions) == 1 {
log.Debug("rbenv not detected, falling back to system ruby ONLY. Please ensure that bundler is installed and available in your path.")
}

return versions
}

Expand All @@ -55,18 +41,40 @@ func setRubyVersion(version string, cmd *exec.Cmd) {

// GetRubyDeps calls GetRubyDepsWithVersion with the system ruby version
func GetRubyDeps(path string) (map[string]string, error) {
GetInstalledRubyVersions()

if len(RubyVersions) != 1 {
log.Debugf("Ruby versions detected: %+v\n", RubyVersions)

for _, version := range RubyVersions {
cmd := exec.Command("gem", "install", "bundler")
setRubyVersion(version, cmd)
data, err := cmd.CombinedOutput()
if err != nil {
log.Debugf("couldn't install bundler: %v", string(data))
}
log.Debugf("Installed bundler for ruby %v\n", version)
}
}

return GetRubyDepsWithVersion(path, 0)
}

// GetRubyDepsWithVersion uses `bundle list` to list ruby dependencies when a Gemfile.lock file exists
func GetRubyDepsWithVersion(path string, version int) (map[string]string, error) {
if version != 0 {
log.Debug("retrying...")
if version == -1 {
return nil, errors.New("GetRubyDeps Failed: there's no actual gemfile!")
}

if version >= len(RubyVersions) {
log.Debug("GetRubyDeps Failed! No more ruby versions available")
return nil, errors.New("GetRubyDeps Failed: all ruby versions failed " + path)
}

if version != 0 {
log.Debugf("retrying with %s...", RubyVersions[version])
}

log.Debugf("GetRubyDeps(%v) %s", RubyVersions[version], path)

gathered := make(map[string]string)
Expand Down Expand Up @@ -104,7 +112,6 @@ func GetRubyDepsWithVersion(path string, version int) (map[string]string, error)

data, err = cmd.Output()
if err != nil {

if version == len(RubyVersions) {
log.Debugf("err: %v", err)
log.Debugf("data: %v", string(data))
Expand Down

0 comments on commit ba88cdd

Please sign in to comment.