1
mirror of https://github.com/rclone/rclone synced 2024-11-02 23:09:23 +01:00

authorize: refactor to use new config interfaces #5178

This commit is contained in:
Nick Craig-Wood 2021-04-03 15:38:12 +01:00
parent 1a41c930f3
commit b9fd02039b
2 changed files with 30 additions and 22 deletions

View File

@ -2,9 +2,11 @@ package config
import ( import (
"context" "context"
"fmt"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configmap"
) )
// Authorize is for remote authorization of headless machines. // Authorize is for remote authorization of headless machines.
@ -20,29 +22,43 @@ func Authorize(ctx context.Context, args []string, noAutoBrowser bool) error {
default: default:
return errors.Errorf("invalid number of arguments: %d", len(args)) return errors.Errorf("invalid number of arguments: %d", len(args))
} }
newType := args[0] Type := args[0] // FIXME could read this from input
f := fs.MustFind(newType) ri, err := fs.Find(Type)
if f.Config == nil { if err != nil {
return errors.Errorf("can't authorize fs %q", newType) 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 // Config map for remote
defer DeleteRemote(name) inM := configmap.Simple{}
// Indicate that we are running rclone authorize // Indicate that we are running rclone authorize
Data.SetValue(name, ConfigAuthorize, "true") inM[ConfigAuthorize] = "true"
if noAutoBrowser { if noAutoBrowser {
Data.SetValue(name, ConfigAuthNoBrowser, "true") inM[ConfigAuthNoBrowser] = "true"
} }
if len(args) == 3 { if len(args) == 3 {
Data.SetValue(name, ConfigClientID, args[1]) inM[ConfigClientID] = args[1]
Data.SetValue(name, ConfigClientSecret, args[2]) inM[ConfigClientSecret] = args[2]
} }
m := fs.ConfigMap(f, name, nil) // Name used for temporary remote
f.Config(ctx, name, m) 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 return nil
} }

View File

@ -526,14 +526,6 @@ version recommended):
return errors.Wrap(err, "failed to get token") 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) return PutToken(name, m, token, true)
} }