|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + log "github.com/Sirupsen/logrus" |
| 8 | +) |
| 9 | + |
| 10 | +var cmdPatch = &Command{ |
| 11 | + Exec: runPatch, |
| 12 | + UsageLine: "_patch [OPTIONS] IDENTIFIER FIELD=VALUE", |
| 13 | + Description: "", |
| 14 | + Hidden: true, |
| 15 | + Help: "PATCH an object on the API", |
| 16 | + Examples: ` |
| 17 | + $ scw _patch myserver state_detail=booted |
| 18 | +`, |
| 19 | +} |
| 20 | + |
| 21 | +func init() { |
| 22 | + cmdPatch.Flag.BoolVar(&patchHelp, []string{"h", "-help"}, false, "Print usage") |
| 23 | +} |
| 24 | + |
| 25 | +// Flags |
| 26 | +var patchHelp bool // -h, --help flag |
| 27 | + |
| 28 | +func runPatch(cmd *Command, args []string) { |
| 29 | + if patchHelp { |
| 30 | + cmd.PrintUsage() |
| 31 | + } |
| 32 | + if len(args) != 2 { |
| 33 | + cmd.PrintShortUsage() |
| 34 | + } |
| 35 | + |
| 36 | + // Parsing FIELD=VALUE |
| 37 | + updateParts := strings.Split(args[1], "=") |
| 38 | + if len(updateParts) != 2 { |
| 39 | + cmd.PrintShortUsage() |
| 40 | + } |
| 41 | + fieldName := updateParts[0] |
| 42 | + newValue := updateParts[1] |
| 43 | + |
| 44 | + ident := getIdentifier(cmd.API, args[0]) |
| 45 | + switch ident.Type { |
| 46 | + case IdentifierServer: |
| 47 | + var payload ScalewayServerPatchDefinition |
| 48 | + |
| 49 | + switch fieldName { |
| 50 | + case "state_detail": |
| 51 | + payload.StateDetail = &newValue |
| 52 | + case "name": |
| 53 | + payload.Name = &newValue |
| 54 | + log.Warnf("Use 'scw rename instead'") |
| 55 | + default: |
| 56 | + log.Fatalf("'_patch server %s=' not implemented", fieldName) |
| 57 | + } |
| 58 | + |
| 59 | + err := cmd.API.PatchServer(ident.Identifier, payload) |
| 60 | + if err != nil { |
| 61 | + log.Fatalf("Cannot rename server: %v", err) |
| 62 | + } |
| 63 | + default: |
| 64 | + log.Fatalf("_patch not implemented for this kind of object") |
| 65 | + } |
| 66 | + fmt.Println(ident.Identifier) |
| 67 | +} |
0 commit comments