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
This commit is contained in:
Mohd Bilal 2022-10-28 15:28:55 +05:30 committed by hasura-bot
parent e814260ccc
commit 3760b377b1
2 changed files with 13 additions and 7 deletions

View File

@ -1,8 +1,11 @@
package version package version
import ( import (
"fmt"
"github.com/Masterminds/semver" "github.com/Masterminds/semver"
"github.com/pkg/errors"
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
) )
// ServerFeatureFlags indicates what features are supported by this // ServerFeatureFlags indicates what features are supported by this
@ -20,6 +23,7 @@ const cronTriggersVersion = "v1.3.0-beta.1"
// GetServerFeatureFlags returns the feature flags for server. // GetServerFeatureFlags returns the feature flags for server.
func (v *Version) GetServerFeatureFlags() error { func (v *Version) GetServerFeatureFlags() error {
var op errors.Op = "version.GetServerFeatureFlags"
flags := &ServerFeatureFlags{} flags := &ServerFeatureFlags{}
if v.ServerSemver == nil { if v.ServerSemver == nil {
flags.HasAccessKey = false flags.HasAccessKey = false
@ -28,7 +32,7 @@ func (v *Version) GetServerFeatureFlags() error {
// create a constraint to check if the current server version has admin secret // create a constraint to check if the current server version has admin secret
adminSecretConstraint, err := semver.NewConstraint("< " + adminSecretVersion) adminSecretConstraint, err := semver.NewConstraint("< " + adminSecretVersion)
if err != nil { if err != nil {
return errors.Wrap(err, "building admin secret constraint failed") return errors.E(op, fmt.Errorf("building admin secret constraint failed: %w", err))
} }
// check the current version with the constraint // check the current version with the constraint
flags.HasAccessKey = adminSecretConstraint.Check(v.ServerSemver) flags.HasAccessKey = adminSecretConstraint.Check(v.ServerSemver)
@ -36,7 +40,7 @@ func (v *Version) GetServerFeatureFlags() error {
// create a constraint to check if the current server version has actions // create a constraint to check if the current server version has actions
actionConstraint, err := semver.NewConstraint(">= " + actionVersion) actionConstraint, err := semver.NewConstraint(">= " + actionVersion)
if err != nil { if err != nil {
return errors.Wrap(err, "building action constraint failed") return errors.E(op, fmt.Errorf("building action constraint failed: %w", err))
} }
// check the current version with the constraint // check the current version with the constraint
flags.HasAction = actionConstraint.Check(v.ServerSemver) flags.HasAction = actionConstraint.Check(v.ServerSemver)
@ -44,7 +48,7 @@ func (v *Version) GetServerFeatureFlags() error {
// cronTriggers Constraint // cronTriggers Constraint
cronTriggersConstraint, err := semver.NewConstraint(">= " + cronTriggersVersion) cronTriggersConstraint, err := semver.NewConstraint(">= " + cronTriggersVersion)
if err != nil { if err != nil {
return errors.Wrap(err, "building cron triggers constraint failed") return errors.E(op, fmt.Errorf("building cron triggers constraint failed: %w", err))
} }
// check the current version with the constraint // check the current version with the constraint
flags.HasCronTriggers = cronTriggersConstraint.Check(v.ServerSemver) flags.HasCronTriggers = cronTriggersConstraint.Check(v.ServerSemver)

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
"github.com/hasura/graphql-engine/cli/v2/internal/httpc" "github.com/hasura/graphql-engine/cli/v2/internal/httpc"
) )
@ -14,21 +15,22 @@ type serverVersionResponse struct {
// FetchServerVersion reads the version from server. // FetchServerVersion reads the version from server.
func FetchServerVersion(endpoint string, client *httpc.Client) (version string, err error) { func FetchServerVersion(endpoint string, client *httpc.Client) (version string, err error) {
var op errors.Op = "version.FetchServerVersion"
req, err := http.NewRequest("GET", endpoint, nil) req, err := http.NewRequest("GET", endpoint, nil)
if err != nil { if err != nil {
return "", fmt.Errorf("error while making http request to %s", endpoint) return "", errors.E(op, fmt.Errorf("error while making http request to %s", endpoint))
} }
var v serverVersionResponse var v serverVersionResponse
response, err := client.Do(context.Background(), req, &v) response, err := client.Do(context.Background(), req, &v)
if err != nil { if err != nil {
return "", fmt.Errorf("failed making version api call: %w", err) return "", errors.E(op, fmt.Errorf("failed making version api call: %w", err))
} }
if response.StatusCode != http.StatusOK { if response.StatusCode != http.StatusOK {
switch response.StatusCode { switch response.StatusCode {
case http.StatusNotFound: case http.StatusNotFound:
return "", nil return "", nil
default: default:
return "", fmt.Errorf("GET %s failed - [%d]", endpoint, response.StatusCode) return "", errors.E(op, errors.KindHasuraAPI, fmt.Errorf("GET %s failed - [%d]", endpoint, response.StatusCode))
} }
} }
return v.Version, nil return v.Version, nil