(client *api.Client, path string, newData map[string]interface{})
| 280 | } |
| 281 | |
| 282 | func (c *KVPatchCommand) readThenWrite(client *api.Client, path string, newData map[string]interface{}) (*api.Secret, int) { |
| 283 | // First, do a read. |
| 284 | // Note that we don't want to see curl output for the read request. |
| 285 | curOutputCurl := client.OutputCurlString() |
| 286 | client.SetOutputCurlString(false) |
| 287 | outputPolicy := client.OutputPolicy() |
| 288 | client.SetOutputPolicy(false) |
| 289 | secret, err := kvReadRequest(client, path, nil) |
| 290 | if err != nil { |
| 291 | c.UI.Error(fmt.Sprintf("Error doing pre-read at %s: %s", path, err)) |
| 292 | return nil, 2 |
| 293 | } |
| 294 | client.SetOutputCurlString(curOutputCurl) |
| 295 | client.SetOutputPolicy(outputPolicy) |
| 296 | |
| 297 | // Make sure a value already exists |
| 298 | if secret == nil || secret.Data == nil { |
| 299 | c.UI.Error(fmt.Sprintf("No value found at %s", path)) |
| 300 | return nil, 2 |
| 301 | } |
| 302 | |
| 303 | // Verify metadata found |
| 304 | rawMeta, ok := secret.Data["metadata"] |
| 305 | if !ok || rawMeta == nil { |
| 306 | c.UI.Error(fmt.Sprintf("No metadata found at %s; patch only works on existing data", path)) |
| 307 | return nil, 2 |
| 308 | } |
| 309 | meta, ok := rawMeta.(map[string]interface{}) |
| 310 | if !ok { |
| 311 | c.UI.Error(fmt.Sprintf("Metadata found at %s is not the expected type (JSON object)", path)) |
| 312 | return nil, 2 |
| 313 | } |
| 314 | if meta == nil { |
| 315 | c.UI.Error(fmt.Sprintf("No metadata found at %s; patch only works on existing data", path)) |
| 316 | return nil, 2 |
| 317 | } |
| 318 | |
| 319 | // Verify old data found |
| 320 | rawData, ok := secret.Data["data"] |
| 321 | if !ok || rawData == nil { |
| 322 | c.UI.Error(fmt.Sprintf("No data found at %s; patch only works on existing data", path)) |
| 323 | return nil, 2 |
| 324 | } |
| 325 | data, ok := rawData.(map[string]interface{}) |
| 326 | if !ok { |
| 327 | c.UI.Error(fmt.Sprintf("Data found at %s is not the expected type (JSON object)", path)) |
| 328 | return nil, 2 |
| 329 | } |
| 330 | if data == nil { |
| 331 | c.UI.Error(fmt.Sprintf("No data found at %s; patch only works on existing data", path)) |
| 332 | return nil, 2 |
| 333 | } |
| 334 | |
| 335 | // Copy new data over |
| 336 | for k, v := range newData { |
| 337 | data[k] = v |
| 338 | } |
| 339 |
no test coverage detected