From c9ecb6c563c8c9ccc4f8e4f000693e827897ffc4 Mon Sep 17 00:00:00 2001 From: Daniel Grunberger <84905812+Daniel-GrunbergerCA@users.noreply.github.com> Date: Fri, 10 Sep 2021 00:57:33 +0300 Subject: [PATCH] Add version handling (#67) * start version handling * add version handling * update latest version check * update build.yaml * erase unused vars * fix build.yaml * fix var name * handle error --- .github/workflows/build.yaml | 6 ++--- cmd/version.go | 49 ++++++++++++++++++++++++++++++++++++ main.go | 18 ++++++++++++- 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 cmd/version.go diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 17592e0d..d0aea435 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -6,7 +6,6 @@ on: pull_request: branches: [ master ] types: [ closed ] - jobs: once: name: Create release @@ -38,9 +37,10 @@ jobs: uses: actions/setup-go@v2 with: go-version: 1.16 - - name: Build - run: mkdir -p build/${{ matrix.os }} && go mod tidy && go build -ldflags "-w -s" -o build/${{ matrix.os }}/kubescape # && md5sum build/${{ matrix.os }}/kubescape > build/${{ matrix.os }}/kubescape.md5 + env: + RELEASE: v1.0.${{ github.run_number }} + run: mkdir -p build/${{ matrix.os }} && go mod tidy && go build -ldflags "-w -s -X github.com/armosec/kubescape/cmd.BuildNumber=$RELEASE" -o build/${{ matrix.os }}/kubescape # && md5sum build/${{ matrix.os }}/kubescape > build/${{ matrix.os }}/kubescape.md5 - name: Upload Release binaries id: upload-release-asset diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 00000000..e78a338f --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,49 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + + "github.com/spf13/cobra" +) + +var BuildNumber string + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Get current version", + Long: ``, + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Println("Your current version is: " + BuildNumber) + return nil + }, +} + +func GetLatestVersion() (string, error) { + latestVersion := "https://api.github.com/repos/armosec/kubescape/releases/latest" + resp, err := http.Get(latestVersion) + if err != nil { + return "", fmt.Errorf("failed to get latest releases from '%s', reason: %s", latestVersion, err.Error()) + } + defer resp.Body.Close() + if resp.StatusCode < 200 || 301 < resp.StatusCode { + return "", fmt.Errorf("failed to download file, status code: %s", resp.Status) + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", fmt.Errorf("failed to read response body from '%s', reason: %s", latestVersion, err.Error()) + } + var data map[string]interface{} + err = json.Unmarshal(body, &data) + if err != nil { + return "", fmt.Errorf("failed to unmarshal response body from '%s', reason: %s", latestVersion, err.Error()) + } + return fmt.Sprintf("%v", data["tag_name"]), nil +} + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/main.go b/main.go index 978bbec4..114f855c 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,23 @@ package main -import "github.com/armosec/kubescape/cmd" +import ( + "fmt" + "os" + + "github.com/armosec/kubescape/cmd" +) func main() { + CheckLatestVersion() cmd.Execute() } + +func CheckLatestVersion() { + latest, err := cmd.GetLatestVersion() + if err != nil { + fmt.Fprintf(os.Stderr, "error: %v\n", err) + } else if latest != cmd.BuildNumber { + fmt.Println("Warning: You are not updated to the latest release: " + latest) + } + +}