diff --git a/datasetIngestor/syncDataToFileserver_unix.go b/datasetIngestor/syncDataToFileserver_unix.go index 9ebf5ea..7a761f7 100644 --- a/datasetIngestor/syncDataToFileserver_unix.go +++ b/datasetIngestor/syncDataToFileserver_unix.go @@ -10,6 +10,7 @@ import ( "os/exec" "strings" version "github.com/mcuadros/go-version" + "regexp" ) // functionality needed for "de-central" data @@ -44,7 +45,7 @@ func SyncDataToFileserver(datasetId string, user map[string]string, RSYNCServer // Show rsync's output rsyncCmd.Stderr = os.Stderr - log.Printf("Running %v.\n", rsyncCmd.Args) + log.Printf("Running: %v.\n", rsyncCmd.Args) err = rsyncCmd.Run() return err } @@ -56,7 +57,15 @@ func getRsyncVersion() (string, error) { if err != nil { return "", err } - version := strings.Split(string(output), "\n")[0] - versionNumber := strings.Split(version, " ")[2] + version := string(output) + + // Use a regular expression to find the version number. + // It will match the first occurrence of a string in the format "x.y.z" in the `version` string, where "x", "y", and "z" are one or more digits. + re := regexp.MustCompile(`\d+\.\d+\.\d+`) + versionNumber := re.FindString(version) + if versionNumber == "" { + return "", fmt.Errorf("could not find version number in rsync version string: %s", version) + } + return versionNumber, nil } diff --git a/datasetIngestor/syncDataToFileserver_unix_test.go b/datasetIngestor/syncDataToFileserver_unix_test.go index 5abc870..456f7f9 100644 --- a/datasetIngestor/syncDataToFileserver_unix_test.go +++ b/datasetIngestor/syncDataToFileserver_unix_test.go @@ -4,6 +4,7 @@ package datasetIngestor import ( "testing" + "regexp" ) func TestGetRsyncVersion(t *testing.T) { @@ -13,5 +14,10 @@ func TestGetRsyncVersion(t *testing.T) { } if version == "" { t.Error("getRsyncVersion() returned an empty string") + } else { + match, _ := regexp.MatchString(`^\d{1,2}\.\d{1,2}\.\d{1,2}$`, version) + if !match { + t.Error("getRsyncVersion() returned wrong version string format: ", version) + } } } diff --git a/datasetUtils/getAvailableDatasets.go b/datasetUtils/getAvailableDatasets.go index 87ad763..c1c43b7 100644 --- a/datasetUtils/getAvailableDatasets.go +++ b/datasetUtils/getAvailableDatasets.go @@ -5,6 +5,8 @@ import ( "log" "os/exec" "strings" + version "github.com/mcuadros/go-version" + "regexp" ) func GetAvailableDatasets(username string, RSYNCServer string, singleDatasetId string) []string { @@ -21,7 +23,21 @@ func GetAvailableDatasets(username string, RSYNCServer string, singleDatasetId s fmt.Printf("====== (only datasets highlighted in green will be retrieved)\n\n") fmt.Printf("====== If you can not find the dataset in this listing: may be you forgot\n") fmt.Printf("====== to start the necessary retrieve job from the the data catalog first ?\n\n") - cmd := exec.Command("rsync", "-e", "ssh -q", "--list-only", username+"@"+RSYNCServer+":retrieve/") + + // Get rsync version + versionNumber, err := getRsyncVersion() + if err != nil { + log.Fatal("Error getting rsync version: ", err) + } + + // Check rsync version and adjust command accordingly + var cmd *exec.Cmd + if version.Compare(versionNumber, "3.2.3", ">=") { + cmd = exec.Command("rsync", "-e", "ssh", "--list-only", username+"@"+RSYNCServer+":retrieve/") + } else { + cmd = exec.Command("rsync", "-e", "ssh -q", "--list-only", username+"@"+RSYNCServer+":retrieve/") + } + out, err := cmd.Output() if err != nil { log.Printf("Running %v.\n", cmd.Args) @@ -43,3 +59,22 @@ func GetAvailableDatasets(username string, RSYNCServer string, singleDatasetId s } return datasetList } + +// Get rsync version +func getRsyncVersion() (string, error) { + cmd := exec.Command("/usr/bin/rsync", "--version") + output, err := cmd.Output() + if err != nil { + return "", err + } + version := string(output) + + // Use a regular expression to find the version number + re := regexp.MustCompile(`\d+\.\d+\.\d+`) + versionNumber := re.FindString(version) + if versionNumber == "" { + return "", fmt.Errorf("could not find version number in rsync version string: %s", version) + } + + return versionNumber, nil +}