From 817002b35bb27cdfddfa2409e7902628ae95bbee Mon Sep 17 00:00:00 2001 From: Ali Vahdati <3798865+kavir1698@users.noreply.github.com> Date: Mon, 8 Apr 2024 10:59:29 +0200 Subject: [PATCH] Make checkForNewVersion function not stop the code (#33) --- cmd/datasetArchiver/main.go | 5 +---- cmd/datasetCleaner/main.go | 5 +---- cmd/datasetGetProposal/main.go | 5 +---- cmd/datasetIngestor/main.go | 5 +---- cmd/datasetRetriever/main.go | 5 +---- datasetUtils/checkForNewVersion.go | 21 ++++++++++----------- datasetUtils/checkForNewVersion_test.go | 22 +++++----------------- 7 files changed, 20 insertions(+), 48 deletions(-) diff --git a/cmd/datasetArchiver/main.go b/cmd/datasetArchiver/main.go index cfeefbf..0f10a05 100644 --- a/cmd/datasetArchiver/main.go +++ b/cmd/datasetArchiver/main.go @@ -59,10 +59,7 @@ func main() { } // check for program version only if running interactively - err := datasetUtils.CheckForNewVersion(client, APP, VERSION, !*nonInteractiveFlag, datasetUtils.StdinUserInput{}) - if err != nil { - log.Fatalf("Error checking for new version: %v", err) - } + datasetUtils.CheckForNewVersion(client, APP, VERSION, !*nonInteractiveFlag, datasetUtils.StdinUserInput{}) if *testenvFlag { APIServer = TEST_API_SERVER diff --git a/cmd/datasetCleaner/main.go b/cmd/datasetCleaner/main.go index ebdcaf0..2a40c94 100644 --- a/cmd/datasetCleaner/main.go +++ b/cmd/datasetCleaner/main.go @@ -83,10 +83,7 @@ func main() { } // check for program version only if running interactively - err := datasetUtils.CheckForNewVersion(client, APP, VERSION, !*nonInteractiveFlag, datasetUtils.StdinUserInput{}) - if err != nil { - log.Fatalf("Error checking for new version: %v", err) - } + datasetUtils.CheckForNewVersion(client, APP, VERSION, !*nonInteractiveFlag, datasetUtils.StdinUserInput{}) datasetUtils.CheckForServiceAvailability(client, *testenvFlag, true) //} diff --git a/cmd/datasetGetProposal/main.go b/cmd/datasetGetProposal/main.go index af0ae46..2c9e0be 100644 --- a/cmd/datasetGetProposal/main.go +++ b/cmd/datasetGetProposal/main.go @@ -54,10 +54,7 @@ func main() { } // check for program version only if running interactively - err := datasetUtils.CheckForNewVersion(client, APP, VERSION, true, datasetUtils.StdinUserInput{}) - if err != nil { - log.Fatalf("Error checking for new version: %v", err) - } + datasetUtils.CheckForNewVersion(client, APP, VERSION, true, datasetUtils.StdinUserInput{}) if *testenvFlag { APIServer = TEST_API_SERVER diff --git a/cmd/datasetIngestor/main.go b/cmd/datasetIngestor/main.go index 6957798..c0c9acc 100644 --- a/cmd/datasetIngestor/main.go +++ b/cmd/datasetIngestor/main.go @@ -141,10 +141,7 @@ func main() { } // check for program version only if running interactively - err := datasetUtils.CheckForNewVersion(client, APP, VERSION, !*noninteractiveFlag, datasetUtils.StdinUserInput{}) - if err != nil { - log.Fatalf("Error checking for new version: %v", err) - } + datasetUtils.CheckForNewVersion(client, APP, VERSION, !*noninteractiveFlag, datasetUtils.StdinUserInput{}) datasetUtils.CheckForServiceAvailability(client, *testenvFlag, *autoarchiveFlag) //} diff --git a/cmd/datasetRetriever/main.go b/cmd/datasetRetriever/main.go index d4f670c..481a08f 100644 --- a/cmd/datasetRetriever/main.go +++ b/cmd/datasetRetriever/main.go @@ -74,10 +74,7 @@ func main() { return } - err := datasetUtils.CheckForNewVersion(client, APP, VERSION, true, datasetUtils.StdinUserInput{}) - if err != nil { - log.Fatalf("Error checking for new version: %v", err) - } + datasetUtils.CheckForNewVersion(client, APP, VERSION, true, datasetUtils.StdinUserInput{}) var env string if *testenvFlag { diff --git a/datasetUtils/checkForNewVersion.go b/datasetUtils/checkForNewVersion.go index fb46d3c..97edbb7 100644 --- a/datasetUtils/checkForNewVersion.go +++ b/datasetUtils/checkForNewVersion.go @@ -52,25 +52,25 @@ func generateDownloadURL(deployLocation, latestVersion, osName string) string { return fmt.Sprintf("%s/v%s/scicat-cli_.%s_%s_x86_64.tar.gz", deployLocation, latestVersion, latestVersion, strings.Title(osName)) } -func CheckForNewVersion(client *http.Client, APP string, VERSION string, interactiveFlag bool, userInput UserInput) error { +func CheckForNewVersion(client *http.Client, APP string, VERSION string, interactiveFlag bool, userInput UserInput) { // avoid checking for new version in test mode if os.Getenv("TEST_MODE") == "true" { - return nil + return } latestVersion, err := fetchLatestVersion(client) if err != nil { - log.Printf("Can not find info about latest version for this program: %s\n", err) - return err + log.Printf("Warning: Can not find info about latest version for this program: %s\n", err) + return } latestVersion = strings.TrimPrefix(latestVersion, "v") _, err = strconv.Atoi(strings.Split(latestVersion, ".")[0]) if err != nil { - log.Fatalf("Illegal latest version number:%v", latestVersion) + log.Printf("Warning: Illegal latest version number:%v\n", latestVersion) } _, err = strconv.Atoi(strings.Split(VERSION, ".")[0]) if err != nil { - log.Fatalf("Illegal version number:%v", VERSION) + log.Printf("Warning: Illegal version number:%v\n", VERSION) } log.Printf("Latest version: %s", latestVersion) @@ -109,13 +109,12 @@ func CheckForNewVersion(client *http.Client, APP string, VERSION string, interac log.Print("Do you want to continue with current version (y/N) ? ") continueyn, err := userInput.ReadLine() if err != nil { - return fmt.Errorf("failed to read user input: %v", err) - } - if strings.TrimSpace(continueyn) != "y" { - return fmt.Errorf("Execution stopped, please update the program now.") + log.Printf("Warning: Failed to read user input: %v\n", err) + } else if strings.TrimSpace(continueyn) != "y" { + log.Println("Warning: Execution stopped, please update the program now.") } } - return nil + return } // UserInput is an interface that defines a method to read a line of input. We use this so that we can test interactive mode. diff --git a/datasetUtils/checkForNewVersion_test.go b/datasetUtils/checkForNewVersion_test.go index a130f64..aa0141c 100644 --- a/datasetUtils/checkForNewVersion_test.go +++ b/datasetUtils/checkForNewVersion_test.go @@ -7,7 +7,6 @@ import ( "testing" "bytes" "log" - "errors" ) func TestFetchLatestVersion(t *testing.T) { @@ -83,7 +82,6 @@ func TestCheckForNewVersion(t *testing.T) { currentVersion string mockResponse string expectedLog string - expectedError error interactiveFlag bool userInput string }{ @@ -92,7 +90,6 @@ func TestCheckForNewVersion(t *testing.T) { currentVersion: "0.9.0", mockResponse: `{"tag_name": "v1.0.0"}`, expectedLog: "You should upgrade to a newer version", - expectedError: nil, interactiveFlag: false, userInput: "y\n", }, @@ -101,7 +98,6 @@ func TestCheckForNewVersion(t *testing.T) { currentVersion: "1.0.0", mockResponse: `{"tag_name": "v1.0.0"}`, expectedLog: "Your version of this program is up-to-date", - expectedError: nil, interactiveFlag: false, userInput: "y\n", }, @@ -110,7 +106,6 @@ func TestCheckForNewVersion(t *testing.T) { currentVersion: "0.9.0", mockResponse: `{"tag_name": "v1.0.0"}`, expectedLog: "You should upgrade to a newer version", - expectedError: nil, interactiveFlag: true, userInput: "y\n", }, @@ -118,8 +113,7 @@ func TestCheckForNewVersion(t *testing.T) { name: "New version available, interactive mode, no upgrade", currentVersion: "0.9.0", mockResponse: `{"tag_name": "v1.0.0"}`, - expectedLog: "Execution stopped, please update the program now.", - expectedError: errors.New("Execution stopped, please update the program now."), + expectedLog: "Warning: Execution stopped, please update the program now.", interactiveFlag: true, userInput: "n\n", }, @@ -128,7 +122,6 @@ func TestCheckForNewVersion(t *testing.T) { currentVersion: "0.9.0", mockResponse: `{"tag_name": "v0.9.1"}`, expectedLog: "You should upgrade to a newer version", - expectedError: nil, interactiveFlag: true, userInput: "y\n", }, @@ -152,17 +145,12 @@ func TestCheckForNewVersion(t *testing.T) { client := server.Client() // Call CheckForNewVersion - err := CheckForNewVersion(client, "test", tt.currentVersion, tt.interactiveFlag, MockUserInput{Input: tt.userInput}) - if err != nil && err.Error() != tt.expectedError.Error() { - t.Errorf("got error %v, want %v", err, tt.expectedLog) - } + CheckForNewVersion(client, "test", tt.currentVersion, tt.interactiveFlag, MockUserInput{Input: tt.userInput}) // Check the log output - if tt.userInput == "y\n" { - logOutput := getLogOutput() - if !strings.Contains(logOutput, tt.expectedLog) { - t.Errorf("Expected log message not found: %s", logOutput) - } + logOutput := getLogOutput() + if !strings.Contains(logOutput, tt.expectedLog) { + t.Errorf("Expected log message not found: %s", logOutput) } // Clear the log buffer after each test