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
3 changed files with 84 additions and 13 deletions
|
@ -26,23 +26,40 @@ Additional functions:
|
||||||
* ` + "`rclone config edit`" + ` – same as above
|
* ` + "`rclone config edit`" + ` – same as above
|
||||||
* ` + "`rclone config file`" + ` – show path of configuration file in use
|
* ` + "`rclone config file`" + ` – show path of configuration file in use
|
||||||
* ` + "`rclone config show`" + ` – print (decrypted) config file
|
* ` + "`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) {
|
Run: func(command *cobra.Command, args []string) {
|
||||||
cmd.CheckArgs(0, 1, command, args)
|
cmd.CheckArgs(0, 4, command, args)
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
fs.EditConfig()
|
fs.EditConfig()
|
||||||
return
|
} else if (len(args) == 1) {
|
||||||
}
|
switch args[0] {
|
||||||
|
case "edit":
|
||||||
switch args[0] {
|
fs.EditConfig()
|
||||||
case "edit":
|
case "show":
|
||||||
fs.EditConfig()
|
fs.ShowConfig()
|
||||||
case "show":
|
case "file":
|
||||||
fs.ShowConfig()
|
fs.ShowConfigLocation()
|
||||||
case "file":
|
case "listproviders":
|
||||||
fs.ShowConfigLocation()
|
fs.ListProviders()
|
||||||
default:
|
default:
|
||||||
fmt.Fprintf(os.Stderr, "Unknown subcommand %q, %s only supports edit, show and file.\n", args[0], command.Name())
|
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/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -991,6 +992,58 @@ func ChooseOption(o *Option) string {
|
||||||
return ReadLine()
|
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
|
// fsOption returns an Option describing the possible remotes
|
||||||
func fsOption() *Option {
|
func fsOption() *Option {
|
||||||
o := &Option{
|
o := &Option{
|
||||||
|
|
1
fs/fs.go
1
fs/fs.go
|
@ -76,6 +76,7 @@ type Option struct {
|
||||||
Optional bool
|
Optional bool
|
||||||
IsPassword bool
|
IsPassword bool
|
||||||
Examples OptionExamples
|
Examples OptionExamples
|
||||||
|
Value string
|
||||||
}
|
}
|
||||||
|
|
||||||
// OptionExamples is a slice of examples
|
// OptionExamples is a slice of examples
|
||||||
|
|
Loading…
Reference in a new issue