config: add show/file subcommands which print the config/its path (fixes #1086)

This commit is contained in:
Stefan 2017-09-19 17:59:19 +02:00 committed by GitHub
parent 87335de8a8
commit 85877f3adc
2 changed files with 56 additions and 3 deletions

View File

@ -1,6 +1,9 @@
package config
import (
"fmt"
"os"
"github.com/ncw/rclone/cmd"
"github.com/ncw/rclone/fs"
"github.com/spf13/cobra"
@ -11,10 +14,35 @@ func init() {
}
var commandDefintion = &cobra.Command{
Use: "config",
Use: "config [function]",
Short: `Enter an interactive configuration session.`,
Long: "`rclone config`" + `
enters an interactive configuration sessions where you can setup
new remotes and manage existing ones. You may also set or remove a password to
protect your configuration.
Additional functions:
* ` + "`rclone config edit`" + ` same as above
* ` + "`rclone config file`" + ` show path of configuration file in use
* ` + "`rclone config show`" + ` print (decrypted) config file
`,
Run: func(command *cobra.Command, args []string) {
cmd.CheckArgs(0, 0, command, args)
fs.EditConfig()
cmd.CheckArgs(0, 1, command, args)
if len(args) == 0 {
fs.EditConfig()
return
}
switch args[0] {
case "edit":
fs.EditConfig()
case "show":
fs.ShowConfig()
case "file":
fs.ShowConfigLocation()
default:
fmt.Fprintf(os.Stderr, "Unknown subcommand %q, %s only supports edit, show and file.\n", args[0], command.Name())
}
},
}

View File

@ -436,6 +436,8 @@ func LoadConfig() {
configData, _ = goconfig.LoadFromReader(&bytes.Buffer{})
} else if err != nil {
log.Fatalf("Failed to load config file %q: %v", ConfigPath, err)
} else {
Debugf(nil, "Using config file from %q", ConfigPath)
}
// Load filters
@ -1096,6 +1098,29 @@ func CopyRemote(name string) {
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
if err := goconfig.SaveConfigData(configData, &buf); err != nil {
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 {