Skip to content

Commit

Permalink
Fix the rsync version check and test
Browse files Browse the repository at this point in the history
  • Loading branch information
kavir1698 committed Apr 22, 2024
1 parent a523cab commit a2f176b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
15 changes: 12 additions & 3 deletions datasetIngestor/syncDataToFileserver_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/exec"
"strings"
version "github.com/mcuadros/go-version"
"regexp"
)

// functionality needed for "de-central" data
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
6 changes: 6 additions & 0 deletions datasetIngestor/syncDataToFileserver_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package datasetIngestor

import (
"testing"
"regexp"
)

func TestGetRsyncVersion(t *testing.T) {
Expand All @@ -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)
}
}
}
37 changes: 36 additions & 1 deletion datasetUtils/getAvailableDatasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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
}

0 comments on commit a2f176b

Please sign in to comment.