mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
101 lines
2.3 KiB
Go
101 lines
2.3 KiB
Go
package ledger
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
fctl "github.com/formancehq/fctl/pkg"
|
|
"github.com/pterm/pterm"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type ServerInfoStore struct {
|
|
Server string `json:"server"`
|
|
Version string `json:"version"`
|
|
}
|
|
type ServerInfoController struct {
|
|
store *ServerInfoStore
|
|
}
|
|
|
|
var _ fctl.Controller[*ServerInfoStore] = (*ServerInfoController)(nil)
|
|
|
|
func NewDefaultServerInfoStore() *ServerInfoStore {
|
|
return &ServerInfoStore{
|
|
Server: "unknown",
|
|
Version: "unknown",
|
|
}
|
|
}
|
|
|
|
func NewServerInfoController() *ServerInfoController {
|
|
return &ServerInfoController{
|
|
store: NewDefaultServerInfoStore(),
|
|
}
|
|
}
|
|
|
|
func NewServerInfoCommand() *cobra.Command {
|
|
return fctl.NewCommand("server-infos",
|
|
fctl.WithArgs(cobra.ExactArgs(0)),
|
|
fctl.WithAliases("si"),
|
|
fctl.WithShortDescription("Read server info"),
|
|
fctl.WithController[*ServerInfoStore](NewServerInfoController()),
|
|
)
|
|
}
|
|
|
|
func (c *ServerInfoController) GetStore() *ServerInfoStore {
|
|
return c.store
|
|
}
|
|
|
|
func (c *ServerInfoController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
|
|
cfg, err := fctl.GetConfig(cmd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
organizationID, err := fctl.ResolveOrganizationID(cmd, cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
stack, err := fctl.ResolveStack(cmd, cfg, organizationID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ledgerClient, err := fctl.NewStackClient(cmd, cfg, stack)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response, err := ledgerClient.Ledger.GetInfo(cmd.Context())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if response.ErrorResponse != nil {
|
|
return nil, fmt.Errorf("%s: %s", response.ErrorResponse.ErrorCode, response.ErrorResponse.ErrorMessage)
|
|
}
|
|
|
|
if response.StatusCode >= 300 {
|
|
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
|
|
}
|
|
|
|
c.store.Server = response.ConfigInfoResponse.Data.Server
|
|
c.store.Version = response.ConfigInfoResponse.Data.Version
|
|
|
|
return c, nil
|
|
}
|
|
|
|
func (c *ServerInfoController) Render(cmd *cobra.Command, args []string) error {
|
|
tableData := pterm.TableData{}
|
|
tableData = append(tableData, []string{pterm.LightCyan("Server"), fmt.Sprint(c.store.Server)})
|
|
tableData = append(tableData, []string{pterm.LightCyan("Version"), fmt.Sprint(c.store.Version)})
|
|
|
|
if err := pterm.DefaultTable.
|
|
WithWriter(cmd.OutOrStdout()).
|
|
WithData(tableData).
|
|
Render(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|