github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/logging/ftp/update.go (about) 1 package ftp 2 3 import ( 4 "io" 5 6 "github.com/fastly/go-fastly/v9/fastly" 7 8 "github.com/fastly/cli/pkg/argparser" 9 "github.com/fastly/cli/pkg/commands/logging/common" 10 "github.com/fastly/cli/pkg/errors" 11 "github.com/fastly/cli/pkg/global" 12 "github.com/fastly/cli/pkg/manifest" 13 "github.com/fastly/cli/pkg/text" 14 ) 15 16 // UpdateCommand calls the Fastly API to update an FTP logging endpoint. 17 type UpdateCommand struct { 18 argparser.Base 19 Manifest manifest.Data 20 21 // Required. 22 EndpointName string // Can't shadow argparser.Base method Name(). 23 ServiceName argparser.OptionalServiceNameID 24 ServiceVersion argparser.OptionalServiceVersion 25 26 // Optional. 27 AutoClone argparser.OptionalAutoClone 28 NewName argparser.OptionalString 29 Address argparser.OptionalString 30 Port argparser.OptionalInt 31 Username argparser.OptionalString 32 Password argparser.OptionalString 33 PublicKey argparser.OptionalString 34 Path argparser.OptionalString 35 Period argparser.OptionalInt 36 GzipLevel argparser.OptionalInt 37 Format argparser.OptionalString 38 FormatVersion argparser.OptionalInt 39 ResponseCondition argparser.OptionalString 40 TimestampFormat argparser.OptionalString 41 Placement argparser.OptionalString 42 CompressionCodec argparser.OptionalString 43 } 44 45 // NewUpdateCommand returns a usable command registered under the parent. 46 func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateCommand { 47 c := UpdateCommand{ 48 Base: argparser.Base{ 49 Globals: g, 50 }, 51 } 52 c.CmdClause = parent.Command("update", "Update an FTP logging endpoint on a Fastly service version") 53 54 // Required. 55 c.CmdClause.Flag("name", "The name of the FTP logging object").Short('n').Required().StringVar(&c.EndpointName) 56 c.RegisterFlag(argparser.StringFlagOpts{ 57 Name: argparser.FlagVersionName, 58 Description: argparser.FlagVersionDesc, 59 Dst: &c.ServiceVersion.Value, 60 Required: true, 61 }) 62 63 // Optional. 64 c.CmdClause.Flag("address", "An hostname or IPv4 address").Action(c.Address.Set).StringVar(&c.Address.Value) 65 c.RegisterAutoCloneFlag(argparser.AutoCloneFlagOpts{ 66 Action: c.AutoClone.Set, 67 Dst: &c.AutoClone.Value, 68 }) 69 common.CompressionCodec(c.CmdClause, &c.CompressionCodec) 70 common.Format(c.CmdClause, &c.Format) 71 common.FormatVersion(c.CmdClause, &c.FormatVersion) 72 common.GzipLevel(c.CmdClause, &c.GzipLevel) 73 c.CmdClause.Flag("new-name", "New name of the FTP logging object").Action(c.NewName.Set).StringVar(&c.NewName.Value) 74 c.CmdClause.Flag("password", "The password for the server (for anonymous use an email address)").Action(c.Password.Set).StringVar(&c.Password.Value) 75 c.CmdClause.Flag("path", "The path to upload log files to. If the path ends in / then it is treated as a directory").Action(c.Path.Set).StringVar(&c.Path.Value) 76 common.Period(c.CmdClause, &c.Period) 77 common.Placement(c.CmdClause, &c.Placement) 78 c.CmdClause.Flag("port", "The port number").Action(c.Port.Set).IntVar(&c.Port.Value) 79 common.PublicKey(c.CmdClause, &c.PublicKey) 80 common.ResponseCondition(c.CmdClause, &c.ResponseCondition) 81 c.RegisterFlag(argparser.StringFlagOpts{ 82 Name: argparser.FlagServiceIDName, 83 Description: argparser.FlagServiceIDDesc, 84 Dst: &g.Manifest.Flag.ServiceID, 85 Short: 's', 86 }) 87 c.RegisterFlag(argparser.StringFlagOpts{ 88 Action: c.ServiceName.Set, 89 Name: argparser.FlagServiceName, 90 Description: argparser.FlagServiceDesc, 91 Dst: &c.ServiceName.Value, 92 }) 93 common.TimestampFormat(c.CmdClause, &c.TimestampFormat) 94 c.CmdClause.Flag("username", "The username for the server (can be anonymous)").Action(c.Username.Set).StringVar(&c.Username.Value) 95 return &c 96 } 97 98 // ConstructInput transforms values parsed from CLI flags into an object to be used by the API client library. 99 func (c *UpdateCommand) ConstructInput(serviceID string, serviceVersion int) (*fastly.UpdateFTPInput, error) { 100 input := fastly.UpdateFTPInput{ 101 ServiceID: serviceID, 102 ServiceVersion: serviceVersion, 103 Name: c.EndpointName, 104 } 105 106 // Set new values if set by user. 107 if c.NewName.WasSet { 108 input.NewName = &c.NewName.Value 109 } 110 111 if c.Address.WasSet { 112 input.Address = &c.Address.Value 113 } 114 115 if c.Port.WasSet { 116 input.Port = &c.Port.Value 117 } 118 119 if c.Username.WasSet { 120 input.Username = &c.Username.Value 121 } 122 123 if c.Password.WasSet { 124 input.Password = &c.Password.Value 125 } 126 127 if c.PublicKey.WasSet { 128 input.PublicKey = &c.PublicKey.Value 129 } 130 131 if c.Path.WasSet { 132 input.Path = &c.Path.Value 133 } 134 135 if c.Period.WasSet { 136 input.Period = &c.Period.Value 137 } 138 139 if c.FormatVersion.WasSet { 140 input.FormatVersion = &c.FormatVersion.Value 141 } 142 143 if c.GzipLevel.WasSet { 144 input.GzipLevel = &c.GzipLevel.Value 145 } 146 147 if c.Format.WasSet { 148 input.Format = &c.Format.Value 149 } 150 151 if c.ResponseCondition.WasSet { 152 input.ResponseCondition = &c.ResponseCondition.Value 153 } 154 155 if c.TimestampFormat.WasSet { 156 input.TimestampFormat = &c.TimestampFormat.Value 157 } 158 159 if c.Placement.WasSet { 160 input.Placement = &c.Placement.Value 161 } 162 163 if c.CompressionCodec.WasSet { 164 input.CompressionCodec = &c.CompressionCodec.Value 165 } 166 167 return &input, nil 168 } 169 170 // Exec invokes the application logic for the command. 171 func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error { 172 serviceID, serviceVersion, err := argparser.ServiceDetails(argparser.ServiceDetailsOpts{ 173 AutoCloneFlag: c.AutoClone, 174 APIClient: c.Globals.APIClient, 175 Manifest: *c.Globals.Manifest, 176 Out: out, 177 ServiceNameFlag: c.ServiceName, 178 ServiceVersionFlag: c.ServiceVersion, 179 VerboseMode: c.Globals.Flags.Verbose, 180 }) 181 if err != nil { 182 c.Globals.ErrLog.AddWithContext(err, map[string]any{ 183 "Service ID": serviceID, 184 "Service Version": errors.ServiceVersion(serviceVersion), 185 }) 186 return err 187 } 188 189 input, err := c.ConstructInput(serviceID, fastly.ToValue(serviceVersion.Number)) 190 if err != nil { 191 c.Globals.ErrLog.Add(err) 192 return err 193 } 194 195 ftp, err := c.Globals.APIClient.UpdateFTP(input) 196 if err != nil { 197 c.Globals.ErrLog.Add(err) 198 return err 199 } 200 201 text.Success(out, 202 "Updated FTP logging endpoint %s (service %s version %d)", 203 fastly.ToValue(ftp.Name), 204 fastly.ToValue(ftp.ServiceID), 205 fastly.ToValue(ftp.ServiceVersion), 206 ) 207 return nil 208 }