Skip to content

Commit 776bcca

Browse files
committed
Address review
1 parent a5d95ea commit 776bcca

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

cmd/arduino-app-cli/version/version.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const (
4040
func NewVersionCmd(clientVersion string) *cobra.Command {
4141
cmd := &cobra.Command{
4242
Use: "version",
43-
Short: "Print the client and server version numbers for the Arduino App CLI.",
43+
Short: "Print the client and server versions for the Arduino App CLI.",
4444
Run: func(cmd *cobra.Command, args []string) {
4545
host, _ := cmd.Flags().GetString("host")
4646

@@ -72,16 +72,16 @@ func versionHandler(httpClient http.Client, clientVersion string, hostAndPort st
7272
Path: "/v1/version",
7373
}
7474

75-
daemonVersion := getServerVersion(httpClient, url.String())
75+
daemonVersion, err := getDaemonVersion(httpClient, url.String())
7676

7777
result := versionResult{
7878
Name: ProgramName,
79-
ClientVersion: clientVersion,
79+
Version: clientVersion,
8080
DaemonVersion: daemonVersion,
8181
}
8282

83-
if daemonVersion == "" {
84-
return result, fmt.Errorf("cannot connect to %s", hostAndPort)
83+
if err != nil {
84+
return result, fmt.Errorf("error getting daemon version %s", hostAndPort)
8585
}
8686
return result, nil
8787
}
@@ -105,42 +105,42 @@ func validateHost(hostPort string) (string, error) {
105105
return net.JoinHostPort(h, p), nil
106106
}
107107

108-
func getServerVersion(httpClient http.Client, url string) string {
108+
func getDaemonVersion(httpClient http.Client, url string) (string, error) {
109109
resp, err := httpClient.Get(url)
110110
if err != nil {
111-
return ""
111+
return "", err
112112
}
113113
defer resp.Body.Close()
114114

115115
if resp.StatusCode != http.StatusOK {
116-
return ""
116+
return "", fmt.Errorf("unexpected status code received")
117117
}
118118

119-
var serverResponse struct {
119+
var daemonResponse struct {
120120
Version string `json:"version"`
121121
}
122-
if err := json.NewDecoder(resp.Body).Decode(&serverResponse); err != nil {
123-
return ""
122+
if err := json.NewDecoder(resp.Body).Decode(&daemonResponse); err != nil {
123+
return "", err
124124
}
125125

126-
return serverResponse.Version
126+
return daemonResponse.Version, nil
127127
}
128128

129129
type versionResult struct {
130130
Name string `json:"name"`
131-
ClientVersion string `json:"client_version"`
131+
Version string `json:"version"`
132132
DaemonVersion string `json:"daemon_version,omitempty"`
133133
}
134134

135135
func (r versionResult) String() string {
136-
serverMessage := fmt.Sprintf("%s client version %s",
137-
ProgramName, r.ClientVersion)
136+
resultMessage := fmt.Sprintf("%s client version %s",
137+
ProgramName, r.Version)
138138

139139
if r.DaemonVersion != "" {
140-
serverMessage = fmt.Sprintf("%s\ndaemon version: %s",
141-
serverMessage, r.DaemonVersion)
140+
resultMessage = fmt.Sprintf("%s\ndaemon version: %s",
141+
resultMessage, r.DaemonVersion)
142142
}
143-
return serverMessage
143+
return resultMessage
144144
}
145145

146146
func (r versionResult) Data() interface{} {

cmd/arduino-app-cli/version/version_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestGetValidUrl(t *testing.T) {
4747
expectedResult: "localhost:8800",
4848
},
4949
{
50-
name: "Custom host and port should return the default.",
50+
name: "Custom host and port should return the provided host:port.",
5151
hostPort: "192.168.100.1:1234",
5252
expectedResult: "192.168.100.1:1234",
5353
},
@@ -87,7 +87,7 @@ func TestServerVersion(t *testing.T) {
8787
serverStub: successServer,
8888
expectedResult: versionResult{
8989
Name: ProgramName,
90-
ClientVersion: clientVersion,
90+
Version: clientVersion,
9191
DaemonVersion: "3.0",
9292
},
9393
hostAndPort: "localhost:8800",
@@ -97,7 +97,7 @@ func TestServerVersion(t *testing.T) {
9797
serverStub: failureServer,
9898
expectedResult: versionResult{
9999
Name: ProgramName,
100-
ClientVersion: clientVersion,
100+
Version: clientVersion,
101101
DaemonVersion: daemonVersion,
102102
},
103103
hostAndPort: unreacheableUrl,
@@ -107,7 +107,7 @@ func TestServerVersion(t *testing.T) {
107107
serverStub: failureServer,
108108
expectedResult: versionResult{
109109
Name: ProgramName,
110-
ClientVersion: clientVersion,
110+
Version: clientVersion,
111111
DaemonVersion: daemonVersion,
112112
},
113113
hostAndPort: unreacheableUrl,
@@ -117,7 +117,7 @@ func TestServerVersion(t *testing.T) {
117117
serverStub: failureInternalServerError,
118118
expectedResult: versionResult{
119119
Name: ProgramName,
120-
ClientVersion: clientVersion,
120+
Version: clientVersion,
121121
DaemonVersion: daemonVersion,
122122
},
123123
hostAndPort: unreacheableUrl,

0 commit comments

Comments
 (0)