|
16 | 16 | package version |
17 | 17 |
|
18 | 18 | import ( |
| 19 | + "encoding/json" |
19 | 20 | "fmt" |
| 21 | + "io" |
| 22 | + "net" |
| 23 | + "net/http" |
| 24 | + "net/url" |
| 25 | + "time" |
20 | 26 |
|
21 | 27 | "github.com/spf13/cobra" |
22 | 28 |
|
23 | 29 | "github.com/arduino/arduino-app-cli/cmd/feedback" |
| 30 | + "github.com/arduino/arduino-app-cli/cmd/i18n" |
24 | 31 | ) |
25 | 32 |
|
26 | | -func NewVersionCmd(version string) *cobra.Command { |
| 33 | +// The actual listening address for the daemon |
| 34 | +// is defined in the installation package |
| 35 | +const ( |
| 36 | + DefaultHostname = "localhost" |
| 37 | + DefaultPort = "8800" |
| 38 | +) |
| 39 | + |
| 40 | +func NewVersionCmd(clientVersion string) *cobra.Command { |
27 | 41 | cmd := &cobra.Command{ |
28 | 42 | Use: "version", |
29 | | - Short: "Print the version number of Arduino App CLI", |
| 43 | + Short: "Print the client and server version numbers for the Arduino App CLI.", |
30 | 44 | Run: func(cmd *cobra.Command, args []string) { |
31 | | - feedback.PrintResult(versionResult{ |
32 | | - AppName: "Arduino App CLI", |
33 | | - Version: version, |
34 | | - }) |
| 45 | + host, _ := cmd.Flags().GetString("host") |
| 46 | + |
| 47 | + versionHandler(clientVersion, host) |
35 | 48 | }, |
36 | 49 | } |
| 50 | + cmd.Flags().String("host", fmt.Sprintf("%s:%s", DefaultHostname, DefaultPort), |
| 51 | + "The daemon network address [host]:[port]") |
37 | 52 | return cmd |
38 | 53 | } |
39 | 54 |
|
40 | | -type versionResult struct { |
41 | | - AppName string `json:"appName"` |
| 55 | +func versionHandler(clientVersion string, host string) { |
| 56 | + httpClient := http.Client{ |
| 57 | + Timeout: time.Second, |
| 58 | + } |
| 59 | + result := doVersionHandler(httpClient, clientVersion, host) |
| 60 | + feedback.PrintResult(result) |
| 61 | +} |
| 62 | + |
| 63 | +func doVersionHandler(httpClient http.Client, clientVersion string, host string) versionResult { |
| 64 | + url, err := getValidOrDefaultUrl(host) |
| 65 | + if err != nil { |
| 66 | + feedback.Fatal(i18n.Tr("Error: invalid host:port format"), feedback.ErrBadArgument) |
| 67 | + } |
| 68 | + |
| 69 | + serverVersion, err := getServerVersion(httpClient, url) |
| 70 | + if err != nil { |
| 71 | + serverVersion = fmt.Sprintf("n/a (cannot connect to the server %s://%s)", url.Scheme, url.Host) |
| 72 | + } |
| 73 | + |
| 74 | + return versionResult{ |
| 75 | + ClientVersion: clientVersion, |
| 76 | + ServerVersion: serverVersion, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func getValidOrDefaultUrl(hostPort string) (url.URL, error) { |
| 81 | + host := DefaultHostname |
| 82 | + port := DefaultPort |
| 83 | + |
| 84 | + if hostPort != "" { |
| 85 | + h, p, err := net.SplitHostPort(hostPort) |
| 86 | + if err != nil { |
| 87 | + return url.URL{}, err |
| 88 | + } |
| 89 | + if h != "" { |
| 90 | + host = h |
| 91 | + } |
| 92 | + if p != "" { |
| 93 | + port = p |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + hostAndPort := net.JoinHostPort(host, port) |
| 99 | + |
| 100 | + u := url.URL{ |
| 101 | + Scheme: "http", |
| 102 | + Host: hostAndPort, |
| 103 | + Path: "/v1/version", |
| 104 | + } |
| 105 | + |
| 106 | + return u, nil |
| 107 | +} |
| 108 | + |
| 109 | +func getServerVersion(httpClient http.Client, url url.URL) (string, error) { |
| 110 | + resp, err := httpClient.Get(url.String()) |
| 111 | + if err != nil { |
| 112 | + return "", err |
| 113 | + } |
| 114 | + defer resp.Body.Close() |
| 115 | + |
| 116 | + if resp.StatusCode != http.StatusOK { |
| 117 | + return "", fmt.Errorf("request failed with status %d", resp.StatusCode) |
| 118 | + } |
| 119 | + |
| 120 | + body, err := io.ReadAll(resp.Body) |
| 121 | + if err != nil { |
| 122 | + return "", err |
| 123 | + } |
| 124 | + |
| 125 | + var serverResponse serverVersionResponse |
| 126 | + if err := json.Unmarshal(body, &serverResponse); err != nil { |
| 127 | + return "", err |
| 128 | + } |
| 129 | + |
| 130 | + return serverResponse.Version, nil |
| 131 | +} |
| 132 | + |
| 133 | +type serverVersionResponse struct { |
42 | 134 | Version string `json:"version"` |
43 | 135 | } |
44 | 136 |
|
| 137 | +type versionResult struct { |
| 138 | + ClientVersion string `json:"version"` |
| 139 | + ServerVersion string `json:"serverVersion"` |
| 140 | +} |
| 141 | + |
45 | 142 | func (r versionResult) String() string { |
46 | | - return fmt.Sprintf("%s v%s", r.AppName, r.Version) |
| 143 | + return fmt.Sprintf("client: %s\nserver: %s", |
| 144 | + r.ClientVersion, r.ServerVersion) |
47 | 145 | } |
48 | 146 |
|
49 | 147 | func (r versionResult) Data() interface{} { |
|
0 commit comments