2018-07-04 15:43:52 +03:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
2022-02-04 14:10:33 +03:00
|
|
|
"context"
|
2021-09-27 17:52:46 +03:00
|
|
|
"fmt"
|
2018-07-04 15:43:52 +03:00
|
|
|
"net/http"
|
2022-02-04 14:10:33 +03:00
|
|
|
|
2022-10-28 12:58:55 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
|
2022-02-04 14:10:33 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/httpc"
|
2018-07-04 15:43:52 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type serverVersionResponse struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchServerVersion reads the version from server.
|
2022-02-04 14:10:33 +03:00
|
|
|
func FetchServerVersion(endpoint string, client *httpc.Client) (version string, err error) {
|
2022-10-28 12:58:55 +03:00
|
|
|
var op errors.Op = "version.FetchServerVersion"
|
2022-02-04 14:10:33 +03:00
|
|
|
req, err := http.NewRequest("GET", endpoint, nil)
|
|
|
|
if err != nil {
|
2022-10-28 12:58:55 +03:00
|
|
|
return "", errors.E(op, fmt.Errorf("error while making http request to %s", endpoint))
|
2022-02-04 14:10:33 +03:00
|
|
|
}
|
|
|
|
var v serverVersionResponse
|
|
|
|
response, err := client.Do(context.Background(), req, &v)
|
2018-07-04 15:43:52 +03:00
|
|
|
if err != nil {
|
2022-10-28 12:58:55 +03:00
|
|
|
return "", errors.E(op, fmt.Errorf("failed making version api call: %w", err))
|
2018-07-04 15:43:52 +03:00
|
|
|
}
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
switch response.StatusCode {
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return "", nil
|
|
|
|
default:
|
2022-10-28 12:58:55 +03:00
|
|
|
return "", errors.E(op, errors.KindHasuraAPI, fmt.Errorf("GET %s failed - [%d]", endpoint, response.StatusCode))
|
2018-07-04 15:43:52 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-04 14:10:33 +03:00
|
|
|
return v.Version, nil
|
2018-07-04 15:43:52 +03:00
|
|
|
}
|