Skip to content

Commit a611769

Browse files
committed
Factor out installDependencies()
1 parent d61d595 commit a611769

File tree

1 file changed

+52
-48
lines changed

1 file changed

+52
-48
lines changed

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,57 @@ func buildWithCustomCommands(inst string) {
506506
util.RunCmd(exec.Command(script.Name()))
507507
}
508508

509+
func installDependencies(depMode DependencyInstallerMode) {
510+
// automatically determine command to install dependencies
511+
var install *exec.Cmd
512+
if depMode == Dep {
513+
// set up the dep cache if SEMMLE_CACHE is set
514+
cacheDir := os.Getenv("SEMMLE_CACHE")
515+
if cacheDir != "" {
516+
depCacheDir := filepath.Join(cacheDir, "go", "dep")
517+
log.Printf("Attempting to create dep cache dir %s\n", depCacheDir)
518+
err := os.MkdirAll(depCacheDir, 0755)
519+
if err != nil {
520+
log.Printf("Failed to create dep cache directory: %s\n", err.Error())
521+
} else {
522+
log.Printf("Setting dep cache directory to %s\n", depCacheDir)
523+
err = os.Setenv("DEPCACHEDIR", depCacheDir)
524+
if err != nil {
525+
log.Println("Failed to set dep cache directory")
526+
} else {
527+
err = os.Setenv("DEPCACHEAGE", "720h") // 30 days
528+
if err != nil {
529+
log.Println("Failed to set dep cache age")
530+
}
531+
}
532+
}
533+
}
534+
535+
if util.FileExists("Gopkg.lock") {
536+
// if Gopkg.lock exists, don't update it and only vendor dependencies
537+
install = exec.Command("dep", "ensure", "-v", "-vendor-only")
538+
} else {
539+
install = exec.Command("dep", "ensure", "-v")
540+
}
541+
log.Println("Installing dependencies using `dep ensure`.")
542+
} else if depMode == Glide {
543+
install = exec.Command("glide", "install")
544+
log.Println("Installing dependencies using `glide install`")
545+
} else {
546+
// explicitly set go module support
547+
if depMode == GoGetWithModules {
548+
os.Setenv("GO111MODULE", "on")
549+
} else if depMode == GoGetNoModules {
550+
os.Setenv("GO111MODULE", "off")
551+
}
552+
553+
// get dependencies
554+
install = exec.Command("go", "get", "-v", "./...")
555+
log.Println("Installing dependencies using `go get -v ./...`.")
556+
}
557+
util.RunCmd(install)
558+
}
559+
509560
func main() {
510561
if len(os.Args) > 1 {
511562
usage()
@@ -582,54 +633,7 @@ func main() {
582633
if modMode == ModVendor {
583634
log.Printf("Skipping dependency installation because a Go vendor directory was found.")
584635
} else {
585-
// automatically determine command to install dependencies
586-
var install *exec.Cmd
587-
if depMode == Dep {
588-
// set up the dep cache if SEMMLE_CACHE is set
589-
cacheDir := os.Getenv("SEMMLE_CACHE")
590-
if cacheDir != "" {
591-
depCacheDir := filepath.Join(cacheDir, "go", "dep")
592-
log.Printf("Attempting to create dep cache dir %s\n", depCacheDir)
593-
err := os.MkdirAll(depCacheDir, 0755)
594-
if err != nil {
595-
log.Printf("Failed to create dep cache directory: %s\n", err.Error())
596-
} else {
597-
log.Printf("Setting dep cache directory to %s\n", depCacheDir)
598-
err = os.Setenv("DEPCACHEDIR", depCacheDir)
599-
if err != nil {
600-
log.Println("Failed to set dep cache directory")
601-
} else {
602-
err = os.Setenv("DEPCACHEAGE", "720h") // 30 days
603-
if err != nil {
604-
log.Println("Failed to set dep cache age")
605-
}
606-
}
607-
}
608-
}
609-
610-
if util.FileExists("Gopkg.lock") {
611-
// if Gopkg.lock exists, don't update it and only vendor dependencies
612-
install = exec.Command("dep", "ensure", "-v", "-vendor-only")
613-
} else {
614-
install = exec.Command("dep", "ensure", "-v")
615-
}
616-
log.Println("Installing dependencies using `dep ensure`.")
617-
} else if depMode == Glide {
618-
install = exec.Command("glide", "install")
619-
log.Println("Installing dependencies using `glide install`")
620-
} else {
621-
// explicitly set go module support
622-
if depMode == GoGetWithModules {
623-
os.Setenv("GO111MODULE", "on")
624-
} else if depMode == GoGetNoModules {
625-
os.Setenv("GO111MODULE", "off")
626-
}
627-
628-
// get dependencies
629-
install = exec.Command("go", "get", "-v", "./...")
630-
log.Println("Installing dependencies using `go get -v ./...`.")
631-
}
632-
util.RunCmd(install)
636+
installDependencies(depMode)
633637
}
634638
}
635639

0 commit comments

Comments
 (0)