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

Remove interactive flag from CheckForNewVersion #35

Merged
merged 1 commit into from
Apr 9, 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
2 changes: 1 addition & 1 deletion cmd/datasetArchiver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {
}

// check for program version only if running interactively
datasetUtils.CheckForNewVersion(client, APP, VERSION, !*nonInteractiveFlag, datasetUtils.StdinUserInput{})
datasetUtils.CheckForNewVersion(client, APP, VERSION)

if *testenvFlag {
APIServer = TEST_API_SERVER
Expand Down
2 changes: 1 addition & 1 deletion cmd/datasetCleaner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func main() {
}

// check for program version only if running interactively
datasetUtils.CheckForNewVersion(client, APP, VERSION, !*nonInteractiveFlag, datasetUtils.StdinUserInput{})
datasetUtils.CheckForNewVersion(client, APP, VERSION)
datasetUtils.CheckForServiceAvailability(client, *testenvFlag, true)

//}
Expand Down
2 changes: 1 addition & 1 deletion cmd/datasetGetProposal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
}

// check for program version only if running interactively
datasetUtils.CheckForNewVersion(client, APP, VERSION, true, datasetUtils.StdinUserInput{})
datasetUtils.CheckForNewVersion(client, APP, VERSION)

if *testenvFlag {
APIServer = TEST_API_SERVER
Expand Down
2 changes: 1 addition & 1 deletion cmd/datasetIngestor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func main() {
}

// check for program version only if running interactively
datasetUtils.CheckForNewVersion(client, APP, VERSION, !*noninteractiveFlag, datasetUtils.StdinUserInput{})
datasetUtils.CheckForNewVersion(client, APP, VERSION)
datasetUtils.CheckForServiceAvailability(client, *testenvFlag, *autoarchiveFlag)

//}
Expand Down
2 changes: 1 addition & 1 deletion cmd/datasetRetriever/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func main() {
return
}

datasetUtils.CheckForNewVersion(client, APP, VERSION, true, datasetUtils.StdinUserInput{})
datasetUtils.CheckForNewVersion(client, APP, VERSION)

var env string
if *testenvFlag {
Expand Down
33 changes: 1 addition & 32 deletions datasetUtils/checkForNewVersion.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ 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) {
func CheckForNewVersion(client *http.Client, APP string, VERSION string) {
// avoid checking for new version in test mode
if os.Getenv("TEST_MODE") == "true" {
return
Expand Down Expand Up @@ -80,16 +80,6 @@ func CheckForNewVersion(client *http.Client, APP string, VERSION string, interac
// Generate the download URL
downloadURL := generateDownloadURL(DeployLocation, latestVersion, osName)

// Split the versions into parts
currentParts := strings.Split(VERSION, ".")
latestParts := strings.Split(latestVersion, ".")

// Convert the major and minor parts to integers
currentMajor, _ := strconv.Atoi(currentParts[0])
currentMinor, _ := strconv.Atoi(currentParts[1])
latestMajor, _ := strconv.Atoi(latestParts[0])
latestMinor, _ := strconv.Atoi(latestParts[1])

if version.Compare(latestVersion, VERSION, ">") {
// Notify an update if the version has changed
log.Println("You should upgrade to a newer version")
Expand All @@ -105,26 +95,5 @@ func CheckForNewVersion(client *http.Client, APP string, VERSION string, interac
} else {
log.Println("Your version of this program is up-to-date")
}
if interactiveFlag && (latestMajor > currentMajor || latestMinor > currentMinor) {
log.Print("Do you want to continue with current version (y/N) ? ")
continueyn, err := userInput.ReadLine()
if err != nil {
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
}

// UserInput is an interface that defines a method to read a line of input. We use this so that we can test interactive mode.
type UserInput interface {
ReadLine() (string, error)
}

type StdinUserInput struct{}

func (StdinUserInput) ReadLine() (string, error) {
reader := bufio.NewReader(os.Stdin)
return reader.ReadString('\n')
}
32 changes: 1 addition & 31 deletions datasetUtils/checkForNewVersion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,48 +82,18 @@ func TestCheckForNewVersion(t *testing.T) {
currentVersion string
mockResponse string
expectedLog string
interactiveFlag bool
userInput string
}{
{
name: "New version available, non-interactive mode",
currentVersion: "0.9.0",
mockResponse: `{"tag_name": "v1.0.0"}`,
expectedLog: "You should upgrade to a newer version",
interactiveFlag: false,
userInput: "y\n",
},
{
name: "No new version available, non-interactive mode",
currentVersion: "1.0.0",
mockResponse: `{"tag_name": "v1.0.0"}`,
expectedLog: "Your version of this program is up-to-date",
interactiveFlag: false,
userInput: "y\n",
},
{
name: "New version available, interactive mode",
currentVersion: "0.9.0",
mockResponse: `{"tag_name": "v1.0.0"}`,
expectedLog: "You should upgrade to a newer version",
interactiveFlag: true,
userInput: "y\n",
},
{
name: "New version available, interactive mode, no upgrade",
currentVersion: "0.9.0",
mockResponse: `{"tag_name": "v1.0.0"}`,
expectedLog: "Warning: Execution stopped, please update the program now.",
interactiveFlag: true,
userInput: "n\n",
},
{
name: "New path available, interactive mode",
currentVersion: "0.9.0",
mockResponse: `{"tag_name": "v0.9.1"}`,
expectedLog: "You should upgrade to a newer version",
interactiveFlag: true,
userInput: "y\n",
},
}

Expand All @@ -145,7 +115,7 @@ func TestCheckForNewVersion(t *testing.T) {
client := server.Client()

// Call CheckForNewVersion
CheckForNewVersion(client, "test", tt.currentVersion, tt.interactiveFlag, MockUserInput{Input: tt.userInput})
CheckForNewVersion(client, "test", tt.currentVersion)

// Check the log output
logOutput := getLogOutput()
Expand Down
Loading