Version 1.0.0
This commit is contained in:
parent
4b7701ea0d
commit
815b15840c
9
CorrectPath.go
Normal file
9
CorrectPath.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func correctPath(path string) string {
|
||||||
|
if path[len(path)-1:] == `/` || path[len(path)-1:] == `\` {
|
||||||
|
return path[:len(path)-1]
|
||||||
|
} else {
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
}
|
34
Main.go
34
Main.go
@ -12,7 +12,7 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Show the current version:
|
// Show the current version:
|
||||||
fmt.Println(`Sync v1.0.0`)
|
log.Println(`Sync v1.0.0`)
|
||||||
|
|
||||||
// Allow Go to use all CPUs:
|
// Allow Go to use all CPUs:
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
@ -26,6 +26,21 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Should I use the current working dir?
|
||||||
|
if localDir == `.` {
|
||||||
|
if currentWD, currentWDError := os.Getwd(); currentWDError != nil {
|
||||||
|
log.Println("Cannot use the current working directory as local directory: " + currentWDError.Error())
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
log.Println("I use the current working directory as local directory: " + currentWD)
|
||||||
|
localDir = currentWD
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove trailing separators from both directories
|
||||||
|
localDir = correctPath(localDir)
|
||||||
|
remoteDir = correctPath(remoteDir)
|
||||||
|
|
||||||
// Check if local dir exist
|
// Check if local dir exist
|
||||||
if dirInfo, dirError := os.Stat(localDir); dirError != nil {
|
if dirInfo, dirError := os.Stat(localDir); dirError != nil {
|
||||||
log.Println("There is an error with the local directory: " + dirError.Error())
|
log.Println("There is an error with the local directory: " + dirError.Error())
|
||||||
@ -41,13 +56,26 @@ func main() {
|
|||||||
for true {
|
for true {
|
||||||
if password == `` {
|
if password == `` {
|
||||||
// Promt for the password:
|
// Promt for the password:
|
||||||
fmt.Println(`Please provide the password for the connection:`)
|
fmt.Print(`Please provide the password for the connection: `)
|
||||||
fmt.Scanln(&password)
|
fmt.Scanln(&password)
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Give some information about the state
|
||||||
|
if supervised {
|
||||||
|
log.Println("I use the supervised mode.")
|
||||||
|
} else {
|
||||||
|
log.Println("I do not use the supervised mode.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if pushOnly {
|
||||||
|
log.Println("I use the push only mode i.e. backup mode. Any remote change will be ignored.")
|
||||||
|
} else {
|
||||||
|
log.Println("I use the full mode and consider also remote changes.")
|
||||||
|
}
|
||||||
|
|
||||||
// Create the SSH configuration:
|
// Create the SSH configuration:
|
||||||
Sync.SetPassword4Callback(password)
|
Sync.SetPassword4Callback(password)
|
||||||
config := &ssh.ClientConfig{
|
config := &ssh.ClientConfig{
|
||||||
@ -67,6 +95,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
defer ssh.Close()
|
defer ssh.Close()
|
||||||
Sync.Synchronise(ssh, supervised, localDir, remoteDir)
|
Sync.Synchronise(ssh, supervised, pushOnly, localDir, remoteDir)
|
||||||
log.Println("Synchronising done.")
|
log.Println("Synchronising done.")
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,9 @@ func readFlags() {
|
|||||||
flag.StringVar(&serverAddrString, `server`, `127.0.0.1:22`, `The (remote) SSH server, e.g. 'my.host.com', 'my.host.com:22', '127.0.0.1:22', 'localhost:22'.`)
|
flag.StringVar(&serverAddrString, `server`, `127.0.0.1:22`, `The (remote) SSH server, e.g. 'my.host.com', 'my.host.com:22', '127.0.0.1:22', 'localhost:22'.`)
|
||||||
flag.StringVar(&username, `user`, `username`, `The user's name for the SSH server.`)
|
flag.StringVar(&username, `user`, `username`, `The user's name for the SSH server.`)
|
||||||
flag.StringVar(&password, `pwd`, ``, `The user's password for the SSH server. You can omit these argument. Thus, the program asks for the password on demand.`)
|
flag.StringVar(&password, `pwd`, ``, `The user's password for the SSH server. You can omit these argument. Thus, the program asks for the password on demand.`)
|
||||||
flag.StringVar(&localDir, `localDir`, ``, `The local directory which should be synced.`)
|
flag.StringVar(&localDir, `localDir`, `.`, `The local directory which should be synced. Use . for the current working directory.`)
|
||||||
flag.StringVar(&remoteDir, `remoteDir`, ``, `The remote directory which should be synced.`)
|
flag.StringVar(&remoteDir, `remoteDir`, ``, `The remote directory which should be synced.`)
|
||||||
flag.BoolVar(&supervised, `supervised`, true, `Use the supervised mode? The algorithm asks you before any change.`)
|
flag.BoolVar(&supervised, `supervised`, true, `Use the supervised mode? The algorithm asks you before any change.`)
|
||||||
|
flag.BoolVar(&pushOnly, `pushOnly`, true, `Use the push only mode, i.e. backup mode. Ignore any change on the remote side!`)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,7 @@
|
|||||||
package Sync
|
package Sync
|
||||||
|
|
||||||
import (
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func comparePath(localBase, localPath, remoteBase, remotePath string) bool {
|
func comparePath(localBase, localPath, remoteBase, remotePath string) bool {
|
||||||
localCompare := normalisePath(localBase, localPath)
|
localCompare := normalisePath(localBase, localPath)
|
||||||
remoteCompare := normalisePath(remoteBase, remotePath)
|
remoteCompare := normalisePath(remoteBase, remotePath)
|
||||||
return localCompare == remoteCompare
|
return localCompare == remoteCompare
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalisePath(base, path string) string {
|
|
||||||
result := strings.Replace(path, base, ``, 1)
|
|
||||||
result = filepath.ToSlash(result)
|
|
||||||
return strings.ToLower(result)
|
|
||||||
}
|
|
||||||
|
12
Sync/NormalisePath.go
Normal file
12
Sync/NormalisePath.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package Sync
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func normalisePath(base, path string) string {
|
||||||
|
result := strings.Replace(path, base, ``, 1)
|
||||||
|
result = filepath.ToSlash(result)
|
||||||
|
return strings.ToLower(result)
|
||||||
|
}
|
23
Sync/ReadYesNoAnswer.go
Normal file
23
Sync/ReadYesNoAnswer.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package Sync
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func readYesNoAnswer(defaultAnswer bool) bool { // true := yes
|
||||||
|
answer := ``
|
||||||
|
if _, scanError := fmt.Scan(&answer); scanError != nil {
|
||||||
|
return defaultAnswer
|
||||||
|
}
|
||||||
|
|
||||||
|
if answer == `` || answer == ` ` {
|
||||||
|
return defaultAnswer
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.ToLower(answer) == `y` {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
@ -13,11 +13,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
func Synchronise(ssh *ssh.Client, supervised, pushOnly bool, localDir, remoteDir string) {
|
||||||
test string = ""
|
|
||||||
)
|
|
||||||
|
|
||||||
func Synchronise(ssh *ssh.Client, supervised bool, localDir, remoteDir string) {
|
|
||||||
/*
|
/*
|
||||||
Algorithm:
|
Algorithm:
|
||||||
- Get all local files and dirs
|
- Get all local files and dirs
|
||||||
@ -65,11 +61,8 @@ func Synchronise(ssh *ssh.Client, supervised bool, localDir, remoteDir string) {
|
|||||||
log.Printf("Found %d local files.\n", len(localFiles))
|
log.Printf("Found %d local files.\n", len(localFiles))
|
||||||
|
|
||||||
//
|
//
|
||||||
// Read the remote files ============================================================================================
|
// Connect to the server ============================================================================================
|
||||||
//
|
//
|
||||||
log.Println("Try to read all remote files now...")
|
|
||||||
remoteFiles = make(map[string]os.FileInfo)
|
|
||||||
|
|
||||||
sftp, sftpError := sftp.NewClient(ssh)
|
sftp, sftpError := sftp.NewClient(ssh)
|
||||||
if sftpError != nil {
|
if sftpError != nil {
|
||||||
log.Println("Was not able to connect to the server: " + sftpError.Error())
|
log.Println("Was not able to connect to the server: " + sftpError.Error())
|
||||||
@ -78,6 +71,11 @@ func Synchronise(ssh *ssh.Client, supervised bool, localDir, remoteDir string) {
|
|||||||
|
|
||||||
defer sftp.Close()
|
defer sftp.Close()
|
||||||
|
|
||||||
|
//
|
||||||
|
// Read the remote files ============================================================================================
|
||||||
|
//
|
||||||
|
log.Println("Try to read all remote files now...")
|
||||||
|
remoteFiles = make(map[string]os.FileInfo)
|
||||||
counterRemoteFile := 0
|
counterRemoteFile := 0
|
||||||
walker := sftp.Walk(remoteDir)
|
walker := sftp.Walk(remoteDir)
|
||||||
for walker.Step() {
|
for walker.Step() {
|
||||||
@ -213,6 +211,7 @@ func Synchronise(ssh *ssh.Client, supervised bool, localDir, remoteDir string) {
|
|||||||
//
|
//
|
||||||
// Free local space ============================================================================================
|
// Free local space ============================================================================================
|
||||||
//
|
//
|
||||||
|
if !pushOnly {
|
||||||
deleteLocalFiles := make([]string, 0)
|
deleteLocalFiles := make([]string, 0)
|
||||||
for localFileNormalised, localFileInfo := range localFilesNormalised {
|
for localFileNormalised, localFileInfo := range localFilesNormalised {
|
||||||
remoteFileNormaliesed := remoteFilesNormalised[localFileNormalised]
|
remoteFileNormaliesed := remoteFilesNormalised[localFileNormalised]
|
||||||
@ -275,11 +274,13 @@ func Synchronise(ssh *ssh.Client, supervised bool, localDir, remoteDir string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Download new files ============================================================================================
|
// Download new files ============================================================================================
|
||||||
//
|
//
|
||||||
|
|
||||||
|
if !pushOnly {
|
||||||
downloadRemoteFiles := make([]string, 0)
|
downloadRemoteFiles := make([]string, 0)
|
||||||
for remoteFileNormalised, _ := range remoteFilesNormalised {
|
for remoteFileNormalised, _ := range remoteFilesNormalised {
|
||||||
localFileNormaliesed := localFilesNormalised[remoteFileNormalised]
|
localFileNormaliesed := localFilesNormalised[remoteFileNormalised]
|
||||||
@ -358,6 +359,7 @@ func Synchronise(ssh *ssh.Client, supervised bool, localDir, remoteDir string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Upload new files ============================================================================================
|
// Upload new files ============================================================================================
|
||||||
@ -447,6 +449,7 @@ func Synchronise(ssh *ssh.Client, supervised bool, localDir, remoteDir string) {
|
|||||||
// Changed files on the remote side ============================================================================================
|
// Changed files on the remote side ============================================================================================
|
||||||
//
|
//
|
||||||
|
|
||||||
|
if !pushOnly {
|
||||||
changedRemoteFiles := make([]string, 0)
|
changedRemoteFiles := make([]string, 0)
|
||||||
for remoteFileNormalised, remoteFileInfo := range remoteFilesNormalised {
|
for remoteFileNormalised, remoteFileInfo := range remoteFilesNormalised {
|
||||||
localFileNormaliesed := localFilesNormalised[remoteFileNormalised]
|
localFileNormaliesed := localFilesNormalised[remoteFileNormalised]
|
||||||
@ -504,6 +507,7 @@ func Synchronise(ssh *ssh.Client, supervised bool, localDir, remoteDir string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Changed files on the local side ============================================================================================
|
// Changed files on the local side ============================================================================================
|
||||||
@ -568,25 +572,3 @@ func Synchronise(ssh *ssh.Client, supervised bool, localDir, remoteDir string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func walkerlocal(path string, info os.FileInfo, err error) error {
|
|
||||||
localFiles[path] = info
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func readYesNoAnswer(defaultAnswer bool) bool { // true := yes
|
|
||||||
answer := ``
|
|
||||||
if _, scanError := fmt.Scan(&answer); scanError != nil {
|
|
||||||
return defaultAnswer
|
|
||||||
}
|
|
||||||
|
|
||||||
if answer == `` || answer == ` ` {
|
|
||||||
return defaultAnswer
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.ToLower(answer) == `y` {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
10
Sync/WalkerLocal.go
Normal file
10
Sync/WalkerLocal.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package Sync
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func walkerlocal(path string, info os.FileInfo, err error) error {
|
||||||
|
localFiles[path] = info
|
||||||
|
return nil
|
||||||
|
}
|
@ -7,4 +7,5 @@ var (
|
|||||||
localDir = `` // The local directory
|
localDir = `` // The local directory
|
||||||
remoteDir = `` // The remote directory
|
remoteDir = `` // The remote directory
|
||||||
supervised = true // Should the tool work supervised?
|
supervised = true // Should the tool work supervised?
|
||||||
|
pushOnly = true // Pushes only local changes to the remote i.e. backup mode
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user