Skip to content

Commit

Permalink
refactor binaries, fix static
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Jun 18, 2023
1 parent 01a60e6 commit c45715f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 39 deletions.
45 changes: 7 additions & 38 deletions binaries/binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"path"
"path/filepath"
"time"

"github.com/steebchen/prisma-client-go/binaries/platform"
"github.com/steebchen/prisma-client-go/logger"
Expand Down Expand Up @@ -86,12 +85,12 @@ func GlobalCacheDir() string {
return path.Join(cache, baseDirName, "cli", PrismaVersion)
}

func FetchEngine(toDir string, engineName string, binaryPlatformName string) error {
func FetchEngine(dir string, engineName string, binaryName string) error {
logger.Debug.Printf("checking %s...", engineName)

to := platform.CheckForExtension(binaryPlatformName, path.Join(toDir, EngineVersion, fmt.Sprintf("prisma-%s-%s", engineName, binaryPlatformName)))
to := GetEnginePath(dir, engineName, binaryName)

binaryPlatformRemoteName := binaryPlatformName
binaryPlatformRemoteName := binaryName
if binaryPlatformRemoteName == "linux" {
binaryPlatformRemoteName = "linux-static-x64"
}
Expand All @@ -101,7 +100,7 @@ func FetchEngine(toDir string, engineName string, binaryPlatformName string) err
return nil
}

url := platform.CheckForExtension(binaryPlatformName, fmt.Sprintf(EngineURL, EngineVersion, binaryPlatformRemoteName, engineName))
url := platform.CheckForExtension(binaryName, fmt.Sprintf(EngineURL, EngineVersion, binaryPlatformRemoteName, engineName))

logger.Debug.Printf("%s is missing, downloading...", engineName)

Expand Down Expand Up @@ -131,7 +130,7 @@ func FetchNative(toDir string) error {
}

for _, e := range Engines {
if _, err := DownloadEngine(e.Name, toDir); err != nil {
if err := FetchEngine(toDir, e.Name, platform.BinaryPlatformNameStatic()); err != nil {
return fmt.Errorf("could not download engines: %w", err)
}
}
Expand Down Expand Up @@ -161,38 +160,8 @@ func DownloadCLI(toDir string) error {
return nil
}

func GetEnginePath(dir, engine, binaryName string) string {
return platform.CheckForExtension(binaryName, path.Join(dir, EngineVersion, fmt.Sprintf("prisma-%s-%s", engine, binaryName)))
}

func DownloadEngine(name string, toDir string) (file string, err error) {
binaryName := platform.BinaryPlatformNameDynamic()

logger.Debug.Printf("checking %s...", name)

to := platform.CheckForExtension(binaryName, path.Join(toDir, EngineVersion, fmt.Sprintf("prisma-%s-%s", name, binaryName)))

url := platform.CheckForExtension(binaryName, fmt.Sprintf(EngineURL, EngineVersion, binaryName, name))

logger.Debug.Printf("download url %s", url)

if _, err := os.Stat(to); !os.IsNotExist(err) {
logger.Debug.Printf("%s is cached", to)
return to, nil
}

logger.Debug.Printf("%s is missing, downloading...", name)

startDownload := time.Now()
if err := download(url, to); err != nil {
return "", fmt.Errorf("could not download %s to %s: %w", url, to, err)
}

logger.Debug.Printf("%s engine download took %s", name, time.Since(startDownload))

logger.Debug.Printf("%s done", name)

return to, nil
func GetEnginePath(dir, engineName, binaryName string) string {
return platform.CheckForExtension(binaryName, path.Join(dir, EngineVersion, fmt.Sprintf("prisma-%s-%s", engineName, binaryName)))
}

func download(url string, to string) error {
Expand Down
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Run(arguments []string, output bool) error {
logger.Debug.Printf("running %s %+v", path.Join(dir, prisma), arguments)

cmd := exec.Command(path.Join(dir, prisma), arguments...) //nolint:gosec
binaryName := platform.CheckForExtension(platform.Name(), platform.BinaryPlatformNameDynamic())
binaryName := platform.CheckForExtension(platform.Name(), platform.BinaryPlatformNameStatic())

cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "PRISMA_HIDE_UPDATE_MESSAGE=true")
Expand Down

0 comments on commit c45715f

Please sign in to comment.