mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 01:12:56 +03:00
d95d6378e8
Co-Authored-By: Marion Schleifer <marion@hasura.io>
42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
package version
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
yaml "github.com/ghodss/yaml"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type serverVersionResponse struct {
|
|
Version string `json:"version"`
|
|
}
|
|
|
|
// FetchServerVersion reads the version from server.
|
|
func FetchServerVersion(endpoint string, client *http.Client) (version string, err error) {
|
|
response, err := client.Get(endpoint)
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "failed making version api call")
|
|
}
|
|
if response.StatusCode != http.StatusOK {
|
|
switch response.StatusCode {
|
|
case http.StatusNotFound:
|
|
return "", nil
|
|
default:
|
|
return "", errors.Errorf("GET %s failed - [%d]", endpoint, response.StatusCode)
|
|
}
|
|
} else {
|
|
defer response.Body.Close()
|
|
data, err := ioutil.ReadAll(response.Body)
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "cannot read version api response")
|
|
}
|
|
var v serverVersionResponse
|
|
err = yaml.Unmarshal(data, &v)
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "failed to parse version api response")
|
|
}
|
|
return v.Version, nil
|
|
}
|
|
}
|