From b9fd02039baa75a545544dd8aa846d15cf2959aa Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sat, 3 Apr 2021 15:38:12 +0100 Subject: [PATCH] authorize: refactor to use new config interfaces #5178 --- fs/config/authorize.go | 44 ++++++++++++++++++++++++++------------ lib/oauthutil/oauthutil.go | 8 ------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/fs/config/authorize.go b/fs/config/authorize.go index dbdcccefa..404b5af5d 100644 --- a/fs/config/authorize.go +++ b/fs/config/authorize.go @@ -2,9 +2,11 @@ package config import ( "context" + "fmt" "github.com/pkg/errors" "github.com/rclone/rclone/fs" + "github.com/rclone/rclone/fs/config/configmap" ) // Authorize is for remote authorization of headless machines. @@ -20,29 +22,43 @@ func Authorize(ctx context.Context, args []string, noAutoBrowser bool) error { default: return errors.Errorf("invalid number of arguments: %d", len(args)) } - newType := args[0] - f := fs.MustFind(newType) - if f.Config == nil { - return errors.Errorf("can't authorize fs %q", newType) + Type := args[0] // FIXME could read this from input + ri, err := fs.Find(Type) + if err != nil { + return err + } + if ri.Config == nil { + return errors.Errorf("can't authorize fs %q", Type) } - // Name used for temporary fs - name := "**temp-fs**" - // Make sure we delete it - defer DeleteRemote(name) + // Config map for remote + inM := configmap.Simple{} // Indicate that we are running rclone authorize - Data.SetValue(name, ConfigAuthorize, "true") + inM[ConfigAuthorize] = "true" if noAutoBrowser { - Data.SetValue(name, ConfigAuthNoBrowser, "true") + inM[ConfigAuthNoBrowser] = "true" } if len(args) == 3 { - Data.SetValue(name, ConfigClientID, args[1]) - Data.SetValue(name, ConfigClientSecret, args[2]) + inM[ConfigClientID] = args[1] + inM[ConfigClientSecret] = args[2] } - m := fs.ConfigMap(f, name, nil) - f.Config(ctx, name, m) + // Name used for temporary remote + name := "**temp-fs**" + + m := fs.ConfigMap(ri, name, inM) + outM := configmap.Simple{} + m.ClearSetters() + m.AddSetter(outM) + + ri.Config(ctx, name, m) + + // Print code if we are doing a manual auth + fmt.Printf("Paste the following into your remote machine --->\n%s\n<---End paste\n", outM["token"]) + + fs.Debugf(nil, "Set parameters %q", outM) + return nil } diff --git a/lib/oauthutil/oauthutil.go b/lib/oauthutil/oauthutil.go index d04aed11f..667d1645c 100644 --- a/lib/oauthutil/oauthutil.go +++ b/lib/oauthutil/oauthutil.go @@ -526,14 +526,6 @@ version recommended): return errors.Wrap(err, "failed to get token") } - // Print code if we are doing a manual auth - if authorizeOnly { - result, err := json.Marshal(token) - if err != nil { - return errors.Wrap(err, "failed to marshal token") - } - fmt.Printf("Paste the following into your remote machine --->\n%s\n<---End paste\n", result) - } return PutToken(name, m, token, true) }