github.com/supabase/cli@v1.168.1/internal/utils/tenant/postgrest.go (about)

     1  package tenant
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"github.com/go-errors/errors"
     9  	"github.com/supabase/cli/pkg/fetcher"
    10  )
    11  
    12  var errPostgrestVersion = errors.New("PostgREST version not found.")
    13  
    14  type SwaggerInfo struct {
    15  	Title       string `json:"title"`
    16  	Description string `json:"description"`
    17  	Version     string `json:"version"`
    18  }
    19  
    20  type SwaggerResponse struct {
    21  	Swagger string      `json:"swagger"`
    22  	Info    SwaggerInfo `json:"info"`
    23  }
    24  
    25  func (t *TenantAPI) GetPostgrestVersion(ctx context.Context) (string, error) {
    26  	resp, err := t.Send(ctx, http.MethodGet, "/rest/v1/", nil)
    27  	if err != nil {
    28  		return "", err
    29  	}
    30  	defer resp.Body.Close()
    31  	data, err := fetcher.ParseJSON[SwaggerResponse](resp.Body)
    32  	if err != nil {
    33  		return "", err
    34  	}
    35  	if len(data.Info.Version) == 0 {
    36  		return "", errors.New(errPostgrestVersion)
    37  	}
    38  	parts := strings.Split(data.Info.Version, " ")
    39  	return "v" + parts[0], nil
    40  }