2014-03-29 01:56:04 +08:00
|
|
|
// Read, write and edit the config file
|
|
|
|
|
2014-03-16 00:06:11 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2015-09-02 05:33:34 +08:00
|
|
|
"encoding/base64"
|
2014-03-16 00:06:11 +08:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-03-15 01:53:53 +08:00
|
|
|
"math"
|
2015-05-10 18:25:54 +08:00
|
|
|
"net/http"
|
2014-03-16 00:06:11 +08:00
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Unknwon/goconfig"
|
2015-05-10 18:25:54 +08:00
|
|
|
"github.com/mreiferson/go-httpclient"
|
2015-08-30 01:14:24 +08:00
|
|
|
"github.com/spf13/pflag"
|
2014-03-16 00:06:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
configFileName = ".rclone.conf"
|
|
|
|
)
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// SizeSuffix is parsed by flag with k/M/G suffixes
|
2015-02-20 03:26:00 +08:00
|
|
|
type SizeSuffix int64
|
|
|
|
|
2014-03-16 00:06:11 +08:00
|
|
|
// Global
|
|
|
|
var (
|
2015-09-23 01:47:16 +08:00
|
|
|
// ConfigFile is the config file data structure
|
2014-03-16 00:06:11 +08:00
|
|
|
ConfigFile *goconfig.ConfigFile
|
2015-09-23 01:47:16 +08:00
|
|
|
// HomeDir is the home directory of the user
|
2014-03-16 01:01:13 +08:00
|
|
|
HomeDir = configHome()
|
2015-09-23 01:47:16 +08:00
|
|
|
// ConfigPath points to the config file
|
2014-03-16 01:01:13 +08:00
|
|
|
ConfigPath = path.Join(HomeDir, configFileName)
|
2015-09-23 01:47:16 +08:00
|
|
|
// Config is the global config
|
2014-03-16 00:06:11 +08:00
|
|
|
Config = &ConfigInfo{}
|
|
|
|
// Flags
|
2015-05-10 18:25:54 +08: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.")
|
2015-06-06 15:38:45 +08:00
|
|
|
checkSum = pflag.BoolP("checksum", "c", false, "Skip based on checksum & size, not mod-time & size")
|
|
|
|
sizeOnly = pflag.BoolP("size-only", "", false, "Skip based on size only, not mod-time or checksum")
|
2015-05-10 18:25:54 +08:00
|
|
|
dryRun = pflag.BoolP("dry-run", "n", false, "Do a trial run with no permanent changes")
|
|
|
|
connectTimeout = pflag.DurationP("contimeout", "", 60*time.Second, "Connect timeout")
|
|
|
|
timeout = pflag.DurationP("timeout", "", 5*60*time.Second, "IO idle timeout")
|
2015-09-09 04:01:26 +08:00
|
|
|
dumpHeaders = pflag.BoolP("dump-headers", "", false, "Dump HTTP headers - may contain sensitive info")
|
|
|
|
dumpBodies = pflag.BoolP("dump-bodies", "", false, "Dump HTTP headers and bodies - may contain sensitive info")
|
2015-05-10 18:25:54 +08:00
|
|
|
bwLimit SizeSuffix
|
2014-03-16 00:06:11 +08:00
|
|
|
)
|
|
|
|
|
2015-02-20 03:26:00 +08:00
|
|
|
func init() {
|
2015-03-15 01:53:53 +08:00
|
|
|
pflag.VarP(&bwLimit, "bwlimit", "", "Bandwidth limit in kBytes/s, or use suffix k|M|G")
|
2015-02-20 03:26:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Turn SizeSuffix into a string
|
2015-03-15 01:53:53 +08:00
|
|
|
func (x SizeSuffix) String() string {
|
|
|
|
scaled := float64(0)
|
|
|
|
suffix := ""
|
2015-02-20 03:26:00 +08:00
|
|
|
switch {
|
2015-03-15 01:53:53 +08:00
|
|
|
case x == 0:
|
2015-02-20 03:26:00 +08:00
|
|
|
return "0"
|
2015-03-15 01:53:53 +08:00
|
|
|
case x < 1024*1024:
|
|
|
|
scaled = float64(x) / 1024
|
|
|
|
suffix = "k"
|
|
|
|
case x < 1024*1024*1024:
|
|
|
|
scaled = float64(x) / 1024 / 1024
|
|
|
|
suffix = "M"
|
2015-02-20 03:26:00 +08:00
|
|
|
default:
|
2015-03-15 01:53:53 +08:00
|
|
|
scaled = float64(x) / 1024 / 1024 / 1024
|
|
|
|
suffix = "G"
|
2015-02-20 03:26:00 +08:00
|
|
|
}
|
2015-03-15 01:53:53 +08:00
|
|
|
if math.Floor(scaled) == scaled {
|
|
|
|
return fmt.Sprintf("%.0f%s", scaled, suffix)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%.3f%s", scaled, suffix)
|
2015-02-20 03:26:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set a SizeSuffix
|
|
|
|
func (x *SizeSuffix) Set(s string) error {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return fmt.Errorf("Empty string")
|
|
|
|
}
|
|
|
|
suffix := s[len(s)-1]
|
|
|
|
suffixLen := 1
|
|
|
|
var multiplier float64
|
|
|
|
switch suffix {
|
|
|
|
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.':
|
|
|
|
suffixLen = 0
|
|
|
|
multiplier = 1 << 10
|
|
|
|
case 'k', 'K':
|
|
|
|
multiplier = 1 << 10
|
|
|
|
case 'm', 'M':
|
|
|
|
multiplier = 1 << 20
|
|
|
|
case 'g', 'G':
|
|
|
|
multiplier = 1 << 30
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Bad suffix %q", suffix)
|
|
|
|
}
|
|
|
|
s = s[:len(s)-suffixLen]
|
|
|
|
value, err := strconv.ParseFloat(s, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-15 01:53:53 +08:00
|
|
|
if value < 0 {
|
|
|
|
return fmt.Errorf("Size can't be negative %q", s)
|
|
|
|
}
|
2015-02-20 03:26:00 +08:00
|
|
|
value *= multiplier
|
|
|
|
*x = SizeSuffix(value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-30 01:14:24 +08:00
|
|
|
// Type of the value
|
|
|
|
func (x *SizeSuffix) Type() string {
|
|
|
|
return "int64"
|
|
|
|
}
|
|
|
|
|
2015-02-20 03:26:00 +08:00
|
|
|
// Check it satisfies the interface
|
|
|
|
var _ pflag.Value = (*SizeSuffix)(nil)
|
|
|
|
|
2015-09-02 05:33:34 +08:00
|
|
|
// Obscure a config value
|
|
|
|
func Obscure(x string) string {
|
|
|
|
y := []byte(x)
|
|
|
|
for i := range y {
|
|
|
|
y[i] ^= byte(i) ^ 0xAA
|
|
|
|
}
|
|
|
|
return base64.StdEncoding.EncodeToString(y)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reveal a config value
|
|
|
|
func Reveal(y string) string {
|
|
|
|
x, err := base64.StdEncoding.DecodeString(y)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to reveal %q: %v", y, err)
|
|
|
|
}
|
|
|
|
for i := range x {
|
|
|
|
x[i] ^= byte(i) ^ 0xAA
|
|
|
|
}
|
|
|
|
return string(x)
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// ConfigInfo is filesystem config options
|
2014-03-16 00:06:11 +08:00
|
|
|
type ConfigInfo struct {
|
2015-05-10 18:25:54 +08:00
|
|
|
Verbose bool
|
|
|
|
Quiet bool
|
|
|
|
DryRun bool
|
2015-06-03 22:08:27 +08:00
|
|
|
CheckSum bool
|
2015-06-06 15:38:45 +08:00
|
|
|
SizeOnly bool
|
2015-05-10 18:25:54 +08:00
|
|
|
ModifyWindow time.Duration
|
|
|
|
Checkers int
|
|
|
|
Transfers int
|
|
|
|
ConnectTimeout time.Duration // Connect timeout
|
|
|
|
Timeout time.Duration // Data channel timeout
|
2015-09-09 04:01:26 +08:00
|
|
|
DumpHeaders bool
|
|
|
|
DumpBodies bool
|
2015-09-27 23:13:20 +08:00
|
|
|
Filter *Filter
|
2015-05-10 18:25:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transport returns an http.RoundTripper with the correct timeouts
|
|
|
|
func (ci *ConfigInfo) Transport() http.RoundTripper {
|
2015-09-09 04:01:26 +08:00
|
|
|
t := &httpclient.Transport{
|
2015-05-10 18:25:54 +08:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
MaxIdleConnsPerHost: ci.Checkers + ci.Transfers + 1,
|
|
|
|
|
|
|
|
// ConnectTimeout, if non-zero, is the maximum amount of time a dial will wait for
|
|
|
|
// a connect to complete.
|
|
|
|
ConnectTimeout: ci.ConnectTimeout,
|
|
|
|
|
|
|
|
// ResponseHeaderTimeout, if non-zero, specifies the amount of
|
|
|
|
// time to wait for a server's response headers after fully
|
|
|
|
// writing the request (including its body, if any). This
|
|
|
|
// time does not include the time to read the response body.
|
|
|
|
ResponseHeaderTimeout: ci.Timeout,
|
|
|
|
|
|
|
|
// RequestTimeout, if non-zero, specifies the amount of time for the entire
|
|
|
|
// request to complete (including all of the above timeouts + entire response body).
|
|
|
|
// This should never be less than the sum total of the above two timeouts.
|
|
|
|
//RequestTimeout: NOT SET,
|
|
|
|
|
|
|
|
// ReadWriteTimeout, if non-zero, will set a deadline for every Read and
|
|
|
|
// Write operation on the request connection.
|
|
|
|
ReadWriteTimeout: ci.Timeout,
|
|
|
|
}
|
2015-09-09 04:01:26 +08:00
|
|
|
if ci.DumpHeaders || ci.DumpBodies {
|
|
|
|
return NewLoggedTransport(t, ci.DumpBodies)
|
|
|
|
}
|
|
|
|
return t
|
2015-05-10 18:25:54 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// Client returns an http.Client with the correct timeouts
|
2015-05-10 18:25:54 +08:00
|
|
|
func (ci *ConfigInfo) Client() *http.Client {
|
|
|
|
return &http.Client{
|
|
|
|
Transport: ci.Transport(),
|
|
|
|
}
|
2014-03-16 00:06:11 +08:00
|
|
|
}
|
|
|
|
|
2014-03-16 01:01:13 +08:00
|
|
|
// Find the config directory
|
|
|
|
func configHome() string {
|
|
|
|
// Find users home directory
|
|
|
|
usr, err := user.Current()
|
2014-12-13 03:18:23 +08:00
|
|
|
if err == nil {
|
|
|
|
return usr.HomeDir
|
2014-03-16 01:01:13 +08:00
|
|
|
}
|
2014-12-13 03:18:23 +08:00
|
|
|
// Fall back to reading $HOME - work around user.Current() not
|
|
|
|
// working for cross compiled binaries on OSX.
|
|
|
|
// https://github.com/golang/go/issues/6376
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
if home != "" {
|
|
|
|
return home
|
|
|
|
}
|
|
|
|
log.Printf("Couldn't find home directory or read HOME environment variable.")
|
|
|
|
log.Printf("Defaulting to storing config in current directory.")
|
|
|
|
log.Printf("Use -config flag to workaround.")
|
|
|
|
log.Printf("Error was: %v", err)
|
|
|
|
return ""
|
2014-03-16 01:01:13 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// LoadConfig loads the config file
|
2014-03-16 00:06:11 +08:00
|
|
|
func LoadConfig() {
|
|
|
|
// Read some flags if set
|
|
|
|
//
|
|
|
|
// FIXME read these from the config file too
|
|
|
|
Config.Verbose = *verbose
|
|
|
|
Config.Quiet = *quiet
|
|
|
|
Config.ModifyWindow = *modifyWindow
|
|
|
|
Config.Checkers = *checkers
|
|
|
|
Config.Transfers = *transfers
|
2014-06-26 22:33:06 +08:00
|
|
|
Config.DryRun = *dryRun
|
2015-05-10 18:25:54 +08:00
|
|
|
Config.Timeout = *timeout
|
|
|
|
Config.ConnectTimeout = *connectTimeout
|
2015-06-03 22:08:27 +08:00
|
|
|
Config.CheckSum = *checkSum
|
2015-06-06 15:38:45 +08:00
|
|
|
Config.SizeOnly = *sizeOnly
|
2015-09-09 04:01:26 +08:00
|
|
|
Config.DumpHeaders = *dumpHeaders
|
|
|
|
Config.DumpBodies = *dumpBodies
|
2014-03-16 00:06:11 +08:00
|
|
|
|
2014-03-16 01:01:13 +08:00
|
|
|
ConfigPath = *configFile
|
2014-03-16 00:06:11 +08:00
|
|
|
|
|
|
|
// Load configuration file.
|
2014-03-16 01:01:13 +08:00
|
|
|
var err error
|
2014-03-16 00:06:11 +08:00
|
|
|
ConfigFile, err = goconfig.LoadConfigFile(ConfigPath)
|
|
|
|
if err != nil {
|
2015-08-16 00:15:02 +08:00
|
|
|
log.Printf("Failed to load config file %v - using defaults: %v", ConfigPath, err)
|
2014-03-16 00:52:51 +08:00
|
|
|
ConfigFile, err = goconfig.LoadConfigFile(os.DevNull)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to read null config file: %v", err)
|
|
|
|
}
|
2014-03-16 00:06:11 +08:00
|
|
|
}
|
2015-02-20 03:26:00 +08:00
|
|
|
|
2015-09-27 23:13:20 +08:00
|
|
|
// Load filters
|
|
|
|
Config.Filter, err = NewFilter()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to load filters: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-02-20 03:26:00 +08:00
|
|
|
// Start the token bucket limiter
|
|
|
|
startTokenBucket()
|
2014-03-16 00:06:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// SaveConfig saves configuration file.
|
2014-03-16 00:06:11 +08:00
|
|
|
func SaveConfig() {
|
|
|
|
err := goconfig.SaveConfigFile(ConfigFile, ConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to save config file: %v", err)
|
|
|
|
}
|
2014-03-16 21:53:51 +08:00
|
|
|
err = os.Chmod(ConfigPath, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to set permissions on config file: %v", err)
|
|
|
|
}
|
2014-03-16 00:06:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// ShowRemotes shows an overview of the config file
|
2014-03-16 00:52:51 +08:00
|
|
|
func ShowRemotes() {
|
2014-03-16 00:06:11 +08:00
|
|
|
remotes := ConfigFile.GetSectionList()
|
2014-03-16 00:52:51 +08:00
|
|
|
if len(remotes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2014-03-16 00:06:11 +08: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)
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// ReadLine reads some input
|
2014-03-16 00:06:11 +08:00
|
|
|
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-16 00:52:51 +08:00
|
|
|
func Command(commands []string) byte {
|
2014-03-16 00:06:11 +08: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 22:18:48 +08:00
|
|
|
i := strings.Index(optString, string(result[0]))
|
2014-03-16 00:06:11 +08:00
|
|
|
if i >= 0 {
|
2014-03-16 00:52:51 +08:00
|
|
|
return result[0]
|
2014-03-16 00:06:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// Confirm asks the user for Yes or No and returns true or false
|
2014-03-16 21:54:43 +08:00
|
|
|
func Confirm() bool {
|
|
|
|
return Command([]string{"yYes", "nNo"}) == 'y'
|
|
|
|
}
|
|
|
|
|
2014-03-16 00:06:11 +08: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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// ShowRemote shows the contents of the remote
|
2014-03-16 00:06:11 +08:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// OkRemote prints the contents of the remote and ask if it is OK
|
2014-03-16 00:06:11 +08:00
|
|
|
func OkRemote(name string) bool {
|
|
|
|
ShowRemote(name)
|
|
|
|
switch i := Command([]string{"yYes this is OK", "eEdit this remote", "dDelete this remote"}); i {
|
2014-03-16 00:52:51 +08:00
|
|
|
case 'y':
|
2014-03-16 00:06:11 +08:00
|
|
|
return true
|
2014-03-16 00:52:51 +08:00
|
|
|
case 'e':
|
2014-03-16 00:06:11 +08:00
|
|
|
return false
|
2014-03-16 00:52:51 +08:00
|
|
|
case 'd':
|
2014-03-16 00:06:11 +08:00
|
|
|
ConfigFile.DeleteSection(name)
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
log.Printf("Bad choice %d", i)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// RemoteConfig runs the config helper for the remote if needed
|
2014-03-16 21:54:43 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// ChooseOption asks the user to choose an option
|
2014-03-29 01:56:04 +08:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// NewRemote make a new remote from its name
|
2014-03-16 00:06:11 +08:00
|
|
|
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-29 01:56:04 +08:00
|
|
|
ConfigFile.SetValue(name, option.Name, ChooseOption(&option))
|
2014-03-16 00:06:11 +08:00
|
|
|
}
|
2014-03-16 21:54:43 +08:00
|
|
|
RemoteConfig(name)
|
2014-03-16 00:06:11 +08:00
|
|
|
if OkRemote(name) {
|
|
|
|
SaveConfig()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
EditRemote(name)
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// EditRemote gets the user to edit a remote
|
2014-03-16 00:06:11 +08:00
|
|
|
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 21:54:43 +08:00
|
|
|
RemoteConfig(name)
|
2014-03-16 00:06:11 +08:00
|
|
|
if OkRemote(name) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// DeleteRemote gets the user to delete a remote
|
2014-03-16 00:52:51 +08:00
|
|
|
func DeleteRemote(name string) {
|
|
|
|
ConfigFile.DeleteSection(name)
|
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// EditConfig edits the config file interactively
|
2014-03-16 00:06:11 +08:00
|
|
|
func EditConfig() {
|
|
|
|
for {
|
2014-03-16 00:52:51 +08: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-16 00:06:11 +08:00
|
|
|
name := ChooseRemote()
|
|
|
|
EditRemote(name)
|
2014-03-16 00:52:51 +08:00
|
|
|
case 'n':
|
2015-02-11 00:48:04 +08:00
|
|
|
nameLoop:
|
2015-02-07 23:49:09 +08:00
|
|
|
for {
|
|
|
|
fmt.Printf("name> ")
|
|
|
|
name := ReadLine()
|
|
|
|
switch {
|
|
|
|
case name == "":
|
|
|
|
fmt.Printf("Can't use empty name\n")
|
|
|
|
case isDriveLetter(name):
|
|
|
|
fmt.Printf("Can't use %q as it can be confused a drive letter\n", name)
|
|
|
|
default:
|
|
|
|
NewRemote(name)
|
2015-02-11 00:48:04 +08:00
|
|
|
break nameLoop
|
2015-02-07 23:49:09 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-16 00:52:51 +08:00
|
|
|
case 'd':
|
2014-03-16 00:06:11 +08:00
|
|
|
name := ChooseRemote()
|
2014-03-16 00:52:51 +08:00
|
|
|
DeleteRemote(name)
|
|
|
|
case 'q':
|
2014-03-16 00:06:11 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|