Skip to content

Commit

Permalink
Handle relatives with ruby scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
sfowl committed Oct 19, 2023
1 parent 86dec07 commit dde0f7f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/scan/gemlock-parser.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'bundler'

# Read the Gemfile.lock
lockfile = Bundler.read_file('Gemfile.lock')
lockfile = Bundler.read_file(ARGV[0])

# Create a new LockfileParser object
parser = Bundler::LockfileParser.new(lockfile)
Expand Down
21 changes: 13 additions & 8 deletions internal/scan/ruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"embed"
"os"
"os/exec"
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"
Expand All @@ -19,27 +20,29 @@ var rubyScript []byte

func GetRubyDeps(path string) (map[string]string, error) {
log.Debugf("GetRubyDeps %s", path)
baseDir := filepath.Dir(path)
lockPath := filepath.Join(baseDir, "Gemfile.lock")

if _, err := os.Stat("Gemfile.lock"); err != nil {
if _, err := os.Stat(lockPath); err != nil {
if os.IsNotExist(err) {
log.Debugf("Creating Gemfile.lock with `bundle lock`")
log.Debugf("Creating %s with `bundle lock`", lockPath)
// Create Gemfile.lock
cmd := exec.Command("bundle", "lock")
data, err := cmd.CombinedOutput()
if err != nil {
log.Errorf("couldn't create Gemfile.lock: %v: %v", err, string(data))
log.Errorf("couldn't create %s: %v: %v", lockPath, err, string(data))
return nil, err
}
log.Debugf("Created Gemfile.lock")
log.Debugf("Created %s", lockPath)
} else {
log.Errorf("Unexpected error: %v", err)
return nil, err
}
}
return runGemlockParser()
return runGemlockParser(lockPath)
}

func runGemlockParser() (map[string]string, error) {
func runGemlockParser(lockPath string) (map[string]string, error) {
gathered := make(map[string]string)

g, err := os.CreateTemp("", scriptName)
Expand All @@ -52,10 +55,12 @@ func runGemlockParser() (map[string]string, error) {
log.Errorf("Could not write ruby script to %s: %s", g.Name(), err)
return gathered, err
}
cmd := exec.Command("ruby", g.Name())
args := []string{g.Name(), lockPath}
log.Debugf("Running ruby %v", args)
cmd := exec.Command("ruby", args...)
data, err := cmd.Output()
if err != nil {
log.Errorf("Error running Gemfile.lock parser: %v", err)
log.Errorf("Error running Gemfile.lock parser: %v: %s", err, string(data))
return gathered, err
}

Expand Down

0 comments on commit dde0f7f

Please sign in to comment.