Skip to content

Commit

Permalink
Merge pull request #21 from thaim/add-all-flag
Browse files Browse the repository at this point in the history
  • Loading branch information
thaim authored Jan 11, 2023
2 parents f9c1c71 + de0a451 commit 5d9130d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
15 changes: 12 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"log"
"os"
"runtime/debug"
Expand Down Expand Up @@ -42,9 +43,7 @@ func main() {
}
ids, err := Ec2id(name, client)
if err == nil && ids != nil {
for id := range ids {
fmt.Println(id)
}
printIds(os.Stdout, ids, all)
}
return err
},
Expand Down Expand Up @@ -72,3 +71,13 @@ func getVersion() string {

return i.Main.Version
}

func printIds(out io.Writer, ids []string, all bool) {
for _, id := range ids {
fmt.Fprintln(out, id)

if !all {
break
}
}
}
52 changes: 52 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"bytes"
"testing"
)

func TestMainPrintIds(t *testing.T) {
cases := []struct {
name string
ids []string
allFlag bool
expect string
}{
{
name: "print nothing with empty id",
ids: nil,
allFlag: false,
expect: "",
},
{
name: "print single id with single id",
ids: []string{"i-012345"},
allFlag: false,
expect: "i-012345\n",
},
{
name: "print single id with multiple ids and disabled all flag",
ids: []string{"i-012345", "i-6789ab", "i-cdef01"},
allFlag: false,
expect: "i-012345\n",
},
{
name: "print multiple ids with multiple ids and enabled all flag",
ids: []string{"i-012345", "i-6789ab", "i-cdef01"},
allFlag: true,
expect: "i-012345\ni-6789ab\ni-cdef01\n",
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer

printIds(&buf, tt.ids, tt.allFlag)

if bufString := buf.String(); bufString != tt.expect {
t.Errorf("expect %s, got id: %s", tt.expect, buf.String())
}
})
}
}

0 comments on commit 5d9130d

Please sign in to comment.