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

Use the nonInteractiveFlag flag in CheckForNewVersion function calls #29

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

// check for program version only if running interactively
datasetUtils.CheckForNewVersion(client, APP, VERSION, true)
err := datasetUtils.CheckForNewVersion(client, APP, VERSION, !*nonInteractiveFlag, datasetUtils.StdinUserInput{})
if err != nil {
log.Fatalf("Error checking for new version: %v", err)
}

if *testenvFlag {
APIServer = TEST_API_SERVER
Expand Down
6 changes: 4 additions & 2 deletions cmd/datasetCleaner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ func main() {
}

// check for program version only if running interactively

datasetUtils.CheckForNewVersion(client, APP, VERSION, true)
err := datasetUtils.CheckForNewVersion(client, APP, VERSION, !*nonInteractiveFlag, datasetUtils.StdinUserInput{})
if err != nil {
log.Fatalf("Error checking for new version: %v", err)
}
datasetUtils.CheckForServiceAvailability(client, *testenvFlag, true)

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

// check for program version only if running interactively
datasetUtils.CheckForNewVersion(client, APP, VERSION, true)
err := datasetUtils.CheckForNewVersion(client, APP, VERSION, true, datasetUtils.StdinUserInput{})
if err != nil {
log.Fatalf("Error checking for new version: %v", err)
}

if *testenvFlag {
APIServer = TEST_API_SERVER
Expand Down
9 changes: 6 additions & 3 deletions cmd/datasetIngestor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func main() {
devenvFlag := flag.Bool("devenv", false, "Use development environment instead of production environment (developers only)")
localenvFlag := flag.Bool("localenv", false, "Use local environment instead of production environment (developers only)")
tunnelenvFlag := flag.Bool("tunnelenv", false, "Use tunneled API server at port 5443 to access development instance (developers only)")
noninteractiveFlag := flag.Bool("noninteractive", false, "If set no questions will be asked and the default settings for all undefined flags will be assumed")
noninteractiveFlag := flag.Bool("noninteractive", false, "If true, no questions will be asked and the default settings for all undefined flags will be assumed")
userpass := flag.String("user", "", "Defines optional username:password string. This can be used both for access to the data catalog API and for access to the intermediate storage server for the decentral use case")
token := flag.String("token", "", "Defines API token for access to the data catalog API. It is now mandatory for normal user accounts, but optional for functional accounts. It takes precedence over username/pw.")
copyFlag := flag.Bool("copy", false, "Defines if files should be copied from your local system to a central server before ingest (i.e. your data is not centrally available and therefore needs to be copied ='decentral' case). copyFlag has higher priority than nocopyFlag. If neither flag is defined the tool will try to make the best guess.")
Expand Down Expand Up @@ -138,9 +138,12 @@ func main() {
fmt.Printf("%s\n", VERSION)
return
}
// check for program version only if running interactively

datasetUtils.CheckForNewVersion(client, APP, VERSION, !(*noninteractiveFlag))
// 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.CheckForServiceAvailability(client, *testenvFlag, *autoarchiveFlag)

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

datasetUtils.CheckForNewVersion(client, APP, VERSION, true)
err := datasetUtils.CheckForNewVersion(client, APP, VERSION, true, datasetUtils.StdinUserInput{})
if err != nil {
log.Fatalf("Error checking for new version: %v", err)
}

var env string
if *testenvFlag {
Expand Down
32 changes: 22 additions & 10 deletions datasetUtils/checkForNewVersion.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ 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) {
func CheckForNewVersion(client *http.Client, APP string, VERSION string, interactiveFlag bool, userInput UserInput) error {
latestVersion, err := fetchLatestVersion(client)
if err != nil {
log.Printf("Can not find info about latest version for this program: %s\n", err)
return
return err
}

latestVersion = strings.TrimPrefix(latestVersion, "v")
Expand Down Expand Up @@ -88,15 +88,27 @@ func CheckForNewVersion(client *http.Client, APP string, VERSION string, interac
log.Printf("Browser: %s\nCommand: curl -L -O %s; tar xzf scicat-cli_.%s_%s_x86_64.tar.gz; cd scicat-cli; chmod +x %s\n", downloadURL, downloadURL, latestVersion, strings.Title(osName), APP)
}

if interactiveFlag {
log.Print("Do you want to continue with current version (y/N) ? ")
scanner.Scan()
continueyn := scanner.Text()
if continueyn != "y" {
log.Fatalf("Execution stopped, please update program now.\n")
}
}
if interactiveFlag {
log.Print("Do you want to continue with current version (y/N) ? ")
continueyn, _ := userInput.ReadLine()
if continueyn != "y\n" {
return fmt.Errorf("Execution stopped, please update the program now.")
}
}
} else {
log.Println("Your version of this program is up-to-date")
}
return nil
}

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

func TestFetchLatestVersion(t *testing.T) {
Expand Down Expand Up @@ -59,33 +60,68 @@ func TestGenerateDownloadURL(t *testing.T) {
},
}

for _, testCase := range testCases {
actualURL := generateDownloadURL(deployLocation, latestVersion, testCase.osName)

if actualURL != testCase.expectedURL {
t.Errorf("Expected URL to be %s, but got %s", testCase.expectedURL, actualURL)
}
for _, testCase := range testCases {
actualURL := generateDownloadURL(deployLocation, latestVersion, testCase.osName)

if actualURL != testCase.expectedURL {
t.Errorf("Expected URL to be %s, but got %s", testCase.expectedURL, actualURL)
}
}
}

type MockUserInput struct {
Input string
}

func (m MockUserInput) ReadLine() (string, error) {
return m.Input, nil
}

func TestCheckForNewVersion(t *testing.T) {
tests := []struct {
name string
currentVersion string
mockResponse string
expectedLog string
expectedError error
interactiveFlag bool
userInput string
}{
{
name: "New version available",
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",
expectedError: nil,
interactiveFlag: false,
userInput: "y\n",
},
{
name: "No new version available",
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",
expectedError: nil,
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",
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."),
interactiveFlag: true,
userInput: "n\n",
},
}

Expand All @@ -107,12 +143,17 @@ func TestCheckForNewVersion(t *testing.T) {
client := server.Client()

// Call CheckForNewVersion
CheckForNewVersion(client, "test", tt.currentVersion, false)
err := CheckForNewVersion(client, "test", tt.currentVersion, tt.interactiveFlag, MockUserInput{Input: tt.userInput})
if err != nil && err.Error() != tt.expectedLog {
t.Errorf("got error %v, want %v", err, tt.expectedLog)
}

// Check the log output
logOutput := getLogOutput()
if !strings.Contains(logOutput, tt.expectedLog) {
t.Errorf("Expected log message not found: %s", logOutput)
if tt.userInput == "y\n" {
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