Files
stack/components/fctl/cmd/profiles/show.go
RAGOT DavidandGitHub 0df34a4d94 feat: fctl output=<json> (#363)
+ Add controller interface
+ Add renderable interface

- A controller define his store. And Running cycle returning a renderable interface
- Json is handle between Run & Render is the proper flag is set
2023-07-07 09:36:12 +02:00

75 lines
1.9 KiB
Go

package profiles
import (
fctl "github.com/formancehq/fctl/pkg"
"github.com/pkg/errors"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
type ProfilesShowStore struct {
MembershipURI string `json:"membershipUri"`
DefaultOrganization string `json:"defaultOrganization"`
}
type ProfilesShowController struct {
store *ProfilesShowStore
}
var _ fctl.Controller[*ProfilesShowStore] = (*ProfilesShowController)(nil)
func NewDefaultProfilesShowStore() *ProfilesShowStore {
return &ProfilesShowStore{
MembershipURI: "",
DefaultOrganization: "",
}
}
func NewProfilesShowController() *ProfilesShowController {
return &ProfilesShowController{
store: NewDefaultProfilesShowStore(),
}
}
func (c *ProfilesShowController) GetStore() *ProfilesShowStore {
return c.store
}
func (c *ProfilesShowController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
config, err := fctl.GetConfig(cmd)
if err != nil {
return nil, err
}
p := config.GetProfile(args[0])
if p == nil {
return nil, errors.New("not found")
}
c.store.DefaultOrganization = p.GetDefaultOrganization()
c.store.MembershipURI = p.GetMembershipURI()
return c, nil
}
func (c *ProfilesShowController) Render(cmd *cobra.Command, args []string) error {
tableData := pterm.TableData{}
tableData = append(tableData, []string{pterm.LightCyan("Membership URI"), c.store.MembershipURI})
tableData = append(tableData, []string{pterm.LightCyan("Default organization"), c.store.DefaultOrganization})
return pterm.DefaultTable.
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Render()
}
func NewShowCommand() *cobra.Command {
return fctl.NewCommand("show <name>",
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithAliases("s"),
fctl.WithShortDescription("Show profile"),
fctl.WithValidArgsFunction(ProfileNamesAutoCompletion),
fctl.WithController[*ProfilesShowStore](NewProfilesShowController()),
)
}