Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced error handling for version check #33

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions cmd/datasetArchiver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions cmd/datasetCleaner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

//}
Expand Down
5 changes: 1 addition & 4 deletions cmd/datasetGetProposal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions cmd/datasetIngestor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

//}
Expand Down
5 changes: 1 addition & 4 deletions cmd/datasetRetriever/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 10 additions & 11 deletions datasetUtils/checkForNewVersion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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.
Expand Down
22 changes: 5 additions & 17 deletions datasetUtils/checkForNewVersion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"
"bytes"
"log"
"errors"
)

func TestFetchLatestVersion(t *testing.T) {
Expand Down Expand Up @@ -83,7 +82,6 @@ func TestCheckForNewVersion(t *testing.T) {
currentVersion string
mockResponse string
expectedLog string
expectedError error
interactiveFlag bool
userInput string
}{
Expand All @@ -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",
},
Expand All @@ -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",
},
Expand All @@ -110,16 +106,14 @@ 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",
},
{
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",
},
Expand All @@ -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",
},
Expand All @@ -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
Expand Down
Loading