github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/runners/organizations/organizations.go (about)

     1  package organizations
     2  
     3  import (
     4  	"github.com/ActiveState/cli/internal/locale"
     5  	"github.com/ActiveState/cli/internal/output"
     6  	"github.com/ActiveState/cli/internal/primer"
     7  	"github.com/ActiveState/cli/pkg/platform/authentication"
     8  	"github.com/ActiveState/cli/pkg/platform/model"
     9  )
    10  
    11  type Organizations struct {
    12  	out  output.Outputer
    13  	auth *authentication.Auth
    14  }
    15  
    16  type primeable interface {
    17  	primer.Outputer
    18  	primer.Auther
    19  }
    20  
    21  func NewOrganizations(prime primeable) *Organizations {
    22  	return &Organizations{prime.Output(), prime.Auth()}
    23  }
    24  
    25  type OrgParams struct {
    26  }
    27  
    28  type orgOutput struct {
    29  	Name            string `json:"name,omitempty"`
    30  	URLName         string `json:"URLName,omitempty" opts:"hidePlain"`
    31  	Tier            string `json:"tier,omitempty" locale:"tier,Tier"`
    32  	PrivateProjects bool   `json:"privateProjects" locale:"privateprojects,Private Projects"`
    33  }
    34  
    35  func (o *Organizations) Run(params *OrgParams) error {
    36  	modelOrgs, err := model.FetchOrganizations(o.auth)
    37  	if err != nil {
    38  		return locale.WrapError(err, "organizations_err")
    39  	}
    40  
    41  	tiers, err := model.FetchTiers(o.auth)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	type tierInfo struct {
    47  		private bool
    48  		title   string
    49  	}
    50  	tiersLookup := make(map[string]tierInfo)
    51  	for _, t := range tiers {
    52  		tiersLookup[t.Name] = tierInfo{
    53  			private: t.RequiresPayment,
    54  			title:   t.Description,
    55  		}
    56  	}
    57  
    58  	orgs := make([]orgOutput, len(modelOrgs))
    59  	for i, org := range modelOrgs {
    60  		var tierPrivate bool
    61  		tierTitle := "Unknown"
    62  		t, ok := tiersLookup[org.Tier]
    63  		if ok {
    64  			tierPrivate = t.private
    65  			tierTitle = t.title
    66  		}
    67  		orgs[i] = orgOutput{
    68  			Name:            org.DisplayName,
    69  			URLName:         org.URLname,
    70  			Tier:            tierTitle,
    71  			PrivateProjects: tierPrivate,
    72  		}
    73  	}
    74  
    75  	var plainOutput interface{} = orgs
    76  	if len(orgs) == 0 {
    77  		plainOutput = locale.T("organization_no_orgs")
    78  	}
    79  	o.out.Print(output.Prepare(plainOutput, orgs))
    80  	return nil
    81  }