2018-01-12 16:30:54 +00:00
|
|
|
// Package config reads, writes and edits the config file and deals with command line flags
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2018-02-19 17:59:27 +00:00
|
|
|
mathrand "math/rand"
|
2018-01-12 16:30:54 +00:00
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-02-19 17:59:27 +00:00
|
|
|
"time"
|
2018-01-12 16:30:54 +00:00
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
"github.com/Unknwon/goconfig"
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/ncw/rclone/fs/accounting"
|
2018-01-18 20:19:55 +00:00
|
|
|
"github.com/ncw/rclone/fs/config/obscure"
|
2018-01-12 16:30:54 +00:00
|
|
|
"github.com/ncw/rclone/fs/driveletter"
|
|
|
|
"github.com/ncw/rclone/fs/fshttp"
|
2018-06-18 09:39:00 +00:00
|
|
|
"github.com/ncw/rclone/fs/fspath"
|
2018-01-12 16:30:54 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/crypto/nacl/secretbox"
|
|
|
|
"golang.org/x/text/unicode/norm"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
configFileName = "rclone.conf"
|
|
|
|
hiddenConfigFileName = "." + configFileName
|
|
|
|
|
|
|
|
// ConfigToken is the key used to store the token under
|
|
|
|
ConfigToken = "token"
|
|
|
|
|
|
|
|
// ConfigClientID is the config key used to store the client id
|
|
|
|
ConfigClientID = "client_id"
|
|
|
|
|
|
|
|
// ConfigClientSecret is the config key used to store the client secret
|
|
|
|
ConfigClientSecret = "client_secret"
|
|
|
|
|
|
|
|
// ConfigAuthURL is the config key used to store the auth server endpoint
|
|
|
|
ConfigAuthURL = "auth_url"
|
|
|
|
|
|
|
|
// ConfigTokenURL is the config key used to store the token server endpoint
|
|
|
|
ConfigTokenURL = "token_url"
|
|
|
|
|
|
|
|
// ConfigAutomatic indicates that we want non-interactive configuration
|
|
|
|
ConfigAutomatic = "config_automatic"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Global
|
|
|
|
var (
|
2018-03-17 11:36:30 +00:00
|
|
|
// configFile is the global config data structure. Don't read it directly, use getConfigData()
|
|
|
|
configFile *goconfig.ConfigFile
|
2018-01-12 16:30:54 +00:00
|
|
|
|
|
|
|
// ConfigPath points to the config file
|
|
|
|
ConfigPath = makeConfigPath()
|
|
|
|
|
|
|
|
// CacheDir points to the cache directory. Users of this
|
|
|
|
// should make a subdirectory and use MkdirAll() to create it
|
|
|
|
// and any parents.
|
|
|
|
CacheDir = makeCacheDir()
|
|
|
|
|
|
|
|
// Key to use for password en/decryption.
|
|
|
|
// When nil, no encryption will be used for saving.
|
|
|
|
configKey []byte
|
2018-06-09 22:27:42 +00:00
|
|
|
|
|
|
|
// output of prompt for password
|
|
|
|
PasswordPromptOutput = os.Stderr
|
2018-01-12 16:30:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-05-14 17:06:57 +00:00
|
|
|
// Set the function pointers up in fs
|
2018-05-21 13:53:45 +00:00
|
|
|
fs.ConfigFileGet = FileGetFlag
|
2018-05-14 17:06:57 +00:00
|
|
|
fs.ConfigFileSet = FileSet
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 11:36:30 +00:00
|
|
|
func getConfigData() *goconfig.ConfigFile {
|
|
|
|
if configFile == nil {
|
|
|
|
LoadConfig()
|
|
|
|
}
|
|
|
|
return configFile
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Return the path to the configuration file
|
|
|
|
func makeConfigPath() string {
|
|
|
|
// Find user's home directory
|
|
|
|
usr, err := user.Current()
|
|
|
|
var homedir string
|
|
|
|
if err == nil {
|
|
|
|
homedir = usr.HomeDir
|
|
|
|
} else {
|
|
|
|
// Fall back to reading $HOME - work around user.Current() not
|
|
|
|
// working for cross compiled binaries on OSX.
|
|
|
|
// https://github.com/golang/go/issues/6376
|
|
|
|
homedir = os.Getenv("HOME")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Possibly find the user's XDG config paths
|
|
|
|
// See XDG Base Directory specification
|
|
|
|
// https://specifications.freedesktop.org/basedir-spec/latest/
|
|
|
|
xdgdir := os.Getenv("XDG_CONFIG_HOME")
|
|
|
|
var xdgcfgdir string
|
|
|
|
if xdgdir != "" {
|
|
|
|
xdgcfgdir = filepath.Join(xdgdir, "rclone")
|
|
|
|
} else if homedir != "" {
|
|
|
|
xdgdir = filepath.Join(homedir, ".config")
|
|
|
|
xdgcfgdir = filepath.Join(xdgdir, "rclone")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use $XDG_CONFIG_HOME/rclone/rclone.conf if already existing
|
|
|
|
var xdgconf string
|
|
|
|
if xdgcfgdir != "" {
|
|
|
|
xdgconf = filepath.Join(xdgcfgdir, configFileName)
|
|
|
|
_, err := os.Stat(xdgconf)
|
|
|
|
if err == nil {
|
|
|
|
return xdgconf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use $HOME/.rclone.conf if already existing
|
|
|
|
var homeconf string
|
|
|
|
if homedir != "" {
|
|
|
|
homeconf = filepath.Join(homedir, hiddenConfigFileName)
|
|
|
|
_, err := os.Stat(homeconf)
|
|
|
|
if err == nil {
|
|
|
|
return homeconf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to create $XDG_CONFIG_HOME/rclone/rclone.conf
|
|
|
|
if xdgconf != "" {
|
|
|
|
// xdgconf != "" implies xdgcfgdir != ""
|
|
|
|
err := os.MkdirAll(xdgcfgdir, os.ModePerm)
|
|
|
|
if err == nil {
|
|
|
|
return xdgconf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to create $HOME/.rclone.conf
|
|
|
|
if homeconf != "" {
|
|
|
|
return homeconf
|
|
|
|
}
|
|
|
|
|
2018-07-15 11:39:11 +00:00
|
|
|
// Check to see if user supplied a --config variable or environment
|
|
|
|
// variable. We can't use pflag for this because it isn't initialised
|
|
|
|
// yet so we search the command line manually.
|
|
|
|
_, configSupplied := os.LookupEnv("RCLONE_CONFIG")
|
|
|
|
for _, item := range os.Args {
|
|
|
|
if item == "--config" || strings.HasPrefix(item, "--config=") {
|
|
|
|
configSupplied = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Default to ./.rclone.conf (current working directory)
|
2018-07-15 11:39:11 +00:00
|
|
|
if !configSupplied {
|
|
|
|
fs.Errorf(nil, "Couldn't find home directory or read HOME or XDG_CONFIG_HOME environment variables.")
|
|
|
|
fs.Errorf(nil, "Defaulting to storing config in current directory.")
|
|
|
|
fs.Errorf(nil, "Use --config flag to workaround.")
|
|
|
|
fs.Errorf(nil, "Error was: %v", err)
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
return hiddenConfigFileName
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadConfig loads the config file
|
|
|
|
func LoadConfig() {
|
|
|
|
// Load configuration file.
|
|
|
|
var err error
|
2018-03-17 11:36:30 +00:00
|
|
|
configFile, err = loadConfigFile()
|
2018-01-12 16:30:54 +00:00
|
|
|
if err == errorConfigFileNotFound {
|
|
|
|
fs.Logf(nil, "Config file %q not found - using defaults", ConfigPath)
|
2018-03-17 11:36:30 +00:00
|
|
|
configFile, _ = goconfig.LoadFromReader(&bytes.Buffer{})
|
2018-01-12 16:30:54 +00:00
|
|
|
} else if err != nil {
|
|
|
|
log.Fatalf("Failed to load config file %q: %v", ConfigPath, err)
|
|
|
|
} else {
|
|
|
|
fs.Debugf(nil, "Using config file from %q", ConfigPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the token bucket limiter
|
|
|
|
accounting.StartTokenBucket()
|
|
|
|
|
|
|
|
// Start the bandwidth update ticker
|
|
|
|
accounting.StartTokenTicker()
|
|
|
|
|
|
|
|
// Start the transactions per second limiter
|
|
|
|
fshttp.StartHTTPTokenBucket()
|
|
|
|
}
|
|
|
|
|
|
|
|
var errorConfigFileNotFound = errors.New("config file not found")
|
|
|
|
|
|
|
|
// loadConfigFile will load a config file, and
|
|
|
|
// automatically decrypt it.
|
|
|
|
func loadConfigFile() (*goconfig.ConfigFile, error) {
|
|
|
|
b, err := ioutil.ReadFile(ConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, errorConfigFileNotFound
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find first non-empty line
|
|
|
|
r := bufio.NewReader(bytes.NewBuffer(b))
|
|
|
|
for {
|
|
|
|
line, _, err := r.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return goconfig.LoadFromReader(bytes.NewBuffer(b))
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
l := strings.TrimSpace(string(line))
|
|
|
|
if len(l) == 0 || strings.HasPrefix(l, ";") || strings.HasPrefix(l, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// First non-empty or non-comment must be ENCRYPT_V0
|
|
|
|
if l == "RCLONE_ENCRYPT_V0:" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(l, "RCLONE_ENCRYPT_V") {
|
|
|
|
return nil, errors.New("unsupported configuration encryption - update rclone for support")
|
|
|
|
}
|
|
|
|
return goconfig.LoadFromReader(bytes.NewBuffer(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypted content is base64 encoded.
|
|
|
|
dec := base64.NewDecoder(base64.StdEncoding, r)
|
|
|
|
box, err := ioutil.ReadAll(dec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to load base64 encoded data")
|
|
|
|
}
|
|
|
|
if len(box) < 24+secretbox.Overhead {
|
|
|
|
return nil, errors.New("Configuration data too short")
|
|
|
|
}
|
|
|
|
envpw := os.Getenv("RCLONE_CONFIG_PASS")
|
|
|
|
|
|
|
|
var out []byte
|
|
|
|
for {
|
|
|
|
if len(configKey) == 0 && envpw != "" {
|
|
|
|
err := setConfigPassword(envpw)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Using RCLONE_CONFIG_PASS returned:", err)
|
|
|
|
} else {
|
|
|
|
fs.Debugf(nil, "Using RCLONE_CONFIG_PASS password.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(configKey) == 0 {
|
|
|
|
if !fs.Config.AskPassword {
|
|
|
|
return nil, errors.New("unable to decrypt configuration and not allowed to ask for password - set RCLONE_CONFIG_PASS to your configuration password")
|
|
|
|
}
|
|
|
|
getConfigPassword("Enter configuration password:")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nonce is first 24 bytes of the ciphertext
|
|
|
|
var nonce [24]byte
|
|
|
|
copy(nonce[:], box[:24])
|
|
|
|
var key [32]byte
|
|
|
|
copy(key[:], configKey[:32])
|
|
|
|
|
|
|
|
// Attempt to decrypt
|
|
|
|
var ok bool
|
|
|
|
out, ok = secretbox.Open(nil, box[24:], &nonce, &key)
|
|
|
|
if ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry
|
|
|
|
fs.Errorf(nil, "Couldn't decrypt configuration, most likely wrong password.")
|
|
|
|
configKey = nil
|
|
|
|
envpw = ""
|
|
|
|
}
|
|
|
|
return goconfig.LoadFromReader(bytes.NewBuffer(out))
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkPassword normalises and validates the password
|
|
|
|
func checkPassword(password string) (string, error) {
|
|
|
|
if !utf8.ValidString(password) {
|
|
|
|
return "", errors.New("password contains invalid utf8 characters")
|
|
|
|
}
|
|
|
|
// Check for leading/trailing whitespace
|
|
|
|
trimmedPassword := strings.TrimSpace(password)
|
|
|
|
// Warn user if password has leading+trailing whitespace
|
|
|
|
if len(password) != len(trimmedPassword) {
|
2018-05-22 08:41:13 +00:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Your password contains leading/trailing whitespace - in previous versions of rclone this was stripped")
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
// Normalize to reduce weird variations.
|
|
|
|
password = norm.NFKC.String(password)
|
|
|
|
if len(password) == 0 || len(trimmedPassword) == 0 {
|
|
|
|
return "", errors.New("no characters in password")
|
|
|
|
}
|
|
|
|
return password, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPassword asks the user for a password with the prompt given.
|
|
|
|
func GetPassword(prompt string) string {
|
2018-06-09 22:27:42 +00:00
|
|
|
_, _ = fmt.Fprintln(PasswordPromptOutput, prompt)
|
2018-01-12 16:30:54 +00:00
|
|
|
for {
|
2018-06-09 22:27:42 +00:00
|
|
|
_, _ = fmt.Fprint(PasswordPromptOutput, "password:")
|
2018-01-12 16:30:54 +00:00
|
|
|
password := ReadPassword()
|
|
|
|
password, err := checkPassword(password)
|
|
|
|
if err == nil {
|
|
|
|
return password
|
|
|
|
}
|
2018-05-22 08:41:13 +00:00
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Bad password: %v\n", err)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangePassword will query the user twice for the named password. If
|
|
|
|
// the same password is entered it is returned.
|
|
|
|
func ChangePassword(name string) string {
|
|
|
|
for {
|
|
|
|
a := GetPassword(fmt.Sprintf("Enter %s password:", name))
|
|
|
|
b := GetPassword(fmt.Sprintf("Confirm %s password:", name))
|
|
|
|
if a == b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
fmt.Println("Passwords do not match!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getConfigPassword will query the user for a password the
|
|
|
|
// first time it is required.
|
|
|
|
func getConfigPassword(q string) {
|
|
|
|
if len(configKey) != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
password := GetPassword(q)
|
|
|
|
err := setConfigPassword(password)
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 08:41:13 +00:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Error:", err)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setConfigPassword will set the configKey to the hash of
|
|
|
|
// the password. If the length of the password is
|
|
|
|
// zero after trimming+normalization, an error is returned.
|
|
|
|
func setConfigPassword(password string) error {
|
|
|
|
password, err := checkPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Create SHA256 has of the password
|
|
|
|
sha := sha256.New()
|
|
|
|
_, err = sha.Write([]byte("[" + password + "][rclone-config]"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
configKey = sha.Sum(nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// changeConfigPassword will query the user twice
|
|
|
|
// for a password. If the same password is entered
|
|
|
|
// twice the key is updated.
|
|
|
|
func changeConfigPassword() {
|
|
|
|
err := setConfigPassword(ChangePassword("NEW configuration"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to set config password: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 17:59:27 +00:00
|
|
|
// saveConfig saves configuration file.
|
2018-01-12 16:30:54 +00:00
|
|
|
// if configKey has been set, the file will be encrypted.
|
2018-02-19 17:59:27 +00:00
|
|
|
func saveConfig() error {
|
2018-01-12 16:30:54 +00:00
|
|
|
dir, name := filepath.Split(ConfigPath)
|
|
|
|
f, err := ioutil.TempFile(dir, name)
|
|
|
|
if err != nil {
|
2018-02-19 17:59:27 +00:00
|
|
|
return errors.Errorf("Failed to create temp file for new config: %v", err)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := os.Remove(f.Name()); err != nil && !os.IsNotExist(err) {
|
|
|
|
fs.Errorf(nil, "Failed to remove temp config file: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2018-03-17 11:36:30 +00:00
|
|
|
err = goconfig.SaveConfigData(getConfigData(), &buf)
|
2018-01-12 16:30:54 +00:00
|
|
|
if err != nil {
|
2018-02-19 17:59:27 +00:00
|
|
|
return errors.Errorf("Failed to save config file: %v", err)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(configKey) == 0 {
|
|
|
|
if _, err := buf.WriteTo(f); err != nil {
|
2018-02-19 17:59:27 +00:00
|
|
|
return errors.Errorf("Failed to write temp config file: %v", err)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-22 08:41:13 +00:00
|
|
|
_, _ = fmt.Fprintln(f, "# Encrypted rclone configuration File")
|
|
|
|
_, _ = fmt.Fprintln(f, "")
|
|
|
|
_, _ = fmt.Fprintln(f, "RCLONE_ENCRYPT_V0:")
|
2018-01-12 16:30:54 +00:00
|
|
|
|
|
|
|
// Generate new nonce and write it to the start of the ciphertext
|
|
|
|
var nonce [24]byte
|
|
|
|
n, _ := rand.Read(nonce[:])
|
|
|
|
if n != 24 {
|
2018-02-19 17:59:27 +00:00
|
|
|
return errors.Errorf("nonce short read: %d", n)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
enc := base64.NewEncoder(base64.StdEncoding, f)
|
|
|
|
_, err = enc.Write(nonce[:])
|
|
|
|
if err != nil {
|
2018-02-19 17:59:27 +00:00
|
|
|
return errors.Errorf("Failed to write temp config file: %v", err)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var key [32]byte
|
|
|
|
copy(key[:], configKey[:32])
|
|
|
|
|
|
|
|
b := secretbox.Seal(nil, buf.Bytes(), &nonce, &key)
|
|
|
|
_, err = enc.Write(b)
|
|
|
|
if err != nil {
|
2018-02-19 17:59:27 +00:00
|
|
|
return errors.Errorf("Failed to write temp config file: %v", err)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
_ = enc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
2018-02-19 17:59:27 +00:00
|
|
|
return errors.Errorf("Failed to close config file: %v", err)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var fileMode os.FileMode = 0600
|
|
|
|
info, err := os.Stat(ConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
fs.Debugf(nil, "Using default permissions for config file: %v", fileMode)
|
|
|
|
} else if info.Mode() != fileMode {
|
|
|
|
fs.Debugf(nil, "Keeping previous permissions for config file: %v", info.Mode())
|
|
|
|
fileMode = info.Mode()
|
|
|
|
}
|
|
|
|
|
|
|
|
attemptCopyGroup(ConfigPath, f.Name())
|
|
|
|
|
|
|
|
err = os.Chmod(f.Name(), fileMode)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to set permissions on config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.Rename(ConfigPath, ConfigPath+".old"); err != nil && !os.IsNotExist(err) {
|
2018-02-19 17:59:27 +00:00
|
|
|
return errors.Errorf("Failed to move previous config to backup location: %v", err)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
if err = os.Rename(f.Name(), ConfigPath); err != nil {
|
2018-02-19 17:59:27 +00:00
|
|
|
return errors.Errorf("Failed to move newly written config from %s to final location: %v", f.Name(), err)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
if err := os.Remove(ConfigPath + ".old"); err != nil && !os.IsNotExist(err) {
|
|
|
|
fs.Errorf(nil, "Failed to remove backup config file: %v", err)
|
|
|
|
}
|
2018-02-19 17:59:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveConfig calling function which saves configuration file.
|
|
|
|
// if saveConfig returns error trying again after sleep.
|
|
|
|
func SaveConfig() {
|
|
|
|
var err error
|
|
|
|
for i := 0; i < fs.Config.LowLevelRetries+1; i++ {
|
|
|
|
if err = saveConfig(); err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
waitingTimeMs := mathrand.Intn(1000)
|
|
|
|
time.Sleep(time.Duration(waitingTimeMs) * time.Millisecond)
|
|
|
|
}
|
|
|
|
log.Fatalf("Failed to save config after %d tries: %v", fs.Config.LowLevelRetries, err)
|
|
|
|
|
|
|
|
return
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetValueAndSave sets the key to the value and saves just that
|
|
|
|
// value in the config file. It loads the old config file in from
|
|
|
|
// disk first and overwrites the given value only.
|
|
|
|
func SetValueAndSave(name, key, value string) (err error) {
|
|
|
|
// Set the value in config in case we fail to reload it
|
2018-03-17 11:36:30 +00:00
|
|
|
getConfigData().SetValue(name, key, value)
|
2018-01-12 16:30:54 +00:00
|
|
|
// Reload the config file
|
|
|
|
reloadedConfigFile, err := loadConfigFile()
|
|
|
|
if err == errorConfigFileNotFound {
|
|
|
|
// Config file not written yet so ignore reload
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = reloadedConfigFile.GetSection(name)
|
|
|
|
if err != nil {
|
|
|
|
// Section doesn't exist yet so ignore reload
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Update the config file with the reloaded version
|
2018-03-17 11:36:30 +00:00
|
|
|
configFile = reloadedConfigFile
|
2018-01-12 16:30:54 +00:00
|
|
|
// Set the value in the reloaded version
|
|
|
|
reloadedConfigFile.SetValue(name, key, value)
|
|
|
|
// Save it again
|
|
|
|
SaveConfig()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShowRemotes shows an overview of the config file
|
|
|
|
func ShowRemotes() {
|
2018-03-17 11:36:30 +00:00
|
|
|
remotes := getConfigData().GetSectionList()
|
2018-01-12 16:30:54 +00:00
|
|
|
if len(remotes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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, FileGet(remote, "type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChooseRemote chooses a remote name
|
|
|
|
func ChooseRemote() string {
|
2018-03-17 11:36:30 +00:00
|
|
|
remotes := getConfigData().GetSectionList()
|
2018-01-12 16:30:54 +00:00
|
|
|
sort.Strings(remotes)
|
|
|
|
return Choose("remote", remotes, nil, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadLine reads some input
|
|
|
|
var ReadLine = func() 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
|
|
|
|
func Command(commands []string) byte {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
i := strings.Index(optString, string(result[0]))
|
|
|
|
if i >= 0 {
|
|
|
|
return result[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 08:09:41 +00:00
|
|
|
// ConfirmWithDefault asks the user for Yes or No and returns true or false.
|
|
|
|
//
|
|
|
|
// If AutoConfirm is set, it will return the Default value passed in
|
|
|
|
func ConfirmWithDefault(Default bool) bool {
|
2018-01-12 16:30:54 +00:00
|
|
|
if fs.Config.AutoConfirm {
|
2018-05-16 08:09:41 +00:00
|
|
|
return Default
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
return Command([]string{"yYes", "nNo"}) == 'y'
|
|
|
|
}
|
|
|
|
|
2018-05-16 08:09:41 +00:00
|
|
|
// Confirm asks the user for Yes or No and returns true or false
|
|
|
|
//
|
|
|
|
// If AutoConfirm is set, it will return true
|
|
|
|
func Confirm() bool {
|
|
|
|
return ConfirmWithDefault(true)
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00: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 {
|
|
|
|
valueDescripton := "an existing"
|
|
|
|
if newOk {
|
|
|
|
valueDescripton = "your own"
|
|
|
|
}
|
|
|
|
fmt.Printf("Choose a number from below, or type in %s value\n", valueDescripton)
|
|
|
|
for i, text := range defaults {
|
|
|
|
var lines []string
|
|
|
|
if help != nil {
|
|
|
|
parts := strings.Split(help[i], "\n")
|
|
|
|
lines = append(lines, parts...)
|
|
|
|
}
|
|
|
|
lines = append(lines, fmt.Sprintf("%q", text))
|
|
|
|
pos := i + 1
|
|
|
|
if len(lines) == 1 {
|
|
|
|
fmt.Printf("%2d > %s\n", pos, text)
|
|
|
|
} else {
|
|
|
|
mid := (len(lines) - 1) / 2
|
|
|
|
for i, line := range lines {
|
|
|
|
var sep rune
|
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
sep = '/'
|
|
|
|
case len(lines) - 1:
|
|
|
|
sep = '\\'
|
|
|
|
default:
|
|
|
|
sep = '|'
|
|
|
|
}
|
|
|
|
number := " "
|
|
|
|
if i == mid {
|
|
|
|
number = fmt.Sprintf("%2d", pos)
|
|
|
|
}
|
|
|
|
fmt.Printf("%s %c %s\n", number, sep, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
fmt.Printf("%s> ", what)
|
|
|
|
result := ReadLine()
|
|
|
|
i, err := strconv.Atoi(result)
|
|
|
|
if err != nil {
|
|
|
|
if newOk {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
for _, v := range defaults {
|
|
|
|
if result == v {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i >= 1 && i <= len(defaults) {
|
|
|
|
return defaults[i-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChooseNumber asks the user to enter a number between min and max
|
|
|
|
// inclusive prompting them with what.
|
|
|
|
func ChooseNumber(what string, min, max int) int {
|
|
|
|
for {
|
|
|
|
fmt.Printf("%s> ", what)
|
|
|
|
result := ReadLine()
|
|
|
|
i, err := strconv.Atoi(result)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Bad number: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i < min || i > max {
|
|
|
|
fmt.Printf("Out of range - %d to %d inclusive\n", min, max)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShowRemote shows the contents of the remote
|
|
|
|
func ShowRemote(name string) {
|
|
|
|
fmt.Printf("--------------------\n")
|
|
|
|
fmt.Printf("[%s]\n", name)
|
|
|
|
fs := MustFindByName(name)
|
2018-03-17 11:36:30 +00:00
|
|
|
for _, key := range getConfigData().GetKeyList(name) {
|
2018-01-12 16:30:54 +00:00
|
|
|
isPassword := false
|
|
|
|
for _, option := range fs.Options {
|
|
|
|
if option.Name == key && option.IsPassword {
|
|
|
|
isPassword = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value := FileGet(name, key)
|
|
|
|
if isPassword && value != "" {
|
|
|
|
fmt.Printf("%s = *** ENCRYPTED ***\n", key)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s = %s\n", key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Printf("--------------------\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// OkRemote prints 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 {
|
|
|
|
case 'y':
|
|
|
|
return true
|
|
|
|
case 'e':
|
|
|
|
return false
|
|
|
|
case 'd':
|
2018-03-17 11:36:30 +00:00
|
|
|
getConfigData().DeleteSection(name)
|
2018-01-12 16:30:54 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
fs.Errorf(nil, "Bad choice %c", i)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustFindByName finds the RegInfo for the remote name passed in or
|
|
|
|
// exits with a fatal error.
|
|
|
|
func MustFindByName(name string) *fs.RegInfo {
|
|
|
|
fsType := FileGet(name, "type")
|
|
|
|
if fsType == "" {
|
|
|
|
log.Fatalf("Couldn't find type of fs for %q", name)
|
|
|
|
}
|
|
|
|
return fs.MustFind(fsType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteConfig runs the config helper for the remote if needed
|
|
|
|
func RemoteConfig(name string) {
|
|
|
|
fmt.Printf("Remote config\n")
|
|
|
|
f := MustFindByName(name)
|
|
|
|
if f.Config != nil {
|
2018-05-14 17:06:57 +00:00
|
|
|
m := fs.ConfigMap(f, name)
|
|
|
|
f.Config(name, m)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 15:06:37 +00:00
|
|
|
// matchProvider returns true if provider matches the providerConfig string.
|
|
|
|
//
|
|
|
|
// The providerConfig string can either be a list of providers to
|
|
|
|
// match, or if it starts with "!" it will be a list of providers not
|
|
|
|
// to match.
|
|
|
|
//
|
|
|
|
// If either providerConfig or provider is blank then it will return true
|
|
|
|
func matchProvider(providerConfig, provider string) bool {
|
|
|
|
if providerConfig == "" || provider == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
negate := false
|
|
|
|
if strings.HasPrefix(providerConfig, "!") {
|
|
|
|
providerConfig = providerConfig[1:]
|
|
|
|
negate = true
|
|
|
|
}
|
|
|
|
providers := strings.Split(providerConfig, ",")
|
|
|
|
matched := false
|
|
|
|
for _, p := range providers {
|
|
|
|
if p == provider {
|
|
|
|
matched = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if negate {
|
|
|
|
return !matched
|
|
|
|
}
|
|
|
|
return matched
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// ChooseOption asks the user to choose an option
|
2018-04-12 16:05:53 +00:00
|
|
|
func ChooseOption(o *fs.Option, name string) string {
|
2018-04-13 15:06:37 +00:00
|
|
|
var subProvider = getConfigData().MustValue(name, fs.ConfigProvider, "")
|
2018-01-12 16:30:54 +00:00
|
|
|
fmt.Println(o.Help)
|
|
|
|
if o.IsPassword {
|
|
|
|
actions := []string{"yYes type in my own password", "gGenerate random password"}
|
2018-05-14 17:06:57 +00:00
|
|
|
if !o.Required {
|
2018-01-12 16:30:54 +00:00
|
|
|
actions = append(actions, "nNo leave this optional password blank")
|
|
|
|
}
|
|
|
|
var password string
|
|
|
|
switch i := Command(actions); i {
|
|
|
|
case 'y':
|
|
|
|
password = ChangePassword("the")
|
|
|
|
case 'g':
|
|
|
|
for {
|
|
|
|
fmt.Printf("Password strength in bits.\n64 is just about memorable\n128 is secure\n1024 is the maximum\n")
|
|
|
|
bits := ChooseNumber("Bits", 64, 1024)
|
|
|
|
bytes := bits / 8
|
|
|
|
if bits%8 != 0 {
|
|
|
|
bytes++
|
|
|
|
}
|
|
|
|
var pw = make([]byte, bytes)
|
|
|
|
n, _ := rand.Read(pw)
|
|
|
|
if n != bytes {
|
|
|
|
log.Fatalf("password short read: %d", n)
|
|
|
|
}
|
|
|
|
password = base64.RawURLEncoding.EncodeToString(pw)
|
|
|
|
fmt.Printf("Your password is: %s\n", password)
|
|
|
|
fmt.Printf("Use this password?\n")
|
|
|
|
if Confirm() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'n':
|
|
|
|
return ""
|
|
|
|
default:
|
|
|
|
fs.Errorf(nil, "Bad choice %c", i)
|
|
|
|
}
|
2018-01-18 20:19:55 +00:00
|
|
|
return obscure.MustObscure(password)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
if len(o.Examples) > 0 {
|
|
|
|
var values []string
|
|
|
|
var help []string
|
|
|
|
for _, example := range o.Examples {
|
2018-04-13 15:06:37 +00:00
|
|
|
if matchProvider(example.Provider, subProvider) {
|
2018-04-12 16:05:53 +00:00
|
|
|
values = append(values, example.Value)
|
|
|
|
help = append(help, example.Help)
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
return Choose(o.Name, values, help, true)
|
|
|
|
}
|
|
|
|
fmt.Printf("%s> ", o.Name)
|
|
|
|
return ReadLine()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateRemote adds the keyValues passed in to the remote of name.
|
|
|
|
// keyValues should be key, value pairs.
|
|
|
|
func UpdateRemote(name string, keyValues []string) error {
|
|
|
|
if len(keyValues)%2 != 0 {
|
|
|
|
return errors.New("found key without value")
|
|
|
|
}
|
|
|
|
// Set the config
|
|
|
|
for i := 0; i < len(keyValues); i += 2 {
|
2018-03-17 11:36:30 +00:00
|
|
|
getConfigData().SetValue(name, keyValues[i], keyValues[i+1])
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
RemoteConfig(name)
|
|
|
|
ShowRemote(name)
|
|
|
|
SaveConfig()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRemote creates a new remote with name, provider and a list of
|
|
|
|
// parameters which are key, value pairs. If update is set then it
|
|
|
|
// adds the new keys rather than replacing all of them.
|
|
|
|
func CreateRemote(name string, provider string, keyValues []string) error {
|
|
|
|
// Suppress Confirm
|
|
|
|
fs.Config.AutoConfirm = true
|
|
|
|
// Delete the old config if it exists
|
2018-03-17 11:36:30 +00:00
|
|
|
getConfigData().DeleteSection(name)
|
2018-01-12 16:30:54 +00:00
|
|
|
// Set the type
|
2018-03-17 11:36:30 +00:00
|
|
|
getConfigData().SetValue(name, "type", provider)
|
2018-01-12 16:30:54 +00:00
|
|
|
// Show this is automatically configured
|
2018-03-17 11:36:30 +00:00
|
|
|
getConfigData().SetValue(name, ConfigAutomatic, "yes")
|
2018-01-12 16:30:54 +00:00
|
|
|
// Set the remaining values
|
|
|
|
return UpdateRemote(name, keyValues)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PasswordRemote adds the keyValues passed in to the remote of name.
|
|
|
|
// keyValues should be key, value pairs.
|
|
|
|
func PasswordRemote(name string, keyValues []string) error {
|
|
|
|
if len(keyValues) != 2 {
|
|
|
|
return errors.New("found key without value")
|
|
|
|
}
|
|
|
|
// Suppress Confirm
|
|
|
|
fs.Config.AutoConfirm = true
|
2018-01-18 20:19:55 +00:00
|
|
|
passwd := obscure.MustObscure(keyValues[1])
|
2018-01-12 16:30:54 +00:00
|
|
|
if passwd != "" {
|
2018-03-17 11:36:30 +00:00
|
|
|
getConfigData().SetValue(name, keyValues[0], passwd)
|
2018-01-12 16:30:54 +00:00
|
|
|
RemoteConfig(name)
|
|
|
|
ShowRemote(name)
|
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSONListProviders prints all the providers and options in JSON format
|
|
|
|
func JSONListProviders() error {
|
|
|
|
b, err := json.MarshalIndent(fs.Registry, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal examples")
|
|
|
|
}
|
|
|
|
_, err = os.Stdout.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to write providers list")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fsOption returns an Option describing the possible remotes
|
|
|
|
func fsOption() *fs.Option {
|
|
|
|
o := &fs.Option{
|
|
|
|
Name: "Storage",
|
|
|
|
Help: "Type of storage to configure.",
|
|
|
|
}
|
|
|
|
for _, item := range fs.Registry {
|
|
|
|
example := fs.OptionExample{
|
|
|
|
Value: item.Name,
|
|
|
|
Help: item.Description,
|
|
|
|
}
|
|
|
|
o.Examples = append(o.Examples, example)
|
|
|
|
}
|
|
|
|
o.Examples.Sort()
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRemoteName asks the user for a name for a remote
|
|
|
|
func NewRemoteName() (name string) {
|
|
|
|
for {
|
|
|
|
fmt.Printf("name> ")
|
|
|
|
name = ReadLine()
|
2018-06-18 09:39:00 +00:00
|
|
|
parts := fspath.Matcher.FindStringSubmatch(name + ":")
|
2018-01-12 16:30:54 +00:00
|
|
|
switch {
|
|
|
|
case name == "":
|
|
|
|
fmt.Printf("Can't use empty name.\n")
|
|
|
|
case driveletter.IsDriveLetter(name):
|
2018-06-18 09:39:00 +00:00
|
|
|
fmt.Printf("Can't use %q as it can be confused with a drive letter.\n", name)
|
2018-01-12 16:30:54 +00:00
|
|
|
case parts == nil:
|
|
|
|
fmt.Printf("Can't use %q as it has invalid characters in it.\n", name)
|
|
|
|
default:
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRemote make a new remote from its name
|
|
|
|
func NewRemote(name string) {
|
2018-04-12 16:05:53 +00:00
|
|
|
newType := ChooseOption(fsOption(), name)
|
2018-03-17 11:36:30 +00:00
|
|
|
getConfigData().SetValue(name, "type", newType)
|
2018-04-13 15:06:37 +00:00
|
|
|
ri := fs.MustFind(newType)
|
|
|
|
for _, option := range ri.Options {
|
|
|
|
subProvider := getConfigData().MustValue(name, fs.ConfigProvider, "")
|
|
|
|
if matchProvider(option.Provider, subProvider) {
|
2018-04-12 16:05:53 +00:00
|
|
|
getConfigData().SetValue(name, option.Name, ChooseOption(&option, name))
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
RemoteConfig(name)
|
|
|
|
if OkRemote(name) {
|
|
|
|
SaveConfig()
|
|
|
|
return
|
|
|
|
}
|
2018-04-13 15:06:37 +00:00
|
|
|
EditRemote(ri, name)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EditRemote gets the user to edit a remote
|
2018-04-13 15:06:37 +00:00
|
|
|
func EditRemote(ri *fs.RegInfo, name string) {
|
2018-01-12 16:30:54 +00:00
|
|
|
ShowRemote(name)
|
|
|
|
fmt.Printf("Edit remote\n")
|
2018-04-13 15:06:37 +00:00
|
|
|
subProvider := getConfigData().MustValue(name, fs.ConfigProvider, "")
|
2018-01-12 16:30:54 +00:00
|
|
|
for {
|
2018-04-13 15:06:37 +00:00
|
|
|
for _, option := range ri.Options {
|
2018-01-12 16:30:54 +00:00
|
|
|
key := option.Name
|
|
|
|
value := FileGet(name, key)
|
2018-04-13 15:06:37 +00:00
|
|
|
if !matchProvider(option.Provider, subProvider) {
|
2018-04-12 16:05:53 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
fmt.Printf("Value %q = %q\n", key, value)
|
|
|
|
fmt.Printf("Edit? (y/n)>\n")
|
|
|
|
if Confirm() {
|
2018-04-12 16:05:53 +00:00
|
|
|
newValue := ChooseOption(&option, name)
|
2018-03-17 11:36:30 +00:00
|
|
|
getConfigData().SetValue(name, key, newValue)
|
2018-04-13 15:06:37 +00:00
|
|
|
// Update subProvider if it changed
|
|
|
|
if key == fs.ConfigProvider {
|
|
|
|
subProvider = newValue
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if OkRemote(name) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SaveConfig()
|
|
|
|
RemoteConfig(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRemote gets the user to delete a remote
|
|
|
|
func DeleteRemote(name string) {
|
2018-03-17 11:36:30 +00:00
|
|
|
getConfigData().DeleteSection(name)
|
2018-01-12 16:30:54 +00:00
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyRemote asks the user for a new remote name and copies name into
|
|
|
|
// it. Returns the new name.
|
|
|
|
func copyRemote(name string) string {
|
|
|
|
newName := NewRemoteName()
|
|
|
|
// Copy the keys
|
2018-03-17 11:36:30 +00:00
|
|
|
for _, key := range getConfigData().GetKeyList(name) {
|
|
|
|
value := getConfigData().MustValue(name, key, "")
|
|
|
|
getConfigData().SetValue(newName, key, value)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
return newName
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenameRemote renames a config section
|
|
|
|
func RenameRemote(name string) {
|
|
|
|
fmt.Printf("Enter new name for %q remote.\n", name)
|
|
|
|
newName := copyRemote(name)
|
|
|
|
if name != newName {
|
2018-03-17 11:36:30 +00:00
|
|
|
getConfigData().DeleteSection(name)
|
2018-01-12 16:30:54 +00:00
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyRemote copies a config section
|
|
|
|
func CopyRemote(name string) {
|
|
|
|
fmt.Printf("Enter name for copy of %q remote.\n", name)
|
|
|
|
copyRemote(name)
|
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShowConfigLocation prints the location of the config file in use
|
|
|
|
func ShowConfigLocation() {
|
|
|
|
if _, err := os.Stat(ConfigPath); os.IsNotExist(err) {
|
|
|
|
fmt.Println("Configuration file doesn't exist, but rclone will use this path:")
|
|
|
|
} else {
|
|
|
|
fmt.Println("Configuration file is stored at:")
|
|
|
|
}
|
|
|
|
fmt.Printf("%s\n", ConfigPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShowConfig prints the (unencrypted) config options
|
|
|
|
func ShowConfig() {
|
|
|
|
var buf bytes.Buffer
|
2018-03-17 11:36:30 +00:00
|
|
|
if err := goconfig.SaveConfigData(getConfigData(), &buf); err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
log.Fatalf("Failed to serialize config: %v", err)
|
|
|
|
}
|
|
|
|
str := buf.String()
|
|
|
|
if str == "" {
|
|
|
|
str = "; empty config\n"
|
|
|
|
}
|
|
|
|
fmt.Printf("%s", str)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditConfig edits the config file interactively
|
|
|
|
func EditConfig() {
|
|
|
|
for {
|
2018-03-17 11:36:30 +00:00
|
|
|
haveRemotes := len(getConfigData().GetSectionList()) != 0
|
2018-01-12 16:30:54 +00:00
|
|
|
what := []string{"eEdit existing remote", "nNew remote", "dDelete remote", "rRename remote", "cCopy remote", "sSet configuration password", "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")
|
|
|
|
// take 2nd item and last 2 items of menu list
|
|
|
|
what = append(what[1:2], what[len(what)-2:]...)
|
|
|
|
}
|
|
|
|
switch i := Command(what); i {
|
|
|
|
case 'e':
|
|
|
|
name := ChooseRemote()
|
|
|
|
fs := MustFindByName(name)
|
|
|
|
EditRemote(fs, name)
|
|
|
|
case 'n':
|
|
|
|
NewRemote(NewRemoteName())
|
|
|
|
case 'd':
|
|
|
|
name := ChooseRemote()
|
|
|
|
DeleteRemote(name)
|
|
|
|
case 'r':
|
|
|
|
RenameRemote(ChooseRemote())
|
|
|
|
case 'c':
|
|
|
|
CopyRemote(ChooseRemote())
|
|
|
|
case 's':
|
|
|
|
SetPassword()
|
|
|
|
case 'q':
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPassword will allow the user to modify the current
|
|
|
|
// configuration encryption settings.
|
|
|
|
func SetPassword() {
|
|
|
|
for {
|
|
|
|
if len(configKey) > 0 {
|
|
|
|
fmt.Println("Your configuration is encrypted.")
|
|
|
|
what := []string{"cChange Password", "uUnencrypt configuration", "qQuit to main menu"}
|
|
|
|
switch i := Command(what); i {
|
|
|
|
case 'c':
|
|
|
|
changeConfigPassword()
|
|
|
|
SaveConfig()
|
|
|
|
fmt.Println("Password changed")
|
|
|
|
continue
|
|
|
|
case 'u':
|
|
|
|
configKey = nil
|
|
|
|
SaveConfig()
|
|
|
|
continue
|
|
|
|
case 'q':
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
fmt.Println("Your configuration is not encrypted.")
|
|
|
|
fmt.Println("If you add a password, you will protect your login information to cloud services.")
|
|
|
|
what := []string{"aAdd Password", "qQuit to main menu"}
|
|
|
|
switch i := Command(what); i {
|
|
|
|
case 'a':
|
|
|
|
changeConfigPassword()
|
|
|
|
SaveConfig()
|
|
|
|
fmt.Println("Password set")
|
|
|
|
continue
|
|
|
|
case 'q':
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authorize is for remote authorization of headless machines.
|
|
|
|
//
|
|
|
|
// It expects 1 or 3 arguments
|
|
|
|
//
|
|
|
|
// rclone authorize "fs name"
|
|
|
|
// rclone authorize "fs name" "client id" "client secret"
|
|
|
|
func Authorize(args []string) {
|
|
|
|
switch len(args) {
|
|
|
|
case 1, 3:
|
|
|
|
default:
|
|
|
|
log.Fatalf("Invalid number of arguments: %d", len(args))
|
|
|
|
}
|
|
|
|
newType := args[0]
|
2018-05-14 17:06:57 +00:00
|
|
|
f := fs.MustFind(newType)
|
|
|
|
if f.Config == nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
log.Fatalf("Can't authorize fs %q", newType)
|
|
|
|
}
|
|
|
|
// Name used for temporary fs
|
|
|
|
name := "**temp-fs**"
|
|
|
|
|
|
|
|
// Make sure we delete it
|
|
|
|
defer DeleteRemote(name)
|
|
|
|
|
|
|
|
// Indicate that we want fully automatic configuration.
|
2018-03-17 11:36:30 +00:00
|
|
|
getConfigData().SetValue(name, ConfigAutomatic, "yes")
|
2018-01-12 16:30:54 +00:00
|
|
|
if len(args) == 3 {
|
2018-03-17 11:36:30 +00:00
|
|
|
getConfigData().SetValue(name, ConfigClientID, args[1])
|
|
|
|
getConfigData().SetValue(name, ConfigClientSecret, args[2])
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
2018-05-14 17:06:57 +00:00
|
|
|
m := fs.ConfigMap(f, name)
|
|
|
|
f.Config(name, m)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 13:53:45 +00:00
|
|
|
// FileGetFlag gets the config key under section returning the
|
|
|
|
// the value and true if found and or ("", false) otherwise
|
|
|
|
func FileGetFlag(section, key string) (string, bool) {
|
|
|
|
newValue, err := getConfigData().GetValue(section, key)
|
2018-05-14 17:06:57 +00:00
|
|
|
return newValue, err == nil
|
2018-05-21 13:53:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// FileGet gets the config key under section returning the
|
|
|
|
// default or empty string if not set.
|
|
|
|
//
|
|
|
|
// It looks up defaults in the environment if they are present
|
|
|
|
func FileGet(section, key string, defaultVal ...string) string {
|
2018-05-16 15:30:09 +00:00
|
|
|
envKey := fs.ConfigToEnv(section, key)
|
2018-01-12 16:30:54 +00:00
|
|
|
newValue, found := os.LookupEnv(envKey)
|
|
|
|
if found {
|
|
|
|
defaultVal = []string{newValue}
|
|
|
|
}
|
2018-03-17 11:36:30 +00:00
|
|
|
return getConfigData().MustValue(section, key, defaultVal...)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FileSet sets the key in section to value. It doesn't save
|
|
|
|
// the config file.
|
|
|
|
func FileSet(section, key, value string) {
|
2018-05-14 17:06:57 +00:00
|
|
|
if value != "" {
|
|
|
|
getConfigData().SetValue(section, key, value)
|
|
|
|
} else {
|
|
|
|
FileDeleteKey(section, key)
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FileDeleteKey deletes the config key in the config file.
|
|
|
|
// It returns true if the key was deleted,
|
|
|
|
// or returns false if the section or key didn't exist.
|
|
|
|
func FileDeleteKey(section, key string) bool {
|
2018-03-17 11:36:30 +00:00
|
|
|
return getConfigData().DeleteKey(section, key)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var matchEnv = regexp.MustCompile(`^RCLONE_CONFIG_(.*?)_TYPE=.*$`)
|
|
|
|
|
|
|
|
// FileSections returns the sections in the config file
|
|
|
|
// including any defined by environment variables.
|
|
|
|
func FileSections() []string {
|
2018-03-17 11:36:30 +00:00
|
|
|
sections := getConfigData().GetSectionList()
|
2018-01-12 16:30:54 +00:00
|
|
|
for _, item := range os.Environ() {
|
|
|
|
matches := matchEnv.FindStringSubmatch(item)
|
|
|
|
if len(matches) == 2 {
|
|
|
|
sections = append(sections, strings.ToLower(matches[1]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sections
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump dumps all the config as a JSON file
|
|
|
|
func Dump() error {
|
|
|
|
dump := make(map[string]map[string]string)
|
2018-03-17 11:36:30 +00:00
|
|
|
for _, name := range getConfigData().GetSectionList() {
|
2018-01-12 16:30:54 +00:00
|
|
|
params := make(map[string]string)
|
2018-03-17 11:36:30 +00:00
|
|
|
for _, key := range getConfigData().GetKeyList(name) {
|
2018-01-12 16:30:54 +00:00
|
|
|
params[key] = FileGet(name, key)
|
|
|
|
}
|
|
|
|
dump[name] = params
|
|
|
|
}
|
|
|
|
b, err := json.MarshalIndent(dump, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal config dump")
|
|
|
|
}
|
|
|
|
_, err = os.Stdout.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to write config dump")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeCacheDir returns a directory to use for caching.
|
|
|
|
//
|
|
|
|
// Code borrowed from go stdlib until it is made public
|
|
|
|
func makeCacheDir() (dir string) {
|
|
|
|
// Compute default location.
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
dir = os.Getenv("LocalAppData")
|
|
|
|
|
|
|
|
case "darwin":
|
|
|
|
dir = os.Getenv("HOME")
|
|
|
|
if dir != "" {
|
|
|
|
dir += "/Library/Caches"
|
|
|
|
}
|
|
|
|
|
|
|
|
case "plan9":
|
|
|
|
dir = os.Getenv("home")
|
|
|
|
if dir != "" {
|
|
|
|
// Plan 9 has no established per-user cache directory,
|
|
|
|
// but $home/lib/xyz is the usual equivalent of $HOME/.xyz on Unix.
|
|
|
|
dir += "/lib/cache"
|
|
|
|
}
|
|
|
|
|
|
|
|
default: // Unix
|
|
|
|
// https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
|
|
|
dir = os.Getenv("XDG_CACHE_HOME")
|
|
|
|
if dir == "" {
|
|
|
|
dir = os.Getenv("HOME")
|
|
|
|
if dir != "" {
|
|
|
|
dir += "/.cache"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no dir found then use TempDir - we will have a cachedir!
|
|
|
|
if dir == "" {
|
|
|
|
dir = os.TempDir()
|
|
|
|
}
|
|
|
|
return filepath.Join(dir, "rclone")
|
|
|
|
}
|