|
| 1 | +package kvstoreentry |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/fastly/go-fastly/v11/fastly" |
| 11 | + |
| 12 | + "github.com/fastly/cli/pkg/argparser" |
| 13 | + fsterr "github.com/fastly/cli/pkg/errors" |
| 14 | + "github.com/fastly/cli/pkg/global" |
| 15 | + "github.com/fastly/cli/pkg/text" |
| 16 | +) |
| 17 | + |
| 18 | +// GetCommand calls the Fastly API to fetch the value of a key from an kv store. |
| 19 | +type GetCommand struct { |
| 20 | + argparser.Base |
| 21 | + argparser.JSONOutput |
| 22 | + |
| 23 | + Input fastly.GetKVStoreItemInput |
| 24 | + Generation string |
| 25 | +} |
| 26 | + |
| 27 | +// NewGetCommand returns a usable command registered under the parent. |
| 28 | +func NewGetCommand(parent argparser.Registerer, g *global.Data) *GetCommand { |
| 29 | + c := GetCommand{ |
| 30 | + Base: argparser.Base{ |
| 31 | + Globals: g, |
| 32 | + // This argument suppresses the 'Fastly API' output from the global verbose command. |
| 33 | + SuppressVerbose: true, |
| 34 | + }, |
| 35 | + } |
| 36 | + c.CmdClause = parent.Command("get", "Get the value associated with a key") |
| 37 | + |
| 38 | + // Required. |
| 39 | + c.CmdClause.Flag("key", "Key name").Short('k').Required().StringVar(&c.Input.Key) |
| 40 | + c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.StoreID) |
| 41 | + |
| 42 | + // Optional. |
| 43 | + c.CmdClause.Flag("if-generation-match", "Compares if the provided generation marker matches that of the object").StringVar(&c.Generation) |
| 44 | + c.RegisterFlagBool(c.JSONFlag()) // --json |
| 45 | + |
| 46 | + return &c |
| 47 | +} |
| 48 | + |
| 49 | +// Exec invokes the application logic for the command. |
| 50 | +func (c *GetCommand) Exec(_ io.Reader, out io.Writer) error { |
| 51 | + // As the 'describe' command provides the object attributes, |
| 52 | + // we won't be supporting a --verbose flag here. |
| 53 | + if c.Globals.Flags.Verbose { |
| 54 | + return fmt.Errorf("the 'get' command does not support the --verbose flag") |
| 55 | + } |
| 56 | + |
| 57 | + if c.Globals.Verbose() && c.JSONOutput.Enabled { |
| 58 | + return fsterr.ErrInvalidVerboseJSONCombo |
| 59 | + } |
| 60 | + |
| 61 | + // Validate generation value before making API call |
| 62 | + var inputGeneration uint64 |
| 63 | + if c.Generation != "" { |
| 64 | + var err error |
| 65 | + inputGeneration, err = strconv.ParseUint(c.Generation, 10, 64) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("invalid generation value: %s", c.Generation) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + result, err := c.Globals.APIClient.GetKVStoreItem(context.TODO(), &c.Input) |
| 72 | + if err != nil { |
| 73 | + c.Globals.ErrLog.Add(err) |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + // Check if the generation marker matches the API result |
| 78 | + if c.Generation != "" { |
| 79 | + if inputGeneration != result.Generation { |
| 80 | + return fmt.Errorf("generation value does not match: expected %d, got %d", result.Generation, inputGeneration) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Ensure we close the value reader. |
| 85 | + if result.Value != nil { |
| 86 | + defer result.Value.Close() |
| 87 | + } |
| 88 | + |
| 89 | + // Read the value from ReadCloser. |
| 90 | + var value string |
| 91 | + if result.Value != nil { |
| 92 | + valueBytes, err := io.ReadAll(result.Value) |
| 93 | + if err != nil { |
| 94 | + c.Globals.ErrLog.Add(err) |
| 95 | + return err |
| 96 | + } |
| 97 | + value = string(valueBytes) |
| 98 | + } |
| 99 | + |
| 100 | + if c.JSONOutput.Enabled { |
| 101 | + // We are encoding the value of the item here to ensure safe |
| 102 | + // output for binary content along with other outputs. |
| 103 | + encodedValue := base64.StdEncoding.EncodeToString([]byte(value)) |
| 104 | + text.Output(out, `{"%s": "%s"}`, c.Input.Key, encodedValue) |
| 105 | + return nil |
| 106 | + } |
| 107 | + |
| 108 | + // IMPORTANT: Don't use `text` package as binary data can be messed up. |
| 109 | + fmt.Fprint(out, value) |
| 110 | + return nil |
| 111 | +} |
0 commit comments