Skip to content

Commit 1905503

Browse files
authored
add output option for one-line comma separated (#66)
* add output option for one-line comma separated * move one-line output to outputs.go
1 parent 8ad5d83 commit 1905503

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

cmd/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const (
4242
terraformHCL = "terraform-hcl"
4343
tableOutput = "table"
4444
tableWideOutput = "table-wide"
45+
oneLine = "one-line"
4546
)
4647

4748
// Filter Flag Constants
@@ -110,6 +111,7 @@ Full docs can be found at github.com/aws/amazon-` + binName
110111
cliOutputTypes := []string{
111112
tableOutput,
112113
tableWideOutput,
114+
oneLine,
113115
}
114116
resultsOutputFn := outputs.SimpleInstanceTypeOutput
115117

@@ -267,6 +269,8 @@ func getOutputFn(outputFlag *string, currentFn selector.InstanceTypesOutputFn) s
267269
return selector.InstanceTypesOutputFn(outputs.TableOutputWide)
268270
case tableOutput:
269271
return selector.InstanceTypesOutputFn(outputs.TableOutputShort)
272+
case oneLine:
273+
return selector.InstanceTypesOutputFn(outputs.OneLineOutput)
270274
}
271275
}
272276
return outputFn

pkg/selector/outputs/outputs.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,15 @@ func TableOutputWide(instanceTypeInfoSlice []*ec2.InstanceTypeInfo) []string {
260260
w.Flush()
261261
return []string{buf.String()}
262262
}
263+
264+
// OneLineOutput is an output function which prints the instance type names on a single line separated by commas
265+
func OneLineOutput(instanceTypeInfoSlice []*ec2.InstanceTypeInfo) []string {
266+
instanceTypeNames := []string{}
267+
for _, instanceType := range instanceTypeInfoSlice {
268+
instanceTypeNames = append(instanceTypeNames, *instanceType.InstanceType)
269+
}
270+
if len(instanceTypeNames) == 0 {
271+
return []string{}
272+
}
273+
return []string{strings.Join(instanceTypeNames, ",")}
274+
}

pkg/selector/outputs/outputs_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,16 @@ func TestTableOutput_MBtoGB(t *testing.T) {
129129
outputStr = strings.Join(instanceTypeOut, "")
130130
h.Assert(t, strings.Contains(outputStr, "15.000"), "table should include 15.000 GB of memory")
131131
}
132+
133+
func TestOneLineOutput(t *testing.T) {
134+
instanceTypes := getInstanceTypes(t, "t3_micro_and_p3_16xl.json")
135+
instanceTypeOut := outputs.OneLineOutput(instanceTypes)
136+
h.Assert(t, len(instanceTypeOut) == 1, "Should always return 1 line")
137+
h.Assert(t, instanceTypeOut[0] == "t3.micro,p3.16xlarge", "Should return both instance types separated by a comma")
138+
139+
instanceTypeOut = outputs.OneLineOutput([]*ec2.InstanceTypeInfo{})
140+
h.Assert(t, len(instanceTypeOut) == 0, "Should return 0 instance types when passed empty slice")
141+
142+
instanceTypeOut = outputs.OneLineOutput(nil)
143+
h.Assert(t, len(instanceTypeOut) == 0, "Should return 0 instance types when passed nil")
144+
}

0 commit comments

Comments
 (0)