func Keygen(keyPath string) error { var newKey bool key, err := secretkey.ReadFile(keyPath) if err != nil { if os.IsNotExist(err) { newKey = true key = secretkey.New() } else { return fmt.Errorf("secretkey.ReadFile: %s", err) } } if newKey { fmt.Fprintf(os.Stderr, "\nCreating new key file: %s\n", keyPath) } else { fmt.Fprintf(os.Stderr, "\nUpdating passphrase for key file: %s\n", keyPath) } if err = secretkey.WriteFile(key, keyPath); err != nil { return fmt.Errorf("secretkey.WriteFile: %s", err) } if newKey { fmt.Fprintf(os.Stderr, "\nKey file created successfully: %s\n", keyPath) } else { fmt.Fprintf(os.Stderr, "\nPassphrase updated successfully: %s\n", keyPath) } fmt.Fprintf(os.Stderr, "You should now write down your key file and store it somewhere safe!\n") return nil }
func main() { c, err := parseArgs(os.Args[1:]) if err != nil { log.Fatalf("usage error: %s", err) } if c.help { fmt.Printf(shortUsage, os.Args[0]) fmt.Print(longUsage) return } if c.version { fmt.Println(version) return } if c.keygen { if err = Keygen(c.keyPath); err != nil { log.Fatalf("keygen error: %s", err) } return } bb, err := openBucket(c.bucketPath) if err != nil { log.Fatalf("error opening bucket: %s", err) } key, err := secretkey.ReadFile(c.keyPath) if err != nil { log.Fatalf("error reading key file: %s", err) } b := upgradeBucket(bb, key) if len(c.deletes) > 0 { err := deleteBuckets(b, c.deletes) if err != nil { log.Fatalf("error deleting backups: %s", err) } return } if len(c.commands) == 0 { _, children, err := b.List() if err != nil { log.Fatalf("error listing bucket: %s", err) } for _, child := range children { fmt.Println(child) } return } var wg sync.WaitGroup wg.Add(len(c.commands)) for _, cmd := range c.commands { go func(cmd Command) { defer wg.Done() start := time.Now() n, err := cmd.Run(b) if err != nil { plog.Printf("command %q failed: %s", cmd.String(), err.Error()) return } elapsed := time.Now().Sub(start) rounded := time.Duration(int64(elapsed/time.Millisecond)) * time.Millisecond mb := float64(n) / 1e6 mbs := fmt.Sprintf("%.2f MB/s", mb/elapsed.Seconds()) plog.Printf("%s success: transferred %.2f MB in %s (%s)", cmd.String(), mb, rounded.String(), mbs) }(cmd) } wg.Wait() }