graphql-engine/cli/version/server.go
Mohd Bilal 3760b377b1 cli: refactor version package to use internal/errors
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
2022-10-28 10:00:12 +00:00

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
}