github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/internal/projects/list/list.go (about)

     1  package list
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/Redstoneguy129/cli/internal/utils"
    11  	"github.com/charmbracelet/glamour"
    12  	"github.com/spf13/afero"
    13  )
    14  
    15  func Run(ctx context.Context, fsys afero.Fs) error {
    16  	resp, err := utils.GetSupabase().GetProjectsWithResponse(ctx)
    17  	if err != nil {
    18  		return err
    19  	}
    20  
    21  	if resp.JSON200 == nil {
    22  		return errors.New("Unexpected error retrieving projects: " + string(resp.Body))
    23  	}
    24  
    25  	table := `|ORG ID|ID|NAME|REGION|CREATED AT (UTC)|
    26  |-|-|-|-|-|
    27  `
    28  	for _, project := range *resp.JSON200 {
    29  		if t, err := time.Parse(time.RFC3339, project.CreatedAt); err == nil {
    30  			project.CreatedAt = t.UTC().Format("2006-01-02 15:04:05")
    31  		}
    32  		if region, ok := utils.RegionMap[project.Region]; ok {
    33  			project.Region = region
    34  		}
    35  		table += fmt.Sprintf(
    36  			"|`%s`|`%s`|`%s`|`%s`|`%s`|\n",
    37  			project.OrganizationId,
    38  			project.Id,
    39  			strings.ReplaceAll(project.Name, "|", "\\|"),
    40  			project.Region,
    41  			project.CreatedAt,
    42  		)
    43  	}
    44  
    45  	r, err := glamour.NewTermRenderer(
    46  		glamour.WithAutoStyle(),
    47  		glamour.WithWordWrap(-1),
    48  	)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	out, err := r.Render(table)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	fmt.Print(out)
    57  
    58  	return nil
    59  }