From 0575623dffd60a064be60227ea27851b3cd3113d Mon Sep 17 00:00:00 2001 From: thierry Date: Thu, 21 Sep 2017 23:27:39 +0200 Subject: [PATCH] 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. --- cmd/config/config.go | 43 ++++++++++++++++++++++++----------- fs/config.go | 53 ++++++++++++++++++++++++++++++++++++++++++++ fs/fs.go | 1 + 3 files changed, 84 insertions(+), 13 deletions(-) diff --git a/cmd/config/config.go b/cmd/config/config.go index 3b403146b..f7ad25ed4 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -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 .\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 .\n", args[0], args[1], args[2], args[3], command.Name()) + } } + return }, } diff --git a/fs/config.go b/fs/config.go index df01ea9d8..fd525b8e7 100644 --- a/fs/config.go +++ b/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{ diff --git a/fs/fs.go b/fs/fs.go index c336e9b38..b5bb5187d 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -76,6 +76,7 @@ type Option struct { Optional bool IsPassword bool Examples OptionExamples + Value string } // OptionExamples is a slice of examples