2015-09-28 19:06:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/SommerEngineering/Sync/Sync"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"log"
|
2015-09-29 19:26:34 +00:00
|
|
|
"os"
|
2015-09-28 19:06:09 +00:00
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
// Show the current version:
|
2015-09-30 08:51:22 +00:00
|
|
|
log.Println(`Sync v1.0.0`)
|
2015-09-28 19:06:09 +00:00
|
|
|
|
|
|
|
// Allow Go to use all CPUs:
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
|
|
|
// Read the configuration from the command-line args:
|
|
|
|
readFlags()
|
|
|
|
|
|
|
|
// Check if the directories are provided:
|
|
|
|
if localDir == `` || remoteDir == `` {
|
|
|
|
log.Println(`Please provide the local and remote directory.`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-30 08:51:22 +00:00
|
|
|
// 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)
|
|
|
|
|
2015-09-29 19:26:34 +00:00
|
|
|
// Check if local dir exist
|
|
|
|
if dirInfo, dirError := os.Stat(localDir); dirError != nil {
|
|
|
|
log.Println("There is an error with the local directory: " + dirError.Error())
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
if !dirInfo.IsDir() {
|
|
|
|
log.Println("There is an error with the local directory: You provided a file instead!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 19:06:09 +00:00
|
|
|
// Check if the password was provided:
|
|
|
|
for true {
|
|
|
|
if password == `` {
|
|
|
|
// Promt for the password:
|
2015-09-30 08:51:22 +00:00
|
|
|
fmt.Print(`Please provide the password for the connection: `)
|
2015-09-28 19:06:09 +00:00
|
|
|
fmt.Scanln(&password)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 08:51:22 +00:00
|
|
|
// 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.")
|
|
|
|
}
|
|
|
|
|
2015-09-28 19:06:09 +00:00
|
|
|
// Create the SSH configuration:
|
|
|
|
Sync.SetPassword4Callback(password)
|
|
|
|
config := &ssh.ClientConfig{
|
|
|
|
User: username,
|
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.Password(password),
|
|
|
|
ssh.PasswordCallback(Sync.PasswordCallback),
|
|
|
|
ssh.KeyboardInteractive(Sync.KeyboardInteractiveChallenge),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to the SSH server:
|
|
|
|
ssh := Sync.ConnectSSH(config, serverAddrString)
|
|
|
|
if ssh == nil {
|
|
|
|
log.Println(`It was not possible to connect to the SSH server.`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer ssh.Close()
|
2015-09-30 08:51:22 +00:00
|
|
|
Sync.Synchronise(ssh, supervised, pushOnly, localDir, remoteDir)
|
2015-09-29 19:26:34 +00:00
|
|
|
log.Println("Synchronising done.")
|
2015-09-28 19:06:09 +00:00
|
|
|
}
|