Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list cache duration together with vault's name #41

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions cmd/vaults/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions internal/app/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/app/streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion internal/app/vaults_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions pkg/vaultsprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
}
Expand Down
Loading