graphql-engine/cli/version/server.go
Shraddha Agrawal d95d6378e8
cli: add support for servers with self-signed certs (fix #4564) (#4582)
Co-Authored-By: Marion Schleifer <marion@hasura.io>
2020-04-28 17:29:57 +05:30

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
}
}