From ee8faaff3ba0087efc82039fa7fe4ff82e7c69ff Mon Sep 17 00:00:00 2001 From: steebchen Date: Sun, 18 Jun 2023 17:43:50 +0100 Subject: [PATCH] refactor binaries, fix static --- binaries/binaries.go | 45 +++++++------------------------------------- cli/cli.go | 2 +- 2 files changed, 8 insertions(+), 39 deletions(-) diff --git a/binaries/binaries.go b/binaries/binaries.go index 1f67ce4ec..9c3516435 100644 --- a/binaries/binaries.go +++ b/binaries/binaries.go @@ -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" @@ -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" } @@ -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) @@ -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, e.Name); err != nil { return fmt.Errorf("could not download engines: %w", err) } } @@ -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 { diff --git a/cli/cli.go b/cli/cli.go index a156faf8b..e40d82e52 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -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")