forked from TrueCloudLab/rclone
Factor rclone main() in preparation for tests
This commit is contained in:
parent
a628bef9c2
commit
3eda36f6da
2 changed files with 72 additions and 54 deletions
18
fs/fs.go
18
fs/fs.go
|
@ -211,6 +211,24 @@ func checkClose(c io.Closer, err *error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Work out modify window for fses passed in - sets Config.ModifyWindow
|
||||||
|
//
|
||||||
|
// This is the largest modify window of all the fses in use, and the
|
||||||
|
// user configured value
|
||||||
|
func CalculateModifyWindow(fs ...Fs) {
|
||||||
|
for _, f := range fs {
|
||||||
|
if f != nil {
|
||||||
|
precision := f.Precision()
|
||||||
|
if precision > Config.ModifyWindow {
|
||||||
|
Config.ModifyWindow = precision
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if Config.Verbose {
|
||||||
|
log.Printf("Modify window is %s\n", Config.ModifyWindow)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check the two files to see if the MD5sums are the same
|
// Check the two files to see if the MD5sums are the same
|
||||||
//
|
//
|
||||||
// May return an error which will already have been logged
|
// May return an error which will already have been logged
|
||||||
|
|
102
rclone.go
102
rclone.go
|
@ -521,10 +521,10 @@ func fatal(message string, args ...interface{}) {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
// Parse the command line flags
|
||||||
|
func ParseFlags() {
|
||||||
pflag.Usage = syntaxError
|
pflag.Usage = syntaxError
|
||||||
pflag.Parse()
|
pflag.Parse()
|
||||||
args := pflag.Args()
|
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
fs.LoadConfig()
|
fs.LoadConfig()
|
||||||
|
|
||||||
|
@ -538,7 +538,11 @@ func main() {
|
||||||
pprof.StartCPUProfile(f)
|
pprof.StartCPUProfile(f)
|
||||||
defer pprof.StopCPUProfile()
|
defer pprof.StopCPUProfile()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the command from the command line
|
||||||
|
func ParseCommand() (*Command, []string) {
|
||||||
|
args := pflag.Args()
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
fatal("No command supplied\n")
|
fatal("No command supplied\n")
|
||||||
}
|
}
|
||||||
|
@ -547,67 +551,44 @@ func main() {
|
||||||
args = args[1:]
|
args = args[1:]
|
||||||
|
|
||||||
// Find the command doing a prefix match
|
// Find the command doing a prefix match
|
||||||
var found *Command
|
var command *Command
|
||||||
for i := range Commands {
|
for i := range Commands {
|
||||||
command := &Commands[i]
|
trialCommand := &Commands[i]
|
||||||
// exact command name found - use that
|
// exact command name found - use that
|
||||||
if command.Name == cmd {
|
if trialCommand.Name == cmd {
|
||||||
found = command
|
command = trialCommand
|
||||||
break
|
break
|
||||||
} else if strings.HasPrefix(command.Name, cmd) {
|
} else if strings.HasPrefix(trialCommand.Name, cmd) {
|
||||||
if found != nil {
|
if command != nil {
|
||||||
fs.Stats.Error()
|
fs.Stats.Error()
|
||||||
log.Fatalf("Not unique - matches multiple commands %q", cmd)
|
log.Fatalf("Not unique - matches multiple commands %q", cmd)
|
||||||
}
|
}
|
||||||
found = command
|
command = trialCommand
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if found == nil {
|
if command == nil {
|
||||||
fs.Stats.Error()
|
fs.Stats.Error()
|
||||||
log.Fatalf("Unknown command %q", cmd)
|
log.Fatalf("Unknown command %q", cmd)
|
||||||
}
|
}
|
||||||
found.checkArgs(args)
|
if command.Run == nil {
|
||||||
|
syntaxError()
|
||||||
|
}
|
||||||
|
command.checkArgs(args)
|
||||||
|
return command, args
|
||||||
|
}
|
||||||
|
|
||||||
// Make source and destination fs
|
// Create a Fs from a name
|
||||||
var fdst, fsrc fs.Fs
|
func NewFs(remote string) fs.Fs {
|
||||||
var err error
|
f, err := fs.NewFs(remote)
|
||||||
if len(args) >= 1 {
|
|
||||||
fdst, err = fs.NewFs(args[0])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fs.Stats.Error()
|
fs.Stats.Error()
|
||||||
log.Fatalf("Failed to create file system for %q: %v", args[0], err)
|
log.Fatalf("Failed to create file system for %q: %v", remote, err)
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(args) >= 2 {
|
|
||||||
fsrc, err = fs.NewFs(args[1])
|
|
||||||
if err != nil {
|
|
||||||
fs.Stats.Error()
|
|
||||||
log.Fatalf("Failed to create destination file system for %q: %v", args[1], err)
|
|
||||||
}
|
|
||||||
fsrc, fdst = fdst, fsrc
|
|
||||||
}
|
}
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
// Work out modify window
|
// Print the stats every statsInterval
|
||||||
if fsrc != nil {
|
func StartStats() {
|
||||||
precision := fsrc.Precision()
|
|
||||||
log.Printf("Source precision %s\n", precision)
|
|
||||||
if precision > fs.Config.ModifyWindow {
|
|
||||||
fs.Config.ModifyWindow = precision
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if fdst != nil {
|
|
||||||
precision := fdst.Precision()
|
|
||||||
log.Printf("Destination precision %s\n", precision)
|
|
||||||
if precision > fs.Config.ModifyWindow {
|
|
||||||
fs.Config.ModifyWindow = precision
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if fs.Config.Verbose {
|
|
||||||
log.Printf("Modify window is %s\n", fs.Config.ModifyWindow)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print the stats every statsInterval
|
|
||||||
if !found.NoStats {
|
|
||||||
go func() {
|
go func() {
|
||||||
ch := time.Tick(*statsInterval)
|
ch := time.Tick(*statsInterval)
|
||||||
for {
|
for {
|
||||||
|
@ -615,12 +596,32 @@ func main() {
|
||||||
fs.Stats.Log()
|
fs.Stats.Log()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ParseFlags()
|
||||||
|
command, args := ParseCommand()
|
||||||
|
|
||||||
|
// Make source and destination fs
|
||||||
|
var fdst, fsrc fs.Fs
|
||||||
|
if len(args) >= 1 {
|
||||||
|
fdst = NewFs(args[0])
|
||||||
|
}
|
||||||
|
if len(args) >= 2 {
|
||||||
|
fsrc = fdst
|
||||||
|
fdst = NewFs(args[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.CalculateModifyWindow(fdst, fsrc)
|
||||||
|
|
||||||
|
if !command.NoStats {
|
||||||
|
StartStats()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the actual command
|
// Run the actual command
|
||||||
if found.Run != nil {
|
if command.Run != nil {
|
||||||
found.Run(fdst, fsrc)
|
command.Run(fdst, fsrc)
|
||||||
if !found.NoStats {
|
if !command.NoStats {
|
||||||
fmt.Println(fs.Stats)
|
fmt.Println(fs.Stats)
|
||||||
}
|
}
|
||||||
if fs.Config.Verbose {
|
if fs.Config.Verbose {
|
||||||
|
@ -631,7 +632,6 @@ func main() {
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
} else {
|
} else {
|
||||||
syntaxError()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue