github.com/supabase/cli@v1.168.1/internal/projects/apiKeys/api_keys.go (about)

     1  package apiKeys
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/go-errors/errors"
     9  	"github.com/spf13/afero"
    10  	"github.com/supabase/cli/internal/migration/list"
    11  	"github.com/supabase/cli/internal/utils"
    12  	"github.com/supabase/cli/pkg/api"
    13  )
    14  
    15  func Run(ctx context.Context, projectRef string, fsys afero.Fs) error {
    16  	keys, err := RunGetApiKeys(ctx, projectRef)
    17  	if err != nil {
    18  		return err
    19  	}
    20  
    21  	table := `|NAME|KEY VALUE|
    22  |-|-|
    23  `
    24  	for _, entry := range keys {
    25  		table += fmt.Sprintf("|`%s`|`%s`|\n", strings.ReplaceAll(entry.Name, "|", "\\|"), entry.ApiKey)
    26  	}
    27  
    28  	return list.RenderTable(table)
    29  }
    30  
    31  func RunGetApiKeys(ctx context.Context, projectRef string) ([]api.ApiKeyResponse, error) {
    32  	resp, err := utils.GetSupabase().GetProjectApiKeysWithResponse(ctx, projectRef)
    33  	if err != nil {
    34  		return nil, errors.Errorf("failed to get api keys: %w", err)
    35  	}
    36  	if resp.JSON200 == nil {
    37  		return nil, errors.New("Unexpected error retrieving project api-keys: " + string(resp.Body))
    38  	}
    39  	return *resp.JSON200, nil
    40  }