Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
5 changes: 3 additions & 2 deletions pkg/commands/secretstore/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package secretstore_test
import (
"bytes"

"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v8/fastly"

"github.com/fastly/cli/pkg/text"
)

func fmtStore(s *fastly.SecretStore) string {
Expand All @@ -13,7 +14,7 @@ func fmtStore(s *fastly.SecretStore) string {
return b.String()
}

func fmtStores(s *fastly.SecretStores) string {
func fmtStores(s []fastly.SecretStore) string {
var b bytes.Buffer
text.PrintSecretStoresTbl(&b, s)
return b.String()
Expand Down
40 changes: 30 additions & 10 deletions pkg/commands/secretstore/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,55 @@ func (c *ListCommand) Exec(in io.Reader, out io.Writer) error {
return fsterr.ErrInvalidVerboseJSONCombo
}

var data []fastly.SecretStore

for {
o, err := c.Globals.APIClient.ListSecretStores(&c.Input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

if ok, err := c.WriteJSON(out, o); ok {
// No pagination prompt w/ JSON output.
// FIXME: This should be fixed here and for KV Store.
return err
}
if o != nil {
data = append(data, o.Data...)

if c.JSONOutput.Enabled || c.Globals.Flags.NonInteractive || c.Globals.Flags.AutoYes {
if o.Meta.NextCursor != "" {
c.Input.Cursor = o.Meta.NextCursor
continue
}
break
}

text.PrintSecretStoresTbl(out, o)
text.PrintSecretStoresTbl(out, o.Data)

if o != nil && o.Meta.NextCursor != "" {
// Check if 'out' is interactive before prompting.
if !c.Globals.Flags.NonInteractive && !c.Globals.Flags.AutoYes && text.IsTTY(out) {
if o.Meta.NextCursor != "" {
text.Break(out)
printNext, err := text.AskYesNo(out, "Print next page [yes/no]: ", in)
if err != nil {
return err
}
if printNext {
text.Break(out)
c.Input.Cursor = o.Meta.NextCursor
continue
}
}
}

return nil
break
}

ok, err := c.WriteJSON(out, data)
if err != nil {
return err
}

// Only print output here if we've not already printed JSON.
// And only if we're non interactive.
// Otherwise interactive mode would have displayed each page of data.
if !ok && (c.Globals.Flags.NonInteractive || c.Globals.Flags.AutoYes) {
text.PrintSecretStoresTbl(out, data)
}
return nil
}
10 changes: 4 additions & 6 deletions pkg/commands/secretstore/secretstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ func TestListStoresCommand(t *testing.T) {

stores := &fastly.SecretStores{
Meta: fastly.SecretStoreMeta{
Limit: 123,
NextCursor: "abc",
Limit: 123,
},
Data: []fastly.SecretStore{
{ID: storeID, Name: storeName},
Expand All @@ -299,7 +298,6 @@ func TestListStoresCommand(t *testing.T) {
},
},
wantAPIInvoked: true,
wantOutput: fmtStores(&fastly.SecretStores{}),
},
{
args: "list",
Expand All @@ -319,7 +317,7 @@ func TestListStoresCommand(t *testing.T) {
},
},
wantAPIInvoked: true,
wantOutput: fmtStores(stores),
wantOutput: fmtStores(stores.Data),
},
{
args: "list --json",
Expand All @@ -329,7 +327,7 @@ func TestListStoresCommand(t *testing.T) {
},
},
wantAPIInvoked: true,
wantOutput: fstfmt.EncodeJSON(stores),
wantOutput: fstfmt.EncodeJSON([]fastly.SecretStore{stores.Data[0]}),
},
}

Expand All @@ -351,7 +349,7 @@ func TestListStoresCommand(t *testing.T) {
err := app.Run(opts)

testutil.AssertErrorContains(t, err, testcase.wantError)
testutil.AssertString(t, testcase.wantOutput, stdout.String())
testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput)
if apiInvoked != testcase.wantAPIInvoked {
t.Fatalf("API ListSecretStores invoked = %v, want %v", apiInvoked, testcase.wantAPIInvoked)
}
Expand Down
17 changes: 3 additions & 14 deletions pkg/text/secretstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,14 @@ import (
)

// PrintSecretStoresTbl displays store data in a table format.
func PrintSecretStoresTbl(out io.Writer, stores *fastly.SecretStores) {
func PrintSecretStoresTbl(out io.Writer, stores []fastly.SecretStore) {
tbl := NewTable(out)
tbl.AddHeader("Name", "ID")

if stores == nil {
tbl.Print()
return
}

for _, s := range stores.Data {
// avoid gosec loop aliasing check :/
s := s
tbl.AddLine(s.Name, s.ID)
for _, store := range stores {
tbl.AddLine(store.Name, store.ID)
}
tbl.Print()

if stores.Meta.NextCursor != "" {
fmt.Fprintf(out, "\nNext cursor: %s\n", stores.Meta.NextCursor)
}
}

// PrintSecretsTbl displays secrets data in a table format.
Expand Down