mirror of
https://github.com/rclone/rclone.git
synced 2024-12-23 15:38:57 +01:00
Add config listproviders, listoptions, jsonconfig for automated config
Addition of a method listing the providers, a method listing the options of a provider and method of manual configuration.
This commit is contained in:
parent
fc8b13c993
commit
0575623dff
@ -26,23 +26,40 @@ 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
|
||||
* ` + "`rclone config listproviders`" + ` – List, in json format, the protocols supported by sync
|
||||
* ` + "`rclone config listoptions type`" + ` – Lists all the options needed to connect to a protocol
|
||||
* ` + "`rclone config jsonconfig`name type jsonoptions" + ` – Created a new remote type X with parameters Y
|
||||
`,
|
||||
Run: func(command *cobra.Command, args []string) {
|
||||
cmd.CheckArgs(0, 1, command, args)
|
||||
cmd.CheckArgs(0, 4, 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())
|
||||
} else if (len(args) == 1) {
|
||||
switch args[0] {
|
||||
case "edit":
|
||||
fs.EditConfig()
|
||||
case "show":
|
||||
fs.ShowConfig()
|
||||
case "file":
|
||||
fs.ShowConfigLocation()
|
||||
case "listproviders":
|
||||
fs.ListProviders()
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Unknown subcommand %q, %s only supports edit, show and file.\n", args[0], command.Name())
|
||||
}
|
||||
} else if (len(args) == 2) {
|
||||
if ((args[0] == "listoptions") && (args[1] != "")) {
|
||||
fs.ListOptions(args[1])
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Unknown subcommand %q %q, %s only supports optionsprovider <type>.\n", args[0], args[1], command.Name())
|
||||
}
|
||||
} else if (len(args) == 4) {
|
||||
if ((args[0] == "jsonconfig") && (args[1] != "") && (args[2] != "") && (args[3]!= "")) {
|
||||
fs.JsonConfig(args[1], args[2], args[3])
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Unknown subcommand %q %q %q %q, %s only supports jsonconfig <name> <type> <json:options>.\n", args[0], args[1], args[2], args[3], command.Name())
|
||||
}
|
||||
}
|
||||
return
|
||||
},
|
||||
}
|
||||
|
53
fs/config.go
53
fs/config.go
@ -10,6 +10,7 @@ import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@ -991,6 +992,58 @@ func ChooseOption(o *Option) string {
|
||||
return ReadLine()
|
||||
}
|
||||
|
||||
// JsonConfig Created a new remote type X with parameters Y
|
||||
func JsonConfig(name string, provider string, jsonstr string) {
|
||||
bytes := []byte(jsonstr)
|
||||
|
||||
// Unmarshal string into structs.
|
||||
var options []Option
|
||||
json.Unmarshal(bytes, &options)
|
||||
|
||||
configData.SetValue(name, "type", provider)
|
||||
// Loop over structs and display them.
|
||||
for op := range options {
|
||||
configData.SetValue(name, options[op].Name, options[op].Value)
|
||||
}
|
||||
configData.SetValue(name, ConfigAutomatic, "yes")
|
||||
RemoteConfig(name)
|
||||
ShowRemote(name)
|
||||
SaveConfig()
|
||||
}
|
||||
|
||||
// ListOptions Lists all the options needed to connect to a protocol
|
||||
func ListOptions(provider string) {
|
||||
fs := MustFind(provider)
|
||||
b, err := json.Marshal(fs.Options)
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
}
|
||||
os.Stdout.Write(b)
|
||||
}
|
||||
|
||||
// ListProviders print all providersList, in json format, the protocols supported by sync
|
||||
func ListProviders() {
|
||||
o := &Option{
|
||||
Name: "Storage",
|
||||
Help: "Type of storage to configure.",
|
||||
}
|
||||
for _, item := range fsRegistry {
|
||||
example := OptionExample{
|
||||
Value: item.Name,
|
||||
Help: item.Description,
|
||||
}
|
||||
o.Examples = append(o.Examples, example)
|
||||
}
|
||||
if len(o.Examples) > 0 {
|
||||
o.Examples.Sort()
|
||||
b, err := json.Marshal(o.Examples)
|
||||
if err != nil {
|
||||
fmt.Println("error:", err)
|
||||
}
|
||||
os.Stdout.Write(b)
|
||||
}
|
||||
}
|
||||
|
||||
// fsOption returns an Option describing the possible remotes
|
||||
func fsOption() *Option {
|
||||
o := &Option{
|
||||
|
Loading…
Reference in New Issue
Block a user