|
| 1 | +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU Affero General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU Affero General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU Affero General Public License |
| 14 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | +) |
| 24 | + |
| 25 | +type About struct { |
| 26 | + Commit string `json:"commit"` |
| 27 | + DeploymentID string `json:"deploymentId"` |
| 28 | + LatestVersion any `json:"latestVersion"` |
| 29 | + License string `json:"license"` |
| 30 | + Mode string `json:"mode"` |
| 31 | + Staging string `json:"staging"` |
| 32 | + Store string `json:"store"` |
| 33 | + UpdateAvailable bool `json:"updateAvailable"` |
| 34 | + Version string `json:"version"` |
| 35 | +} |
| 36 | + |
| 37 | +func FetchAbout(client *HTTPClient) (about About, err error) { |
| 38 | + req, err := client.NewRequest("GET", "about", nil) |
| 39 | + if err != nil { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + resp, err := client.client.Do(req) |
| 44 | + if err != nil { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + bytes, err := io.ReadAll(resp.Body) |
| 49 | + if err != nil { |
| 50 | + return |
| 51 | + } |
| 52 | + defer resp.Body.Close() |
| 53 | + |
| 54 | + if resp.StatusCode == 200 { |
| 55 | + err = json.Unmarshal(bytes, &about) |
| 56 | + } else { |
| 57 | + body := string(bytes) |
| 58 | + body = fmt.Sprintf("Request Failed\nStatus Code: %s\nResponse: %s\n", resp.Status, body) |
| 59 | + err = errors.New(body) |
| 60 | + } |
| 61 | + return |
| 62 | +} |
0 commit comments