Skip to content

Commit 4e0da03

Browse files
author
Rodrigo Okamoto
committed
moved all sorting constants to sorter.go
1 parent 02a20fc commit 4e0da03

File tree

4 files changed

+58
-81
lines changed

4 files changed

+58
-81
lines changed

cmd/main.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ const (
4949
tableWideOutput = "table-wide"
5050
oneLine = "one-line"
5151
bubbleTeaOutput = "interactive"
52+
53+
// Sort filter default
54+
instanceNamePath = ".InstanceType"
5255
)
5356

5457
// Filter Flag Constants
@@ -120,20 +123,6 @@ const (
120123
sortBy = "sort-by"
121124
)
122125

123-
// Sorting Constants
124-
const (
125-
// Direction
126-
127-
sortAscending = "ascending"
128-
sortAsc = "asc"
129-
sortDescending = "descending"
130-
sortDesc = "desc"
131-
132-
// Sort filter default
133-
134-
instanceNamePath = ".InstanceType"
135-
)
136-
137126
var (
138127
// versionID is overridden at compilation with the version based on the git tag
139128
versionID = "dev"
@@ -164,10 +153,10 @@ Full docs can be found at github.com/aws/amazon-` + binName
164153
resultsOutputFn := outputs.SimpleInstanceTypeOutput
165154

166155
cliSortDirections := []string{
167-
sortAscending,
168-
sortAsc,
169-
sortDescending,
170-
sortDesc,
156+
sorter.SortAscending,
157+
sorter.SortAsc,
158+
sorter.SortDescending,
159+
sorter.SortDesc,
171160
}
172161

173162
// Registers flags with specific input types from the cli pkg
@@ -234,7 +223,7 @@ Full docs can be found at github.com/aws/amazon-` + binName
234223
cli.ConfigBoolFlag(verbose, cli.StringMe("v"), nil, "Verbose - will print out full instance specs")
235224
cli.ConfigBoolFlag(help, cli.StringMe("h"), nil, "Help")
236225
cli.ConfigBoolFlag(version, nil, nil, "Prints CLI version")
237-
cli.ConfigStringOptionsFlag(sortDirection, nil, cli.StringMe(sortAscending), fmt.Sprintf("Specify the direction to sort in (%s)", strings.Join(cliSortDirections, ", ")), cliSortDirections)
226+
cli.ConfigStringOptionsFlag(sortDirection, nil, cli.StringMe(sorter.SortAscending), fmt.Sprintf("Specify the direction to sort in (%s)", strings.Join(cliSortDirections, ", ")), cliSortDirections)
238227
cli.ConfigStringFlag(sortBy, nil, cli.StringMe(instanceNamePath), "Specify the field to sort by. Quantity flags present in this CLI (memory, gpus, etc.) or a JSON path to the appropriate instance type field (Ex: \".MemoryInfo.SizeInMiB\") is acceptable.", nil)
239228

240229
// Parses the user input with the registered flags and runs type specific validation on the user input

pkg/selector/outputs/bubbletea.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package outputs
1515

1616
import (
1717
"github.com/aws/amazon-ec2-instance-selector/v2/pkg/instancetypes"
18+
"github.com/aws/amazon-ec2-instance-selector/v2/pkg/sorter"
1819
tea "github.com/charmbracelet/bubbletea"
1920
"github.com/charmbracelet/lipgloss"
2021
"github.com/muesli/termenv"
@@ -83,9 +84,9 @@ func (m BubbleTeaModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
8384
if m.currentState == stateSorting && msg.String() == "enter" {
8485
jsonPath := m.sortingModel.sortTextInput.Value()
8586

86-
sortDirection := "asc"
87+
sortDirection := sorter.SortAscending
8788
if m.sortingModel.isDescending {
88-
sortDirection = "desc"
89+
sortDirection = sorter.SortDescending
8990
}
9091

9192
var err error
@@ -140,9 +141,9 @@ func (m BubbleTeaModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
140141
if m.currentState == stateSorting {
141142
sortFilter := string(m.sortingModel.shorthandList.SelectedItem().(item))
142143

143-
sortDirection := "asc"
144+
sortDirection := sorter.SortAscending
144145
if m.sortingModel.isDescending {
145-
sortDirection = "desc"
146+
sortDirection = sorter.SortDescending
146147
}
147148

148149
var err error

pkg/selector/outputs/sortingView.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"strings"
2020

2121
"github.com/aws/amazon-ec2-instance-selector/v2/pkg/instancetypes"
22+
"github.com/aws/amazon-ec2-instance-selector/v2/pkg/sorter"
2223
"github.com/charmbracelet/bubbles/key"
2324
"github.com/charmbracelet/bubbles/list"
2425
"github.com/charmbracelet/bubbles/textinput"
@@ -32,20 +33,6 @@ const (
3233
sortingTitlePadding = 3
3334
sortingFooterPadding = 2
3435

35-
// shorthand flags
36-
gpus = "gpus"
37-
inferenceAccelerators = "inference-accelerators"
38-
vcpus = "vcpus"
39-
memory = "memory"
40-
gpuMemoryTotal = "gpu-memory-total"
41-
networkInterfaces = "network-interfaces"
42-
spotPrice = "spot-price"
43-
odPrice = "on-demand-price"
44-
instanceStorage = "instance-storage"
45-
ebsOptimizedBaselineBandwidth = "ebs-optimized-baseline-bandwidth"
46-
ebsOptimizedBaselineThroughput = "ebs-optimized-baseline-throughput"
47-
ebsOptimizedBaselineIOPS = "ebs-optimized-baseline-iops"
48-
4936
// controls
5037
sortingListControls = "Controls: ↑/↓ - up/down • enter - select filter • tab - toggle direction • esc - return to table • q - quit"
5138
sortingTextControls = "Controls: ↑/↓ - up/down • tab - toggle direction • enter - enter json path"
@@ -151,18 +138,18 @@ func createListKeyMap() list.KeyMap {
151138
// createListItems creates a list item for shorthand sorting flag
152139
func createListItems() *[]list.Item {
153140
shorthandFlags := []string{
154-
gpus,
155-
inferenceAccelerators,
156-
vcpus,
157-
memory,
158-
gpuMemoryTotal,
159-
networkInterfaces,
160-
spotPrice,
161-
odPrice,
162-
instanceStorage,
163-
ebsOptimizedBaselineBandwidth,
164-
ebsOptimizedBaselineThroughput,
165-
ebsOptimizedBaselineIOPS,
141+
sorter.GPUCountField,
142+
sorter.InferenceAcceleratorsField,
143+
sorter.VCPUs,
144+
sorter.Memory,
145+
sorter.GPUMemoryTotal,
146+
sorter.NetworkInterfaces,
147+
sorter.SpotPrice,
148+
sorter.ODPrice,
149+
sorter.InstanceStorage,
150+
sorter.EBSOptimizedBaselineBandwidth,
151+
sorter.EBSOptimizedBaselineThroughput,
152+
sorter.EBSOptimizedBaselineIOPS,
166153
}
167154

168155
items := []list.Item{}

pkg/sorter/sorter.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,29 @@ import (
2828
const (
2929
// Sort direction
3030

31-
sortAscending = "ascending"
32-
sortAsc = "asc"
33-
sortDescending = "descending"
34-
sortDesc = "desc"
31+
SortAscending = "ascending"
32+
SortAsc = "asc"
33+
SortDescending = "descending"
34+
SortDesc = "desc"
3535

3636
// Not all fields can be reached through a json path (Ex: gpu count)
3737
// so we have special flags for such cases.
3838

39-
gpuCountField = "gpus"
40-
inferenceAcceleratorsField = "inference-accelerators"
39+
GPUCountField = "gpus"
40+
InferenceAcceleratorsField = "inference-accelerators"
4141

4242
// shorthand flags
4343

44-
vcpus = "vcpus"
45-
memory = "memory"
46-
gpuMemoryTotal = "gpu-memory-total"
47-
networkInterfaces = "network-interfaces"
48-
spotPrice = "spot-price"
49-
odPrice = "on-demand-price"
50-
instanceStorage = "instance-storage"
51-
ebsOptimizedBaselineBandwidth = "ebs-optimized-baseline-bandwidth"
52-
ebsOptimizedBaselineThroughput = "ebs-optimized-baseline-throughput"
53-
ebsOptimizedBaselineIOPS = "ebs-optimized-baseline-iops"
44+
VCPUs = "vcpus"
45+
Memory = "memory"
46+
GPUMemoryTotal = "gpu-memory-total"
47+
NetworkInterfaces = "network-interfaces"
48+
SpotPrice = "spot-price"
49+
ODPrice = "on-demand-price"
50+
InstanceStorage = "instance-storage"
51+
EBSOptimizedBaselineBandwidth = "ebs-optimized-baseline-bandwidth"
52+
EBSOptimizedBaselineThroughput = "ebs-optimized-baseline-throughput"
53+
EBSOptimizedBaselineIOPS = "ebs-optimized-baseline-iops"
5454

5555
// JSON field paths for shorthand flags
5656

@@ -91,16 +91,16 @@ type sorter struct {
9191
// sortDirection represents the direction to sort in. Valid options: "ascending", "asc", "descending", "desc".
9292
func Sort(instanceTypes []*instancetypes.Details, sortField string, sortDirection string) ([]*instancetypes.Details, error) {
9393
sortingKeysMap := map[string]string{
94-
vcpus: vcpuPath,
95-
memory: memoryPath,
96-
gpuMemoryTotal: gpuMemoryTotalPath,
97-
networkInterfaces: networkInterfacesPath,
98-
spotPrice: spotPricePath,
99-
odPrice: odPricePath,
100-
instanceStorage: instanceStoragePath,
101-
ebsOptimizedBaselineBandwidth: ebsOptimizedBaselineBandwidthPath,
102-
ebsOptimizedBaselineThroughput: ebsOptimizedBaselineThroughputPath,
103-
ebsOptimizedBaselineIOPS: ebsOptimizedBaselineIOPSPath,
94+
VCPUs: vcpuPath,
95+
Memory: memoryPath,
96+
GPUMemoryTotal: gpuMemoryTotalPath,
97+
NetworkInterfaces: networkInterfacesPath,
98+
SpotPrice: spotPricePath,
99+
ODPrice: odPricePath,
100+
InstanceStorage: instanceStoragePath,
101+
EBSOptimizedBaselineBandwidth: ebsOptimizedBaselineBandwidthPath,
102+
EBSOptimizedBaselineThroughput: ebsOptimizedBaselineThroughputPath,
103+
EBSOptimizedBaselineIOPS: ebsOptimizedBaselineIOPSPath,
104104
}
105105

106106
// determine if user used a shorthand for sorting flag
@@ -130,12 +130,12 @@ func Sort(instanceTypes []*instancetypes.Details, sortField string, sortDirectio
130130
func newSorter(instanceTypes []*instancetypes.Details, sortField string, sortDirection string) (*sorter, error) {
131131
var isDescending bool
132132
switch sortDirection {
133-
case sortDescending, sortDesc:
133+
case SortDescending, SortDesc:
134134
isDescending = true
135-
case sortAscending, sortAsc:
135+
case SortAscending, SortAsc:
136136
isDescending = false
137137
default:
138-
return nil, fmt.Errorf("invalid sort direction: %s (valid options: %s, %s, %s, %s)", sortDirection, sortAscending, sortAsc, sortDescending, sortDesc)
138+
return nil, fmt.Errorf("invalid sort direction: %s (valid options: %s, %s, %s, %s)", sortDirection, SortAscending, SortAsc, SortDescending, SortDesc)
139139
}
140140

141141
sortField = formatSortField(sortField)
@@ -163,7 +163,7 @@ func newSorter(instanceTypes []*instancetypes.Details, sortField string, sortDir
163163
// matches one of the special flags.
164164
func formatSortField(sortField string) string {
165165
// check to see if the sorting field matched one of the special exceptions
166-
if sortField == gpuCountField || sortField == inferenceAcceleratorsField {
166+
if sortField == GPUCountField || sortField == InferenceAcceleratorsField {
167167
return sortField
168168
}
169169

@@ -176,13 +176,13 @@ func newSorterNode(instanceType *instancetypes.Details, sortField string) (*sort
176176
// some important fields (such as gpu count) can not be accessed directly in the instancetypes.Details
177177
// struct, so we have special hard-coded flags to handle such cases
178178
switch sortField {
179-
case gpuCountField:
179+
case GPUCountField:
180180
gpuCount := getTotalGpusCount(instanceType)
181181
return &sorterNode{
182182
instanceType: instanceType,
183183
fieldValue: reflect.ValueOf(gpuCount),
184184
}, nil
185-
case inferenceAcceleratorsField:
185+
case InferenceAcceleratorsField:
186186
acceleratorsCount := getTotalAcceleratorsCount(instanceType)
187187
return &sorterNode{
188188
instanceType: instanceType,

0 commit comments

Comments
 (0)