2021-03-11 17:20:31 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-03-13 15:54:26 +01:00
|
|
|
"github.com/pkg/errors"
|
2021-03-11 17:20:31 +01:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Authorize is for remote authorization of headless machines.
|
|
|
|
//
|
|
|
|
// It expects 1 or 3 arguments
|
|
|
|
//
|
|
|
|
// rclone authorize "fs name"
|
|
|
|
// rclone authorize "fs name" "client id" "client secret"
|
2021-03-13 15:54:26 +01:00
|
|
|
func Authorize(ctx context.Context, args []string, noAutoBrowser bool) error {
|
2021-03-11 17:20:31 +01:00
|
|
|
ctx = suppressConfirm(ctx)
|
|
|
|
switch len(args) {
|
|
|
|
case 1, 3:
|
|
|
|
default:
|
2021-03-13 15:54:26 +01:00
|
|
|
return errors.Errorf("invalid number of arguments: %d", len(args))
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
newType := args[0]
|
|
|
|
f := fs.MustFind(newType)
|
|
|
|
if f.Config == nil {
|
2021-03-13 15:54:26 +01:00
|
|
|
return errors.Errorf("can't authorize fs %q", newType)
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
// Name used for temporary fs
|
|
|
|
name := "**temp-fs**"
|
|
|
|
|
|
|
|
// Make sure we delete it
|
|
|
|
defer DeleteRemote(name)
|
|
|
|
|
|
|
|
// Indicate that we are running rclone authorize
|
|
|
|
Data.SetValue(name, ConfigAuthorize, "true")
|
|
|
|
if noAutoBrowser {
|
|
|
|
Data.SetValue(name, ConfigAuthNoBrowser, "true")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 3 {
|
|
|
|
Data.SetValue(name, ConfigClientID, args[1])
|
|
|
|
Data.SetValue(name, ConfigClientSecret, args[2])
|
|
|
|
}
|
|
|
|
|
2021-02-10 15:40:36 +01:00
|
|
|
m := fs.ConfigMap(f, name, nil)
|
2021-03-11 17:20:31 +01:00
|
|
|
f.Config(ctx, name, m)
|
2021-03-13 15:54:26 +01:00
|
|
|
return nil
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|