github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/dictionaryentry/update.go (about) 1 package dictionaryentry 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "os" 8 9 "github.com/fastly/go-fastly/v9/fastly" 10 11 "github.com/fastly/cli/pkg/argparser" 12 "github.com/fastly/cli/pkg/global" 13 "github.com/fastly/cli/pkg/text" 14 ) 15 16 // UpdateCommand calls the Fastly API to update a dictionary item. 17 type UpdateCommand struct { 18 argparser.Base 19 20 Input fastly.UpdateDictionaryItemInput 21 InputBatch fastly.BatchModifyDictionaryItemsInput 22 file argparser.OptionalString 23 serviceName argparser.OptionalServiceNameID 24 } 25 26 // NewUpdateCommand returns a usable command registered under the parent. 27 func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand { 28 c := UpdateCommand{ 29 Base: argparser.Base{ 30 Globals: g, 31 }, 32 } 33 c.CmdClause = parent.Command("update", "Update or insert an item on a Fastly edge dictionary") 34 35 // Required. 36 c.CmdClause.Flag("dictionary-id", "Dictionary ID").Required().StringVar(&c.Input.DictionaryID) 37 38 // Optional. 39 c.CmdClause.Flag("file", "Batch update json file").Action(c.file.Set).StringVar(&c.file.Value) 40 c.CmdClause.Flag("key", "Dictionary item key").StringVar(&c.Input.ItemKey) 41 c.RegisterFlag(argparser.StringFlagOpts{ 42 Name: argparser.FlagServiceIDName, 43 Description: argparser.FlagServiceIDDesc, 44 Dst: &g.Manifest.Flag.ServiceID, 45 Short: 's', 46 }) 47 c.RegisterFlag(argparser.StringFlagOpts{ 48 Action: c.serviceName.Set, 49 Name: argparser.FlagServiceName, 50 Description: argparser.FlagServiceDesc, 51 Dst: &c.serviceName.Value, 52 }) 53 c.CmdClause.Flag("value", "Dictionary item value").StringVar(&c.Input.ItemValue) 54 return &c 55 } 56 57 // Exec invokes the application logic for the command. 58 func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error { 59 serviceID, source, flag, err := argparser.ServiceID(c.serviceName, *c.Globals.Manifest, c.Globals.APIClient, c.Globals.ErrLog) 60 if err != nil { 61 return err 62 } 63 if c.Globals.Verbose() { 64 argparser.DisplayServiceID(serviceID, flag, source, out) 65 } 66 67 c.Input.ServiceID = serviceID 68 c.InputBatch.ServiceID = serviceID 69 c.InputBatch.DictionaryID = c.Input.DictionaryID 70 71 if c.file.WasSet { 72 err := c.batchModify(out) 73 if err != nil { 74 c.Globals.ErrLog.Add(err) 75 return err 76 } 77 return nil 78 } 79 80 if c.Input.ItemKey == "" || c.Input.ItemValue == "" { 81 return fmt.Errorf("an empty value is not allowed for either the '--key' or '--value' flags") 82 } 83 84 d, err := c.Globals.APIClient.UpdateDictionaryItem(&c.Input) 85 if err != nil { 86 c.Globals.ErrLog.Add(err) 87 return err 88 } 89 90 text.Success(out, "Updated dictionary item (service %s)\n\n", fastly.ToValue(d.ServiceID)) 91 text.PrintDictionaryItem(out, "", d) 92 return nil 93 } 94 95 func (c *UpdateCommand) batchModify(out io.Writer) error { 96 jsonFile, err := os.Open(c.file.Value) 97 if err != nil { 98 c.Globals.ErrLog.Add(err) 99 return err 100 } 101 102 jsonBytes, err := io.ReadAll(jsonFile) 103 if err != nil { 104 c.Globals.ErrLog.Add(err) 105 return err 106 } 107 108 err = json.Unmarshal(jsonBytes, &c.InputBatch) 109 if err != nil { 110 c.Globals.ErrLog.Add(err) 111 return err 112 } 113 114 if len(c.InputBatch.Items) == 0 { 115 return fmt.Errorf("item key not found in file %s", c.file.Value) 116 } 117 118 err = c.Globals.APIClient.BatchModifyDictionaryItems(&c.InputBatch) 119 if err != nil { 120 c.Globals.ErrLog.Add(err) 121 return err 122 } 123 124 text.Success(out, "Made %d modifications of Dictionary %s on service %s", len(c.InputBatch.Items), c.Input.DictionaryID, c.InputBatch.ServiceID) 125 return nil 126 }