mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 01:12:56 +03:00
3760b377b1
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6509 Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com> GitOrigin-RevId: 56c534327883de52ee93eeb4a83c9329fde6ec94
38 lines
1.0 KiB
Go
38 lines
1.0 KiB
Go
package version
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/httpc"
|
|
)
|
|
|
|
type serverVersionResponse struct {
|
|
Version string `json:"version"`
|
|
}
|
|
|
|
// FetchServerVersion reads the version from server.
|
|
func FetchServerVersion(endpoint string, client *httpc.Client) (version string, err error) {
|
|
var op errors.Op = "version.FetchServerVersion"
|
|
req, err := http.NewRequest("GET", endpoint, nil)
|
|
if err != nil {
|
|
return "", errors.E(op, fmt.Errorf("error while making http request to %s", endpoint))
|
|
}
|
|
var v serverVersionResponse
|
|
response, err := client.Do(context.Background(), req, &v)
|
|
if err != nil {
|
|
return "", errors.E(op, fmt.Errorf("failed making version api call: %w", err))
|
|
}
|
|
if response.StatusCode != http.StatusOK {
|
|
switch response.StatusCode {
|
|
case http.StatusNotFound:
|
|
return "", nil
|
|
default:
|
|
return "", errors.E(op, errors.KindHasuraAPI, fmt.Errorf("GET %s failed - [%d]", endpoint, response.StatusCode))
|
|
}
|
|
}
|
|
return v.Version, nil
|
|
}
|