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 (
|
2020-11-05 16:59:59 +00:00
|
|
|
"context"
|
2018-01-12 16:30:54 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2018-02-19 17:59:27 +00:00
|
|
|
mathrand "math/rand"
|
2018-01-12 16:30:54 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2018-02-19 17:59:27 +00:00
|
|
|
"time"
|
2018-01-12 16:30:54 +00:00
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2018-01-12 16:30:54 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-11-07 21:05:58 +00:00
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
|
|
|
"github.com/rclone/rclone/fs/fspath"
|
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2019-08-25 07:39:31 +00:00
|
|
|
"github.com/rclone/rclone/lib/random"
|
2018-01-12 16:30:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2020-01-14 17:33:35 +00:00
|
|
|
// ConfigEncoding is the config key to change the encoding for a backend
|
|
|
|
ConfigEncoding = "encoding"
|
|
|
|
|
|
|
|
// ConfigEncodingHelp is the help for ConfigEncoding
|
|
|
|
ConfigEncodingHelp = "This sets the encoding for the backend.\n\nSee: the [encoding section in the overview](/overview/#encoding) for more info."
|
|
|
|
|
2019-01-17 15:01:13 +00:00
|
|
|
// ConfigAuthorize indicates that we just want "rclone authorize"
|
|
|
|
ConfigAuthorize = "config_authorize"
|
2019-10-26 19:19:22 +00:00
|
|
|
|
|
|
|
// ConfigAuthNoBrowser indicates that we do not want to open browser
|
|
|
|
ConfigAuthNoBrowser = "config_auth_no_browser"
|
2018-01-12 16:30:54 +00:00
|
|
|
)
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
// Storage defines an interface for loading and saving config to
|
|
|
|
// persistent storage. Rclone provides a default implementation to
|
|
|
|
// load and save to a config file when this is imported
|
|
|
|
//
|
|
|
|
// import "github.com/rclone/rclone/fs/config/configfile"
|
|
|
|
// configfile.LoadConfig(ctx)
|
2020-11-07 21:05:58 +00:00
|
|
|
type Storage interface {
|
|
|
|
// GetSectionList returns a slice of strings with names for all the
|
|
|
|
// sections
|
|
|
|
GetSectionList() []string
|
|
|
|
|
|
|
|
// HasSection returns true if section exists in the config file
|
|
|
|
HasSection(section string) bool
|
|
|
|
|
|
|
|
// DeleteSection removes the named section and all config from the
|
|
|
|
// config file
|
|
|
|
DeleteSection(section string)
|
|
|
|
|
|
|
|
// GetKeyList returns the keys in this section
|
|
|
|
GetKeyList(section string) []string
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
// GetValue returns the key in section with a found flag
|
|
|
|
GetValue(section string, key string) (value string, found bool)
|
2020-11-07 21:05:58 +00:00
|
|
|
|
|
|
|
// SetValue sets the value under key in section
|
|
|
|
SetValue(section string, key string, value string)
|
|
|
|
|
|
|
|
// DeleteKey removes the key under section
|
|
|
|
DeleteKey(section string, key string) bool
|
|
|
|
|
|
|
|
// Load the config from permanent storage
|
|
|
|
Load() error
|
|
|
|
|
|
|
|
// Save the config to permanent storage
|
|
|
|
Save() error
|
|
|
|
|
|
|
|
// Serialize the config into a string
|
|
|
|
Serialize() (string, error)
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Global
|
|
|
|
var (
|
2021-03-10 15:40:34 +00:00
|
|
|
// Data is the global config data structure
|
|
|
|
Data Storage = defaultStorage{}
|
2018-01-12 16:30:54 +00:00
|
|
|
|
|
|
|
// CacheDir points to the cache directory. Users of this
|
|
|
|
// should make a subdirectory and use MkdirAll() to create it
|
|
|
|
// and any parents.
|
|
|
|
CacheDir = makeCacheDir()
|
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
// ConfigPath points to the config file
|
|
|
|
ConfigPath = makeConfigPath()
|
2019-08-28 10:46:35 +00:00
|
|
|
|
|
|
|
// Password can be used to configure the random password generator
|
|
|
|
Password = random.Password
|
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
|
2019-08-14 16:06:13 +00:00
|
|
|
fs.ConfigFileSet = SetValueAndSave
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the path to the configuration file
|
|
|
|
func makeConfigPath() string {
|
2019-06-03 19:14:32 +00:00
|
|
|
// Use rclone.conf from rclone executable directory if already existing
|
|
|
|
exe, err := os.Executable()
|
|
|
|
if err == nil {
|
|
|
|
exedir := filepath.Dir(exe)
|
|
|
|
cfgpath := filepath.Join(exedir, configFileName)
|
|
|
|
_, err := os.Stat(cfgpath)
|
|
|
|
if err == nil {
|
|
|
|
return cfgpath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Find user's home directory
|
2019-05-10 20:25:24 +00:00
|
|
|
homeDir, err := homedir.Dir()
|
2018-01-12 16:30:54 +00:00
|
|
|
|
2018-09-03 14:30:53 +00:00
|
|
|
// Find user's configuration directory.
|
|
|
|
// Prefer XDG config path, with fallback to $HOME/.config.
|
2018-01-12 16:30:54 +00:00
|
|
|
// See XDG Base Directory specification
|
2018-09-03 14:30:53 +00:00
|
|
|
// https://specifications.freedesktop.org/basedir-spec/latest/),
|
2018-01-12 16:30:54 +00:00
|
|
|
xdgdir := os.Getenv("XDG_CONFIG_HOME")
|
2018-09-03 14:30:53 +00:00
|
|
|
var cfgdir string
|
2018-01-12 16:30:54 +00:00
|
|
|
if xdgdir != "" {
|
2018-09-03 14:30:53 +00:00
|
|
|
// User's configuration directory for rclone is $XDG_CONFIG_HOME/rclone
|
|
|
|
cfgdir = filepath.Join(xdgdir, "rclone")
|
2019-05-10 20:25:24 +00:00
|
|
|
} else if homeDir != "" {
|
2018-09-03 14:30:53 +00:00
|
|
|
// User's configuration directory for rclone is $HOME/.config/rclone
|
2019-05-10 20:25:24 +00:00
|
|
|
cfgdir = filepath.Join(homeDir, ".config", "rclone")
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 14:30:53 +00:00
|
|
|
// Use rclone.conf from user's configuration directory if already existing
|
|
|
|
var cfgpath string
|
|
|
|
if cfgdir != "" {
|
|
|
|
cfgpath = filepath.Join(cfgdir, configFileName)
|
|
|
|
_, err := os.Stat(cfgpath)
|
2018-01-12 16:30:54 +00:00
|
|
|
if err == nil {
|
2018-09-03 14:30:53 +00:00
|
|
|
return cfgpath
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 14:30:53 +00:00
|
|
|
// Use .rclone.conf from user's home directory if already existing
|
2018-01-12 16:30:54 +00:00
|
|
|
var homeconf string
|
2019-05-10 20:25:24 +00:00
|
|
|
if homeDir != "" {
|
|
|
|
homeconf = filepath.Join(homeDir, hiddenConfigFileName)
|
2018-01-12 16:30:54 +00:00
|
|
|
_, err := os.Stat(homeconf)
|
|
|
|
if err == nil {
|
|
|
|
return homeconf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 14:30:53 +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")
|
|
|
|
if !configSupplied {
|
|
|
|
for _, item := range os.Args {
|
|
|
|
if item == "--config" || strings.HasPrefix(item, "--config=") {
|
|
|
|
configSupplied = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If user's configuration directory was found, then try to create it
|
|
|
|
// and assume rclone.conf can be written there. If user supplied config
|
|
|
|
// then skip creating the directory since it will not be used.
|
|
|
|
if cfgpath != "" {
|
|
|
|
// cfgpath != "" implies cfgdir != ""
|
|
|
|
if configSupplied {
|
|
|
|
return cfgpath
|
|
|
|
}
|
|
|
|
err := os.MkdirAll(cfgdir, os.ModePerm)
|
2018-01-12 16:30:54 +00:00
|
|
|
if err == nil {
|
2018-09-03 14:30:53 +00:00
|
|
|
return cfgpath
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 14:30:53 +00:00
|
|
|
// Assume .rclone.conf can be written to user's home directory.
|
2018-01-12 16:30:54 +00:00
|
|
|
if homeconf != "" {
|
|
|
|
return homeconf
|
|
|
|
}
|
|
|
|
|
2018-09-03 14:30:53 +00:00
|
|
|
// Default to ./.rclone.conf (current working directory) if everything else fails.
|
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
|
2020-11-05 16:59:59 +00:00
|
|
|
func LoadConfig(ctx context.Context) {
|
2020-06-02 10:46:28 +00:00
|
|
|
// Set RCLONE_CONFIG_DIR for backend config and subprocesses
|
|
|
|
_ = os.Setenv("RCLONE_CONFIG_DIR", filepath.Dir(ConfigPath))
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Load configuration file.
|
2020-11-07 21:05:58 +00:00
|
|
|
if err := Data.Load(); err == ErrorConfigFileNotFound {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.Logf(nil, "Config file %q not found - using defaults", ConfigPath)
|
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 21:05:58 +00:00
|
|
|
// ErrorConfigFileNotFound is returned when the config file is not found
|
|
|
|
var ErrorConfigFileNotFound = errors.New("config file not found")
|
2018-02-19 17:59:27 +00:00
|
|
|
|
|
|
|
// SaveConfig calling function which saves configuration file.
|
2021-03-10 14:13:01 +00:00
|
|
|
// if SaveConfig returns error trying again after sleep.
|
2018-02-19 17:59:27 +00:00
|
|
|
func SaveConfig() {
|
2020-11-05 11:33:32 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
ci := fs.GetConfig(ctx)
|
2018-02-19 17:59:27 +00:00
|
|
|
var err error
|
2020-11-05 11:33:32 +00:00
|
|
|
for i := 0; i < ci.LowLevelRetries+1; i++ {
|
2020-11-07 21:05:58 +00:00
|
|
|
if err = Data.Save(); err == nil {
|
2018-02-19 17:59:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
waitingTimeMs := mathrand.Intn(1000)
|
|
|
|
time.Sleep(time.Duration(waitingTimeMs) * time.Millisecond)
|
|
|
|
}
|
2020-11-05 11:33:32 +00:00
|
|
|
log.Fatalf("Failed to save config after %d tries: %v", ci.LowLevelRetries, err)
|
2018-02-19 17:59:27 +00:00
|
|
|
|
|
|
|
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.
|
2020-11-07 21:05:58 +00:00
|
|
|
func SetValueAndSave(name, key, value string) error {
|
2018-01-12 16:30:54 +00:00
|
|
|
// Set the value in config in case we fail to reload it
|
2020-11-07 21:05:58 +00:00
|
|
|
Data.SetValue(name, key, value)
|
2018-01-12 16:30:54 +00:00
|
|
|
// Save it again
|
|
|
|
SaveConfig()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
// getWithDefault gets key out of section name returning defaultValue if not
|
|
|
|
// found.
|
|
|
|
func getWithDefault(name, key, defaultValue string) string {
|
|
|
|
value, found := Data.GetValue(name, key)
|
|
|
|
if !found {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// UpdateRemote adds the keyValues passed in to the remote of name.
|
|
|
|
// keyValues should be key, value pairs.
|
2020-11-05 18:02:26 +00:00
|
|
|
func UpdateRemote(ctx context.Context, name string, keyValues rc.Params, doObscure, noObscure bool) error {
|
2020-05-12 13:24:53 +00:00
|
|
|
if doObscure && noObscure {
|
|
|
|
return errors.New("can't use --obscure and --no-obscure together")
|
|
|
|
}
|
2019-09-05 10:01:04 +00:00
|
|
|
err := fspath.CheckConfigName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-05 11:33:32 +00:00
|
|
|
ctx = suppressConfirm(ctx)
|
2019-06-10 16:58:47 +00:00
|
|
|
|
|
|
|
// Work out which options need to be obscured
|
|
|
|
needsObscure := map[string]struct{}{}
|
2020-05-12 13:24:53 +00:00
|
|
|
if !noObscure {
|
|
|
|
if fsType := FileGet(name, "type"); fsType != "" {
|
|
|
|
if ri, err := fs.Find(fsType); err != nil {
|
|
|
|
fs.Debugf(nil, "Couldn't find fs for type %q", fsType)
|
|
|
|
} else {
|
|
|
|
for _, opt := range ri.Options {
|
|
|
|
if opt.IsPassword {
|
|
|
|
needsObscure[opt.Name] = struct{}{}
|
|
|
|
}
|
2019-06-10 16:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-12 13:24:53 +00:00
|
|
|
} else {
|
|
|
|
fs.Debugf(nil, "UpdateRemote: Couldn't find fs type")
|
2019-06-10 16:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Set the config
|
2018-11-04 18:23:12 +00:00
|
|
|
for k, v := range keyValues {
|
2019-06-10 16:58:47 +00:00
|
|
|
vStr := fmt.Sprint(v)
|
|
|
|
// Obscure parameter if necessary
|
|
|
|
if _, ok := needsObscure[k]; ok {
|
|
|
|
_, err := obscure.Reveal(vStr)
|
2020-05-12 13:24:53 +00:00
|
|
|
if err != nil || doObscure {
|
2019-06-10 16:58:47 +00:00
|
|
|
// If error => not already obscured, so obscure it
|
2020-05-12 13:24:53 +00:00
|
|
|
// or we are forced to obscure
|
2019-06-10 16:58:47 +00:00
|
|
|
vStr, err = obscure.Obscure(vStr)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "UpdateRemote: obscure failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-07 21:05:58 +00:00
|
|
|
Data.SetValue(name, k, vStr)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
2020-11-05 11:33:32 +00:00
|
|
|
RemoteConfig(ctx, name)
|
2018-01-12 16:30:54 +00:00
|
|
|
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.
|
2020-11-05 18:02:26 +00:00
|
|
|
func CreateRemote(ctx context.Context, name string, provider string, keyValues rc.Params, doObscure, noObscure bool) error {
|
2019-09-05 10:01:04 +00:00
|
|
|
err := fspath.CheckConfigName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
// Delete the old config if it exists
|
2020-11-07 21:05:58 +00:00
|
|
|
Data.DeleteSection(name)
|
2018-01-12 16:30:54 +00:00
|
|
|
// Set the type
|
2020-11-07 21:05:58 +00:00
|
|
|
Data.SetValue(name, "type", provider)
|
2018-01-12 16:30:54 +00:00
|
|
|
// Set the remaining values
|
2020-11-05 18:02:26 +00:00
|
|
|
return UpdateRemote(ctx, name, keyValues, doObscure, noObscure)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PasswordRemote adds the keyValues passed in to the remote of name.
|
|
|
|
// keyValues should be key, value pairs.
|
2020-11-05 18:02:26 +00:00
|
|
|
func PasswordRemote(ctx context.Context, name string, keyValues rc.Params) error {
|
2020-11-05 11:33:32 +00:00
|
|
|
ctx = suppressConfirm(ctx)
|
2019-09-05 10:01:04 +00:00
|
|
|
err := fspath.CheckConfigName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-04 18:23:12 +00:00
|
|
|
for k, v := range keyValues {
|
|
|
|
keyValues[k] = obscure.MustObscure(fmt.Sprint(v))
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
2020-11-05 18:02:26 +00:00
|
|
|
return UpdateRemote(ctx, name, keyValues, false, true)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
2018-06-19 20:29:52 +00:00
|
|
|
Name: "Storage",
|
|
|
|
Help: "Type of storage to configure.",
|
|
|
|
Default: "",
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-03-10 15:40:34 +00:00
|
|
|
return Data.GetValue(section, key)
|
2018-05-21 13:53:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 15:40:34 +00:00
|
|
|
// FileGet gets the config key under section returning the default if not set.
|
2018-01-12 16:30:54 +00:00
|
|
|
//
|
|
|
|
// It looks up defaults in the environment if they are present
|
2021-03-10 15:40:34 +00:00
|
|
|
func FileGet(section, key string) string {
|
|
|
|
var defaultVal 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 {
|
2021-03-10 15:40:34 +00:00
|
|
|
defaultVal = newValue
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
2021-03-10 15:40:34 +00:00
|
|
|
return getWithDefault(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 != "" {
|
2020-11-07 21:05:58 +00:00
|
|
|
Data.SetValue(section, key, value)
|
2018-05-14 17:06:57 +00:00
|
|
|
} 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 {
|
2020-11-07 21:05:58 +00:00
|
|
|
return Data.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 {
|
2020-11-07 21:05:58 +00:00
|
|
|
sections := Data.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
|
|
|
|
}
|
|
|
|
|
2018-11-04 18:23:12 +00:00
|
|
|
// DumpRcRemote dumps the config for a single remote
|
|
|
|
func DumpRcRemote(name string) (dump rc.Params) {
|
|
|
|
params := rc.Params{}
|
2020-11-07 21:05:58 +00:00
|
|
|
for _, key := range Data.GetKeyList(name) {
|
2018-11-04 18:23:12 +00:00
|
|
|
params[key] = FileGet(name, key)
|
|
|
|
}
|
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
|
|
|
// DumpRcBlob dumps all the config as an unstructured blob suitable
|
|
|
|
// for the rc
|
|
|
|
func DumpRcBlob() (dump rc.Params) {
|
|
|
|
dump = rc.Params{}
|
2020-11-07 21:05:58 +00:00
|
|
|
for _, name := range Data.GetSectionList() {
|
2018-11-04 18:23:12 +00:00
|
|
|
dump[name] = DumpRcRemote(name)
|
2018-01-12 16:30:54 +00:00
|
|
|
}
|
2018-11-04 18:23:12 +00:00
|
|
|
return dump
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump dumps all the config as a JSON file
|
|
|
|
func Dump() error {
|
|
|
|
dump := DumpRcBlob()
|
2018-01-12 16:30:54 +00:00
|
|
|
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")
|
|
|
|
}
|