mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 01:12:56 +03:00
2329c16ce9
https://github.com/hasura/graphql-engine-mono/pull/2295 Co-authored-by: Kali Vara Purushotham Santhati <72007599+purush7@users.noreply.github.com> GitOrigin-RevId: 16a03d33d5bf4d45a71ba79b3b775fba3176a1e8
85 lines
2.5 KiB
Go
85 lines
2.5 KiB
Go
package cliext
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func getCliExtFileContent(ec *cli.ExecutionContext) ([]byte, error) {
|
|
if ec.CliExtSourceBinPath != "" {
|
|
ec.Logger.Debug("cli-ext: setting up using --cli-ext-path binary")
|
|
return ioutil.ReadFile(ec.CliExtSourceBinPath)
|
|
}
|
|
|
|
var cliExtBinName string = "cli-ext"
|
|
if runtime.GOOS == "windows" {
|
|
cliExtBinName = "cli-ext.exe"
|
|
}
|
|
cliExtFileContent, err := cliExtFS.ReadFile(filepath.ToSlash(filepath.Join("static-bin", runtime.GOOS, runtime.GOARCH, cliExtBinName)))
|
|
if err == nil {
|
|
ec.Logger.Debug("cli-ext: setting up using cli-ext embedded in cli binary")
|
|
return cliExtFileContent, nil
|
|
}
|
|
|
|
cliExtVersion := "v2.0.0"
|
|
ec.Logger.Warn("Unable to find an embedded cli-ext. So trying to fetch it from CDN")
|
|
ec.Logger.Warn("Tip: --cli-ext-path can be used for setting up cli-ext from local file system")
|
|
CDNpath := fmt.Sprintf("https://graphql-engine-cdn.hasura.io/cli-ext/releases/versioned/%s/cli-ext-%s-%s",
|
|
cliExtVersion,
|
|
runtime.GOOS,
|
|
runtime.GOARCH,
|
|
)
|
|
resp, err := http.Get(CDNpath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("error downloading cli-ext from CDN %s with status code: %d", CDNpath, resp.StatusCode)
|
|
}
|
|
return ioutil.ReadAll(bufio.NewReader(resp.Body))
|
|
}
|
|
|
|
// Setup sets up cli-ext binary for using it in various cli commands
|
|
func Setup(ec *cli.ExecutionContext) error {
|
|
parentDirPath := filepath.Join(ec.GlobalConfigDir, "cli-ext", ec.Version.GetCLIVersion())
|
|
err := os.MkdirAll(parentDirPath, 0755)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "error creating base directory while setting up cli-ext")
|
|
}
|
|
cliExtDirPath, err := ioutil.TempDir(parentDirPath, "cli-ext-*")
|
|
if err != nil {
|
|
return errors.Wrapf(err, "error creating directory while setting up cli-ext")
|
|
}
|
|
ec.CliExtDestinationDir = cliExtDirPath
|
|
|
|
cliExtBinName := "cli-ext"
|
|
if runtime.GOOS == "windows" {
|
|
cliExtBinName = "cli-ext.exe"
|
|
}
|
|
cliExtBinPath := filepath.Join(cliExtDirPath, cliExtBinName)
|
|
cliExtFileContent, err := getCliExtFileContent(ec)
|
|
if err != nil {
|
|
return fmt.Errorf("error reading cli-ext binary: %w", err)
|
|
}
|
|
err = ioutil.WriteFile(cliExtBinPath, cliExtFileContent, 0755)
|
|
if err != nil {
|
|
return errors.Wrap(err, "error unpacking binary while setting up cli-ext")
|
|
}
|
|
ec.CliExtDestinationBinPath = cliExtBinPath
|
|
|
|
return nil
|
|
}
|
|
|
|
func Cleanup(ec *cli.ExecutionContext) {
|
|
_ = os.RemoveAll(ec.CliExtDestinationDir)
|
|
}
|