From 4991d7bbdf35072c42eb4877870770dd73c94062 Mon Sep 17 00:00:00 2001 From: Bruno Calza Date: Thu, 21 Mar 2024 16:13:59 -0300 Subject: [PATCH] list cache duration together with vault's name Signed-off-by: Bruno Calza --- cmd/vaults/commands.go | 19 ++++++++++++++----- internal/app/models.go | 6 ++++++ internal/app/streamer_test.go | 4 ++-- internal/app/vaults_provider.go | 2 +- pkg/vaultsprovider/provider.go | 12 ++++++------ 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/cmd/vaults/commands.go b/cmd/vaults/commands.go index 064ca8d..8f17f37 100644 --- a/cmd/vaults/commands.go +++ b/cmd/vaults/commands.go @@ -378,10 +378,10 @@ func newListCommand() *cli.Command { &cli.StringFlag{ Name: "format", Category: "OPTIONAL:", - Usage: "The output format (text or json)", - DefaultText: "text", + Usage: "The output format (table or json)", + DefaultText: "table", Destination: &format, - Value: "text", + Value: "table", }, }, Action: func(cCtx *cli.Context) error { @@ -396,10 +396,19 @@ func newListCommand() *cli.Command { return fmt.Errorf("failed to list vaults: %s", err) } - if format == "text" { + if format == "table" { + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"Vault", "Cache Duration"}) for _, vault := range vaults { - fmt.Printf("%s\n", vault) + cacheDuration := "" + if vault.CacheDuration != nil { + cacheDuration = fmt.Sprint(*vault.CacheDuration) + } + table.Append([]string{ + string(vault.Vault), cacheDuration, + }) } + table.Render() } else if format == "json" { jsonData, err := json.Marshal(vaults) if err != nil { diff --git a/internal/app/models.go b/internal/app/models.go index 3eed3a5..0b3b6c4 100644 --- a/internal/app/models.go +++ b/internal/app/models.go @@ -9,6 +9,12 @@ import ( // Vault represents a vault. type Vault string +// VaultWithCacheDuration represents a vault with its cache. +type VaultWithCacheDuration struct { + Vault Vault `json:"vault"` + CacheDuration *CacheDuration `json:"cache_duration"` +} + // Account represents an account. type Account struct { address common.Address diff --git a/internal/app/streamer_test.go b/internal/app/streamer_test.go index de1e400..9308059 100644 --- a/internal/app/streamer_test.go +++ b/internal/app/streamer_test.go @@ -230,8 +230,8 @@ func (bp *vaultsProviderMock) CreateVault( return nil } -func (bp *vaultsProviderMock) ListVaults(_ context.Context, _ ListVaultsParams) ([]Vault, error) { - return []Vault{}, nil +func (bp *vaultsProviderMock) ListVaults(_ context.Context, _ ListVaultsParams) ([]VaultWithCacheDuration, error) { + return []VaultWithCacheDuration{}, nil } func (bp *vaultsProviderMock) ListVaultEvents( diff --git a/internal/app/vaults_provider.go b/internal/app/vaults_provider.go index 16d66a5..13f8a99 100644 --- a/internal/app/vaults_provider.go +++ b/internal/app/vaults_provider.go @@ -11,7 +11,7 @@ import ( // VaultsProvider defines Vaults API. type VaultsProvider interface { CreateVault(context.Context, CreateVaultParams) error - ListVaults(context.Context, ListVaultsParams) ([]Vault, error) + ListVaults(context.Context, ListVaultsParams) ([]VaultWithCacheDuration, error) ListVaultEvents(context.Context, ListVaultEventsParams) ([]EventInfo, error) WriteVaultEvent(context.Context, WriteVaultEventParams) error RetrieveEvent(context.Context, RetrieveEventParams, io.Writer) (string, error) diff --git a/pkg/vaultsprovider/provider.go b/pkg/vaultsprovider/provider.go index fff0f38..8c13751 100644 --- a/pkg/vaultsprovider/provider.go +++ b/pkg/vaultsprovider/provider.go @@ -66,24 +66,24 @@ func (bp *VaultsProvider) CreateVault(ctx context.Context, params app.CreateVaul // ListVaults lists all vaults from a given account. func (bp *VaultsProvider) ListVaults( ctx context.Context, params app.ListVaultsParams, -) ([]app.Vault, error) { +) ([]app.VaultWithCacheDuration, error) { req, err := http.NewRequestWithContext( - ctx, http.MethodGet, fmt.Sprintf("%s/vaults/?account=%s", bp.provider, params.Account.Hex()), nil) + ctx, http.MethodGet, fmt.Sprintf("%s/v2/vaults/?account=%s", bp.provider, params.Account.Hex()), nil) if err != nil { - return []app.Vault{}, fmt.Errorf("could not create request: %s", err) + return []app.VaultWithCacheDuration{}, fmt.Errorf("could not create request: %s", err) } resp, err := bp.client.Do(req) if err != nil { - return []app.Vault{}, fmt.Errorf("request to list vaults failed: %s", err) + return []app.VaultWithCacheDuration{}, fmt.Errorf("request to list vaults failed: %s", err) } defer func() { _ = resp.Body.Close() }() - var vaults []app.Vault + var vaults []app.VaultWithCacheDuration if err := json.NewDecoder(resp.Body).Decode(&vaults); err != nil { - return []app.Vault{}, fmt.Errorf("failed to read response: %s", err) + return []app.VaultWithCacheDuration{}, fmt.Errorf("failed to read response: %s", err) } return vaults, nil }