2014-03-28 18:56:04 +01:00
|
|
|
// Read, write and edit the config file
|
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Unknwon/goconfig"
|
2014-03-27 17:55:29 +01:00
|
|
|
"github.com/ogier/pflag"
|
2014-03-15 17:06:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
configFileName = ".rclone.conf"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Global
|
|
|
|
var (
|
|
|
|
// Config file
|
|
|
|
ConfigFile *goconfig.ConfigFile
|
2014-03-15 18:01:13 +01:00
|
|
|
// Home directory
|
|
|
|
HomeDir = configHome()
|
2014-03-15 17:06:11 +01:00
|
|
|
// Config file path
|
2014-03-15 18:01:13 +01:00
|
|
|
ConfigPath = path.Join(HomeDir, configFileName)
|
2014-03-15 17:06:11 +01:00
|
|
|
// Global config
|
|
|
|
Config = &ConfigInfo{}
|
|
|
|
// Flags
|
2014-03-27 17:55:29 +01:00
|
|
|
verbose = pflag.BoolP("verbose", "v", false, "Print lots more stuff")
|
|
|
|
quiet = pflag.BoolP("quiet", "q", false, "Print as little stuff as possible")
|
|
|
|
modifyWindow = pflag.DurationP("modify-window", "", time.Nanosecond, "Max time diff to be considered the same")
|
|
|
|
checkers = pflag.IntP("checkers", "", 8, "Number of checkers to run in parallel.")
|
|
|
|
transfers = pflag.IntP("transfers", "", 4, "Number of file transfers to run in parallel.")
|
|
|
|
configFile = pflag.StringP("config", "", ConfigPath, "Config file.")
|
2014-03-28 18:56:04 +01:00
|
|
|
dryRun = pflag.BoolP("dry-run", "n", false, "Do a trial run with no permanent changes")
|
2014-03-15 17:06:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Filesystem config options
|
|
|
|
type ConfigInfo struct {
|
|
|
|
Verbose bool
|
|
|
|
Quiet bool
|
2014-03-28 18:56:04 +01:00
|
|
|
DryRun bool
|
2014-03-15 17:06:11 +01:00
|
|
|
ModifyWindow time.Duration
|
|
|
|
Checkers int
|
|
|
|
Transfers int
|
|
|
|
}
|
|
|
|
|
2014-03-15 18:01:13 +01:00
|
|
|
// Find the config directory
|
|
|
|
func configHome() string {
|
|
|
|
// Find users home directory
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Couldn't find home directory: %v", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return usr.HomeDir
|
|
|
|
}
|
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
// Loads the config file
|
|
|
|
func LoadConfig() {
|
|
|
|
// Read some flags if set
|
|
|
|
//
|
|
|
|
// FIXME read these from the config file too
|
|
|
|
Config.Verbose = *verbose
|
|
|
|
Config.Quiet = *quiet
|
2014-03-28 18:56:04 +01:00
|
|
|
Config.Quiet = *dryRun
|
2014-03-15 17:06:11 +01:00
|
|
|
Config.ModifyWindow = *modifyWindow
|
|
|
|
Config.Checkers = *checkers
|
|
|
|
Config.Transfers = *transfers
|
|
|
|
|
2014-03-15 18:01:13 +01:00
|
|
|
ConfigPath = *configFile
|
2014-03-15 17:06:11 +01:00
|
|
|
|
|
|
|
// Load configuration file.
|
2014-03-15 18:01:13 +01:00
|
|
|
var err error
|
2014-03-15 17:06:11 +01:00
|
|
|
ConfigFile, err = goconfig.LoadConfigFile(ConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to load config file %v - using defaults", ConfigPath)
|
2014-03-15 17:52:51 +01:00
|
|
|
ConfigFile, err = goconfig.LoadConfigFile(os.DevNull)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to read null config file: %v", err)
|
|
|
|
}
|
2014-03-15 17:06:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save configuration file.
|
|
|
|
func SaveConfig() {
|
|
|
|
err := goconfig.SaveConfigFile(ConfigFile, ConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to save config file: %v", err)
|
|
|
|
}
|
2014-03-16 14:53:51 +01:00
|
|
|
err = os.Chmod(ConfigPath, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to set permissions on config file: %v", err)
|
|
|
|
}
|
2014-03-15 17:06:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show an overview of the config file
|
2014-03-15 17:52:51 +01:00
|
|
|
func ShowRemotes() {
|
2014-03-15 17:06:11 +01:00
|
|
|
remotes := ConfigFile.GetSectionList()
|
2014-03-15 17:52:51 +01:00
|
|
|
if len(remotes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2014-03-15 17:06:11 +01:00
|
|
|
sort.Strings(remotes)
|
|
|
|
fmt.Printf("%-20s %s\n", "Name", "Type")
|
|
|
|
fmt.Printf("%-20s %s\n", "====", "====")
|
|
|
|
for _, remote := range remotes {
|
|
|
|
fmt.Printf("%-20s %s\n", remote, ConfigFile.MustValue(remote, "type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChooseRemote chooses a remote name
|
|
|
|
func ChooseRemote() string {
|
|
|
|
remotes := ConfigFile.GetSectionList()
|
|
|
|
sort.Strings(remotes)
|
|
|
|
return Choose("remote", remotes, nil, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read some input
|
|
|
|
func ReadLine() string {
|
|
|
|
buf := bufio.NewReader(os.Stdin)
|
|
|
|
line, err := buf.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to read line: %v", err)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command - choose one
|
2014-03-15 17:52:51 +01:00
|
|
|
func Command(commands []string) byte {
|
2014-03-15 17:06:11 +01:00
|
|
|
opts := []string{}
|
|
|
|
for _, text := range commands {
|
|
|
|
fmt.Printf("%c) %s\n", text[0], text[1:])
|
|
|
|
opts = append(opts, text[:1])
|
|
|
|
}
|
|
|
|
optString := strings.Join(opts, "")
|
|
|
|
optHelp := strings.Join(opts, "/")
|
|
|
|
for {
|
|
|
|
fmt.Printf("%s> ", optHelp)
|
|
|
|
result := strings.ToLower(ReadLine())
|
|
|
|
if len(result) != 1 {
|
|
|
|
continue
|
|
|
|
}
|
2014-06-26 16:18:48 +02:00
|
|
|
i := strings.Index(optString, string(result[0]))
|
2014-03-15 17:06:11 +01:00
|
|
|
if i >= 0 {
|
2014-03-15 17:52:51 +01:00
|
|
|
return result[0]
|
2014-03-15 17:06:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 14:54:43 +01:00
|
|
|
// Asks the user for Yes or No and returns true or false
|
|
|
|
func Confirm() bool {
|
|
|
|
return Command([]string{"yYes", "nNo"}) == 'y'
|
|
|
|
}
|
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
// Choose one of the defaults or type a new string if newOk is set
|
|
|
|
func Choose(what string, defaults, help []string, newOk bool) string {
|
|
|
|
fmt.Printf("Choose a number from below")
|
|
|
|
if newOk {
|
|
|
|
fmt.Printf(", or type in your own value")
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
for i, text := range defaults {
|
|
|
|
if help != nil {
|
|
|
|
parts := strings.Split(help[i], "\n")
|
|
|
|
for _, part := range parts {
|
|
|
|
fmt.Printf(" * %s\n", part)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Printf("%2d) %s\n", i+1, text)
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
fmt.Printf("%s> ", what)
|
|
|
|
result := ReadLine()
|
|
|
|
i, err := strconv.Atoi(result)
|
|
|
|
if err != nil {
|
|
|
|
if newOk {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i >= 1 && i <= len(defaults) {
|
|
|
|
return defaults[i-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show the contents of the remote
|
|
|
|
func ShowRemote(name string) {
|
|
|
|
fmt.Printf("--------------------\n")
|
|
|
|
fmt.Printf("[%s]\n", name)
|
|
|
|
for _, key := range ConfigFile.GetKeyList(name) {
|
|
|
|
fmt.Printf("%s = %s\n", key, ConfigFile.MustValue(name, key))
|
|
|
|
}
|
|
|
|
fmt.Printf("--------------------\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the contents of the remote and ask if it is OK
|
|
|
|
func OkRemote(name string) bool {
|
|
|
|
ShowRemote(name)
|
|
|
|
switch i := Command([]string{"yYes this is OK", "eEdit this remote", "dDelete this remote"}); i {
|
2014-03-15 17:52:51 +01:00
|
|
|
case 'y':
|
2014-03-15 17:06:11 +01:00
|
|
|
return true
|
2014-03-15 17:52:51 +01:00
|
|
|
case 'e':
|
2014-03-15 17:06:11 +01:00
|
|
|
return false
|
2014-03-15 17:52:51 +01:00
|
|
|
case 'd':
|
2014-03-15 17:06:11 +01:00
|
|
|
ConfigFile.DeleteSection(name)
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
log.Printf("Bad choice %d", i)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-03-16 14:54:43 +01:00
|
|
|
// Runs the config helper for the remote if needed
|
|
|
|
func RemoteConfig(name string) {
|
|
|
|
fmt.Printf("Remote config\n")
|
|
|
|
fsName := ConfigFile.MustValue(name, "type")
|
|
|
|
if fsName == "" {
|
|
|
|
log.Fatalf("Couldn't find type of fs for %q", name)
|
|
|
|
}
|
|
|
|
f, err := Find(fsName)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Didn't find filing system: %v", err)
|
|
|
|
}
|
|
|
|
if f.Config != nil {
|
|
|
|
f.Config(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 18:56:04 +01:00
|
|
|
// Choose an option
|
|
|
|
func ChooseOption(o *Option) string {
|
|
|
|
fmt.Println(o.Help)
|
|
|
|
if len(o.Examples) > 0 {
|
|
|
|
var values []string
|
|
|
|
var help []string
|
|
|
|
for _, example := range o.Examples {
|
|
|
|
values = append(values, example.Value)
|
|
|
|
help = append(help, example.Help)
|
|
|
|
}
|
|
|
|
return Choose(o.Name, values, help, true)
|
|
|
|
}
|
|
|
|
fmt.Printf("%s> ", o.Name)
|
|
|
|
return ReadLine()
|
|
|
|
}
|
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
// Make a new remote
|
|
|
|
func NewRemote(name string) {
|
|
|
|
fmt.Printf("What type of source is it?\n")
|
|
|
|
types := []string{}
|
|
|
|
for _, item := range fsRegistry {
|
|
|
|
types = append(types, item.Name)
|
|
|
|
}
|
|
|
|
newType := Choose("type", types, nil, false)
|
|
|
|
ConfigFile.SetValue(name, "type", newType)
|
|
|
|
fs, err := Find(newType)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to find fs: %v", err)
|
|
|
|
}
|
|
|
|
for _, option := range fs.Options {
|
2014-03-28 18:56:04 +01:00
|
|
|
ConfigFile.SetValue(name, option.Name, ChooseOption(&option))
|
2014-03-15 17:06:11 +01:00
|
|
|
}
|
2014-03-16 14:54:43 +01:00
|
|
|
RemoteConfig(name)
|
2014-03-15 17:06:11 +01:00
|
|
|
if OkRemote(name) {
|
|
|
|
SaveConfig()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
EditRemote(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edit a remote
|
|
|
|
func EditRemote(name string) {
|
|
|
|
ShowRemote(name)
|
|
|
|
fmt.Printf("Edit remote\n")
|
|
|
|
for {
|
|
|
|
for _, key := range ConfigFile.GetKeyList(name) {
|
|
|
|
value := ConfigFile.MustValue(name, key)
|
|
|
|
fmt.Printf("Press enter to accept current value, or type in a new one\n")
|
|
|
|
fmt.Printf("%s = %s>", key, value)
|
|
|
|
newValue := ReadLine()
|
|
|
|
if newValue != "" {
|
|
|
|
ConfigFile.SetValue(name, key, newValue)
|
|
|
|
}
|
|
|
|
}
|
2014-03-16 14:54:43 +01:00
|
|
|
RemoteConfig(name)
|
2014-03-15 17:06:11 +01:00
|
|
|
if OkRemote(name) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
|
2014-03-15 17:52:51 +01:00
|
|
|
// Delete a remote
|
|
|
|
func DeleteRemote(name string) {
|
|
|
|
ConfigFile.DeleteSection(name)
|
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
// Edit the config file interactively
|
|
|
|
func EditConfig() {
|
|
|
|
for {
|
2014-03-15 17:52:51 +01:00
|
|
|
haveRemotes := len(ConfigFile.GetSectionList()) != 0
|
|
|
|
what := []string{"eEdit existing remote", "nNew remote", "dDelete remote", "qQuit config"}
|
|
|
|
if haveRemotes {
|
|
|
|
fmt.Printf("Current remotes:\n\n")
|
|
|
|
ShowRemotes()
|
|
|
|
fmt.Printf("\n")
|
|
|
|
} else {
|
|
|
|
fmt.Printf("No remotes found - make a new one\n")
|
|
|
|
what = append(what[1:2], what[3])
|
|
|
|
}
|
|
|
|
switch i := Command(what); i {
|
|
|
|
case 'e':
|
2014-03-15 17:06:11 +01:00
|
|
|
name := ChooseRemote()
|
|
|
|
EditRemote(name)
|
2014-03-15 17:52:51 +01:00
|
|
|
case 'n':
|
2014-03-15 17:06:11 +01:00
|
|
|
fmt.Printf("name> ")
|
|
|
|
name := ReadLine()
|
|
|
|
NewRemote(name)
|
2014-03-15 17:52:51 +01:00
|
|
|
case 'd':
|
2014-03-15 17:06:11 +01:00
|
|
|
name := ChooseRemote()
|
2014-03-15 17:52:51 +01:00
|
|
|
DeleteRemote(name)
|
|
|
|
case 'q':
|
2014-03-15 17:06:11 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|