|
| 1 | +package instance |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sort" |
| 6 | + |
| 7 | + "github.com/scaleway/scaleway-cli/internal/core" |
| 8 | + "github.com/scaleway/scaleway-sdk-go/api/instance/v1" |
| 9 | +) |
| 10 | + |
| 11 | +func volumeTypeListBuilder(cmd *core.Command) *core.Command { |
| 12 | + type customVolumeType struct { |
| 13 | + Type string `json:"type"` |
| 14 | + instance.VolumeType |
| 15 | + } |
| 16 | + |
| 17 | + cmd.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { |
| 18 | + res, err := runner(ctx, argsI) |
| 19 | + if err != nil { |
| 20 | + return res, err |
| 21 | + } |
| 22 | + |
| 23 | + volumeTypes := []*customVolumeType(nil) |
| 24 | + for typeName, volumeType := range res.(*instance.ListVolumesTypesResponse).Volumes { |
| 25 | + volumeTypes = append(volumeTypes, &customVolumeType{ |
| 26 | + Type: typeName, |
| 27 | + VolumeType: *volumeType, |
| 28 | + }) |
| 29 | + } |
| 30 | + |
| 31 | + // sort for consistent order output |
| 32 | + sort.Slice(volumeTypes, func(i, j int) bool { |
| 33 | + return volumeTypes[i].Type < volumeTypes[j].Type |
| 34 | + }) |
| 35 | + |
| 36 | + return volumeTypes, nil |
| 37 | + }) |
| 38 | + |
| 39 | + cmd.AllowAnonymousClient = true |
| 40 | + |
| 41 | + cmd.View = &core.View{ |
| 42 | + Fields: []*core.ViewField{ |
| 43 | + {FieldName: "Type", Label: "Type"}, |
| 44 | + {FieldName: "DisplayName", Label: "Name"}, |
| 45 | + {FieldName: "Capabilities.Snapshot", Label: "Snapshot"}, |
| 46 | + {FieldName: "Constraints.Min", Label: "Min"}, |
| 47 | + {FieldName: "Constraints.Max", Label: "Max"}, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + return cmd |
| 52 | +} |
0 commit comments