Skip to content

Commit

Permalink
Merge pull request #41 from tablelandnetwork/bcalza/listcacheduratio
Browse files Browse the repository at this point in the history
list cache duration together with vault's name
  • Loading branch information
brunocalza authored Mar 26, 2024
2 parents ad95dcb + 4991d7b commit 6df00e0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
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)
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

0 comments on commit 6df00e0

Please sign in to comment.