Skip to content

Commit 15d0a4e

Browse files
fix(keys): fix range checks on input (#93)
1 parent 359d135 commit 15d0a4e

2 files changed

Lines changed: 53 additions & 15 deletions

File tree

cmd/keys.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package cmd
22

33
import (
44
"fmt"
5+
"io"
56
"io/ioutil"
7+
"os"
68
"path/filepath"
79
"strconv"
810
"strings"
@@ -68,13 +70,19 @@ func KeyAdd(keyLocation string) error {
6870
var key api.KeyCreateRequest
6971

7072
if keyLocation == "" {
71-
key, err = chooseKey()
73+
ks, err := listKeys()
74+
if err != nil {
75+
return err
76+
}
77+
key, err = chooseKey(ks, os.Stdin)
78+
if err != nil {
79+
return err
80+
}
7281
} else {
7382
key, err = getKey(keyLocation)
74-
}
75-
76-
if err != nil {
77-
return err
83+
if err != nil {
84+
return err
85+
}
7886
}
7987

8088
fmt.Printf("Uploading %s to deis...", filepath.Base(key.Name))
@@ -88,13 +96,7 @@ func KeyAdd(keyLocation string) error {
8896
return nil
8997
}
9098

91-
func chooseKey() (api.KeyCreateRequest, error) {
92-
keys, err := listKeys()
93-
94-
if err != nil {
95-
return api.KeyCreateRequest{}, err
96-
}
97-
99+
func chooseKey(keys []api.KeyCreateRequest, input io.Reader) (api.KeyCreateRequest, error) {
98100
fmt.Println("Found the following SSH public keys:")
99101

100102
for i, key := range keys {
@@ -106,15 +108,15 @@ func chooseKey() (api.KeyCreateRequest, error) {
106108
var selected string
107109

108110
fmt.Print("Which would you like to use with Deis? ")
109-
fmt.Scanln(&selected)
111+
fmt.Fscanln(input, &selected)
110112

111113
numSelected, err := strconv.Atoi(selected)
112114

113115
if err != nil {
114-
return api.KeyCreateRequest{}, err
116+
return api.KeyCreateRequest{}, fmt.Errorf("%s is not a valid integer", selected)
115117
}
116118

117-
if numSelected > len(keys)+1 {
119+
if numSelected < 0 || numSelected > len(keys) {
118120
return api.KeyCreateRequest{}, fmt.Errorf("%d is not a valid option", numSelected)
119121
}
120122

cmd/keys_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package cmd
22

33
import (
4+
"io"
45
"io/ioutil"
56
"os"
67
"path/filepath"
78
"reflect"
9+
"strings"
810
"testing"
911

1012
"github.com/deis/controller-sdk-go/api"
@@ -121,3 +123,37 @@ func TestListKeys(t *testing.T) {
121123
t.Errorf("Expected %v, Got %v", expected, keys)
122124
}
123125
}
126+
127+
type chooseKeyCases struct {
128+
Reader io.Reader
129+
Expected string
130+
}
131+
132+
func TestChooseKey(t *testing.T) {
133+
testKeys := []api.KeyCreateRequest{
134+
api.KeyCreateRequest{},
135+
}
136+
137+
checks := []chooseKeyCases{
138+
chooseKeyCases{
139+
Reader: strings.NewReader("-1"),
140+
Expected: "-1 is not a valid option",
141+
},
142+
chooseKeyCases{
143+
Reader: strings.NewReader("2"),
144+
Expected: "2 is not a valid option",
145+
},
146+
chooseKeyCases{
147+
Reader: strings.NewReader("a"),
148+
Expected: "a is not a valid integer",
149+
},
150+
}
151+
152+
for _, check := range checks {
153+
_, err := chooseKey(testKeys, check.Reader)
154+
155+
if err.Error() != check.Expected {
156+
t.Errorf("Expected %s, Got %s", check.Expected, err.Error())
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)