diff --git a/Makefile b/Makefile index 247593930..ae7b8fabe 100644 --- a/Makefile +++ b/Makefile @@ -24,11 +24,11 @@ build: ## Build CLI. build-base: ## Build CLI binary (select the platform via GOOS / GOARCH env variables). go build ${GCLFAGS} -ldflags="${LDFLAGS_EXT} \ - -X 'github.com/kubeshark/kubeshark/kubeshark.GitCommitHash=$(COMMIT_HASH)' \ - -X 'github.com/kubeshark/kubeshark/kubeshark.Branch=$(GIT_BRANCH)' \ - -X 'github.com/kubeshark/kubeshark/kubeshark.BuildTimestamp=$(BUILD_TIMESTAMP)' \ - -X 'github.com/kubeshark/kubeshark/kubeshark.Platform=$(SUFFIX)' \ - -X 'github.com/kubeshark/kubeshark/kubeshark.Ver=$(VER)'" \ + -X 'github.com/kubeshark/kubeshark/misc.GitCommitHash=$(COMMIT_HASH)' \ + -X 'github.com/kubeshark/kubeshark/misc.Branch=$(GIT_BRANCH)' \ + -X 'github.com/kubeshark/kubeshark/misc.BuildTimestamp=$(BUILD_TIMESTAMP)' \ + -X 'github.com/kubeshark/kubeshark/misc.Platform=$(SUFFIX)' \ + -X 'github.com/kubeshark/kubeshark/misc.Ver=$(VER)'" \ -o bin/kubeshark_$(SUFFIX) kubeshark.go && \ cd bin && shasum -a 256 kubeshark_${SUFFIX} > kubeshark_${SUFFIX}.sha256 diff --git a/misc/version/versionCheck.go b/misc/version/versionCheck.go index 7d8011231..c835d1249 100644 --- a/misc/version/versionCheck.go +++ b/misc/version/versionCheck.go @@ -3,15 +3,12 @@ package version import ( "context" "fmt" - "io" - "net/http" "os" "runtime" "strings" "time" "github.com/kubeshark/kubeshark/misc" - "github.com/kubeshark/kubeshark/pkg/version" "github.com/kubeshark/kubeshark/utils" "github.com/rs/zerolog/log" @@ -28,60 +25,26 @@ func CheckNewerVersion() { client := github.NewClient(nil) latestRelease, _, err := client.Repositories.GetLatestRelease(context.Background(), misc.Program, misc.Program) if err != nil { - log.Error().Msg("Failed to get latest release.") + log.Error().Msg("Failed to get the latest release.") return } - versionFileUrl := "" - for _, asset := range latestRelease.Assets { - if *asset.Name == "version.txt" { - versionFileUrl = *asset.BrowserDownloadURL - break - } - } - if versionFileUrl == "" { - log.Error().Msg("Version file not found in the latest release.") - return - } - - res, err := http.Get(versionFileUrl) - if err != nil { - log.Error().Err(err).Msg("Failed to get the version file.") - return - } - - data, err := io.ReadAll(res.Body) - res.Body.Close() - if err != nil { - log.Error().Err(err).Msg("Failed to read the version file.") - return - } - gitHubVersion := string(data) - gitHubVersion = gitHubVersion[:len(gitHubVersion)-1] - - greater, err := version.GreaterThen(gitHubVersion, misc.Ver) - if err != nil { - log.Error(). - Str("upstream-version", gitHubVersion). - Str("local-version", misc.Ver). - Msg("Version is invalid!") - return - } + latestVersion := *latestRelease.TagName log.Debug(). - Str("upstream-version", gitHubVersion). + Str("upstream-version", latestVersion). Str("local-version", misc.Ver). Dur("elapsed-time", time.Since(start)). - Msg("Finished version validation.") + Msg("Fetched the latest release:") - if greater { + if misc.Ver != latestVersion { var downloadCommand string if runtime.GOOS == "windows" { downloadCommand = fmt.Sprintf("curl -LO %v/%s.exe", strings.Replace(*latestRelease.HTMLURL, "tag", "download", 1), misc.Program) } else { downloadCommand = fmt.Sprintf("sh <(curl -Ls %s/install)", misc.Website) } - msg := fmt.Sprintf("Update available! %v -> %v run:", misc.Ver, gitHubVersion) + msg := fmt.Sprintf("There is a new release! %v -> %v run:", misc.Ver, latestVersion) log.Warn().Str("command", downloadCommand).Msg(fmt.Sprintf(utils.Yellow, msg)) } } diff --git a/pkg/version/version.go b/pkg/version/version.go deleted file mode 100644 index 2de444c01..000000000 --- a/pkg/version/version.go +++ /dev/null @@ -1,85 +0,0 @@ -package version - -import ( - "fmt" - "regexp" - "strconv" -) - -type Version struct { - Major int - Patch int - Incremental int -} - -func Parse(ver string) (*Version, error) { - re := regexp.MustCompile(`^(\d+)\.(\d+)(?:-\w+(\d+))?$`) - match := re.FindStringSubmatch(ver) - if len(match) != 4 { - return nil, fmt.Errorf("invalid format expected .(-)? %s,", ver) - } - major, err := strconv.Atoi(match[1]) - if err != nil { - return nil, fmt.Errorf("error parsing major int: %s, err %w", match[1], err) - } - patch, err := strconv.Atoi(match[2]) - if err != nil { - return nil, fmt.Errorf("error parsing patch int: %s, err %w", match[2], err) - } - - if match[3] == "" { - return &Version{Major: major, Patch: patch, Incremental: -1}, nil - } - - inc, err := strconv.Atoi(match[3]) - if err != nil { - return nil, fmt.Errorf("Error parsing incremental int: %s, err %w", match[3], err) - } - return &Version{Major: major, Patch: patch, Incremental: inc}, nil - -} - -func AreEquals(first string, second string) (bool, error) { - firstVer, err := Parse(first) - if err != nil { - return false, fmt.Errorf("Failed parsing fist version: %s, error: %w", first, err) - } - secondVer, err := Parse(second) - if err != nil { - return false, fmt.Errorf("Failed parsing second version: %s, error: %w", second, err) - } - - return *firstVer == *secondVer, nil -} - -func GreaterThen(first string, second string) (bool, error) { - firstVer, err := Parse(first) - if err != nil { - return false, fmt.Errorf("Failed parsing fist version: %s, error: %w", first, err) - } - secondVer, err := Parse(second) - if err != nil { - return false, fmt.Errorf("Failed parsing second version: %s, error: %w", second, err) - } - - if firstVer.Major > secondVer.Major { - return true, nil - } else if firstVer.Major < secondVer.Major { - return false, nil - } - - if firstVer.Patch > secondVer.Patch { - return true, nil - } else if firstVer.Patch < secondVer.Patch { - return false, nil - } - - if firstVer.Incremental == -1 && secondVer.Incremental > -1 { - return true, nil - } - - if firstVer.Incremental > secondVer.Incremental { - return true, nil - } - return false, nil -} diff --git a/pkg/version/version_test.go b/pkg/version/version_test.go deleted file mode 100644 index a08fee0ba..000000000 --- a/pkg/version/version_test.go +++ /dev/null @@ -1,104 +0,0 @@ -package version - -import ( - "testing" -) - -func TestEqualsEquality(t *testing.T) { - tests := []struct { - Name string - First string - Second string - }{ - {Name: "major", First: "1.0", Second: "1.0"}, - {Name: "patch", First: "1.1", Second: "1.1"}, - {Name: "incremental", First: "1.0-dev0", Second: "1.0-dev0"}, - } - for _, test := range tests { - t.Run(test.Name, func(t *testing.T) { - if equal, _ := AreEquals(test.First, test.Second); !equal { - t.Fatalf("Expected %s == %s", test.First, test.Second) - } - }) - } -} - -func TestEqualsInvalidVersion(t *testing.T) { - tests := []struct { - Name string - First string - Second string - }{ - {Name: "first semver", First: "1.0.0", Second: "1.0"}, - {Name: "second semver", First: "1.1", Second: "1.1.0"}, - {Name: "incremental invalid", First: "1.0-dev0de", Second: "1.0-dev0"}, - } - for _, test := range tests { - t.Run(test.Name, func(t *testing.T) { - if _, err := AreEquals(test.First, test.Second); err == nil { - t.Fatalf("Expected error") - } - }) - } -} - -func TestEqualsNoEquality(t *testing.T) { - tests := []struct { - Name string - First string - Second string - }{ - {Name: "major", First: "1.0", Second: "2.0"}, - {Name: "patch", First: "1.0", Second: "1.1"}, - {Name: "incremental", First: "1.0-dev2", Second: "1.0-dev3"}, - } - for _, test := range tests { - t.Run(test.Name, func(t *testing.T) { - if equal, _ := AreEquals(test.First, test.Second); equal { - t.Fatalf("Expected %s != %s", test.First, test.Second) - } - }) - } -} - -func TestGreaterThenGreater(t *testing.T) { - tests := []struct { - Name string - First string - Second string - }{ - {Name: "major", First: "2.0", Second: "1.0"}, - {Name: "patch", First: "1.1", Second: "1.0"}, - {Name: "incremental", First: "1.0-dev1", Second: "1.0-dev0"}, - {Name: "major vs incremental", First: "1.0", Second: "1.0-dev1"}, - } - for _, test := range tests { - t.Run(test.Name, func(t *testing.T) { - if greater, _ := GreaterThen(test.First, test.Second); !greater { - t.Fatalf("Expected %s > %s", test.First, test.Second) - } - }) - } -} - -func TestGreaterThenLessThen(t *testing.T) { - tests := []struct { - Name string - First string - Second string - }{ - {Name: "major", First: "1.0", Second: "2.0"}, - {Name: "major equals", First: "1.0", Second: "1.0"}, - {Name: "patch", First: "1.0", Second: "1.1"}, - {Name: "patch equals", First: "1.1", Second: "1.1"}, - {Name: "incremental", First: "1.0-dev0", Second: "1.0-dev1"}, - {Name: "incremental equals", First: "1.0-dev0", Second: "1.0-dev0"}, - } - for _, test := range tests { - t.Run(test.Name, func(t *testing.T) { - if greater, _ := GreaterThen(test.First, test.Second); greater { - t.Fatalf("Expected %s < %s", test.First, test.Second) - } - }) - } -}