5cd2909677
In case the password must provided interactively, it is now invisible. Added also exit codes in case of errors for a better usage and automation e.g. with Docker containers. Finally, improved the console output for e.g. Docker containers.
113 lines
2.8 KiB
Go
113 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/SommerEngineering/Sync/Sync"
|
|
"github.com/howeyc/gopass"
|
|
"golang.org/x/crypto/ssh"
|
|
"log"
|
|
"os"
|
|
"runtime"
|
|
)
|
|
|
|
func main() {
|
|
|
|
// Show the current version:
|
|
log.Println(`Sync v1.2.0`)
|
|
|
|
// 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.`)
|
|
os.Exit(1)
|
|
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())
|
|
os.Exit(2)
|
|
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
|
|
if dirInfo, dirError := os.Stat(localDir); dirError != nil {
|
|
log.Println("There is an error with the local directory: " + dirError.Error())
|
|
os.Exit(3)
|
|
return
|
|
} else {
|
|
if !dirInfo.IsDir() {
|
|
log.Println("There is an error with the local directory: You provided a file instead!")
|
|
os.Exit(4)
|
|
return
|
|
}
|
|
}
|
|
|
|
// Check if the password was provided:
|
|
for true {
|
|
if password == `` {
|
|
// Promt for the password:
|
|
fmt.Print(`Please provide the password for the connection: `)
|
|
if pass, errPass := gopass.GetPasswd(); errPass != nil {
|
|
log.Println(`There was an error reading the password securely: ` + errPass.Error())
|
|
os.Exit(5)
|
|
return
|
|
} else {
|
|
password = string(pass)
|
|
}
|
|
} else {
|
|
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:
|
|
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.`)
|
|
os.Exit(6)
|
|
return
|
|
}
|
|
|
|
defer ssh.Close()
|
|
Sync.Synchronise(ssh, supervised, pushOnly, localDir, remoteDir)
|
|
log.Println("Synchronising done.")
|
|
}
|