github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/secretstore/list.go (about)

     1  package secretstore
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/fastly/go-fastly/v9/fastly"
     7  
     8  	"github.com/fastly/cli/pkg/argparser"
     9  	fsterr "github.com/fastly/cli/pkg/errors"
    10  	"github.com/fastly/cli/pkg/global"
    11  	"github.com/fastly/cli/pkg/text"
    12  )
    13  
    14  // NewListCommand returns a usable command registered under the parent.
    15  func NewListCommand(parent argparser.Registerer, g *global.Data) *ListCommand {
    16  	c := ListCommand{
    17  		Base: argparser.Base{
    18  			Globals: g,
    19  		},
    20  	}
    21  
    22  	c.CmdClause = parent.Command("list", "List secret stores")
    23  
    24  	// Optional.
    25  	c.RegisterFlag(argparser.CursorFlag(&c.Input.Cursor))  // --cursor
    26  	c.RegisterFlagBool(c.JSONFlag())                       // --json
    27  	c.RegisterFlagInt(argparser.LimitFlag(&c.Input.Limit)) // --limit
    28  
    29  	return &c
    30  }
    31  
    32  // ListCommand calls the Fastly API to list appropriate resources.
    33  type ListCommand struct {
    34  	argparser.Base
    35  	argparser.JSONOutput
    36  
    37  	// NOTE: API returns 10 items even when --limit is set to smaller.
    38  	Input fastly.ListSecretStoresInput
    39  }
    40  
    41  // Exec invokes the application logic for the command.
    42  func (c *ListCommand) Exec(in io.Reader, out io.Writer) error {
    43  	if c.Globals.Verbose() && c.JSONOutput.Enabled {
    44  		return fsterr.ErrInvalidVerboseJSONCombo
    45  	}
    46  
    47  	var data []fastly.SecretStore
    48  
    49  	for {
    50  		o, err := c.Globals.APIClient.ListSecretStores(&c.Input)
    51  		if err != nil {
    52  			c.Globals.ErrLog.Add(err)
    53  			return err
    54  		}
    55  
    56  		if o != nil {
    57  			data = append(data, o.Data...)
    58  
    59  			if c.JSONOutput.Enabled || c.Globals.Flags.NonInteractive || c.Globals.Flags.AutoYes {
    60  				if o.Meta.NextCursor != "" {
    61  					c.Input.Cursor = o.Meta.NextCursor
    62  					continue
    63  				}
    64  				break
    65  			}
    66  
    67  			text.PrintSecretStoresTbl(out, o.Data)
    68  
    69  			if o.Meta.NextCursor != "" {
    70  				text.Break(out)
    71  				printNext, err := text.AskYesNo(out, "Print next page [y/N]: ", in)
    72  				if err != nil {
    73  					return err
    74  				}
    75  				if printNext {
    76  					text.Break(out)
    77  					c.Input.Cursor = o.Meta.NextCursor
    78  					continue
    79  				}
    80  			}
    81  		}
    82  
    83  		break
    84  	}
    85  
    86  	ok, err := c.WriteJSON(out, data)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	// Only print output here if we've not already printed JSON.
    92  	// And only if we're non interactive.
    93  	// Otherwise interactive mode would have displayed each page of data.
    94  	if !ok && (c.Globals.Flags.NonInteractive || c.Globals.Flags.AutoYes) {
    95  		text.PrintSecretStoresTbl(out, data)
    96  	}
    97  	return nil
    98  }