forked from TrueCloudLab/rclone
cmd/rc: add --user and --pass flags and interpret --rc-user, --rc-pass, --rc-addr
This commit is contained in:
parent
75e8ea383c
commit
181267e20e
1 changed files with 59 additions and 15 deletions
74
cmd/rc/rc.go
74
cmd/rc/rc.go
|
@ -22,6 +22,8 @@ var (
|
||||||
noOutput = false
|
noOutput = false
|
||||||
url = "http://localhost:5572/"
|
url = "http://localhost:5572/"
|
||||||
jsonInput = ""
|
jsonInput = ""
|
||||||
|
authUser = ""
|
||||||
|
authPass = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -29,14 +31,24 @@ func init() {
|
||||||
commandDefintion.Flags().BoolVarP(&noOutput, "no-output", "", noOutput, "If set don't output the JSON result.")
|
commandDefintion.Flags().BoolVarP(&noOutput, "no-output", "", noOutput, "If set don't output the JSON result.")
|
||||||
commandDefintion.Flags().StringVarP(&url, "url", "", url, "URL to connect to rclone remote control.")
|
commandDefintion.Flags().StringVarP(&url, "url", "", url, "URL to connect to rclone remote control.")
|
||||||
commandDefintion.Flags().StringVarP(&jsonInput, "json", "", jsonInput, "Input JSON - use instead of key=value args.")
|
commandDefintion.Flags().StringVarP(&jsonInput, "json", "", jsonInput, "Input JSON - use instead of key=value args.")
|
||||||
|
commandDefintion.Flags().StringVarP(&authUser, "user", "", "", "Username to use to rclone remote control.")
|
||||||
|
commandDefintion.Flags().StringVarP(&authPass, "pass", "", "", "Password to use to connect to rclone remote control.")
|
||||||
}
|
}
|
||||||
|
|
||||||
var commandDefintion = &cobra.Command{
|
var commandDefintion = &cobra.Command{
|
||||||
Use: "rc commands parameter",
|
Use: "rc commands parameter",
|
||||||
Short: `Run a command against a running rclone.`,
|
Short: `Run a command against a running rclone.`,
|
||||||
Long: `
|
Long: `
|
||||||
This runs a command against a running rclone. By default it will use
|
|
||||||
that specified in the --rc-addr command.
|
This runs a command against a running rclone. Use the --url flag to
|
||||||
|
specify an non default URL to connect on. This can be either a
|
||||||
|
":port" which is taken to mean "http://localhost:port" or a
|
||||||
|
"host:port" which is taken to mean "http://host:port"
|
||||||
|
|
||||||
|
A username and password can be passed in with --user and --pass.
|
||||||
|
|
||||||
|
Note that --rc-addr, --rc-user, --rc-pass will be read also for --url,
|
||||||
|
--user, --pass.
|
||||||
|
|
||||||
Arguments should be passed in as parameter=value.
|
Arguments should be passed in as parameter=value.
|
||||||
|
|
||||||
|
@ -50,6 +62,7 @@ Use "rclone rc" to see a list of all possible commands.`,
|
||||||
Run: func(command *cobra.Command, args []string) {
|
Run: func(command *cobra.Command, args []string) {
|
||||||
cmd.CheckArgs(0, 1E9, command, args)
|
cmd.CheckArgs(0, 1E9, command, args)
|
||||||
cmd.Run(false, false, command, func() error {
|
cmd.Run(false, false, command, func() error {
|
||||||
|
parseFlags()
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return list()
|
return list()
|
||||||
}
|
}
|
||||||
|
@ -58,30 +71,56 @@ Use "rclone rc" to see a list of all possible commands.`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse the flags
|
||||||
|
func parseFlags() {
|
||||||
|
// set alternates from alternate flags
|
||||||
|
setAlternateFlag("rc-addr", &url)
|
||||||
|
setAlternateFlag("rc-user", &authUser)
|
||||||
|
setAlternateFlag("rc-pass", &authPass)
|
||||||
|
// If url is just :port then fix it up
|
||||||
|
if strings.HasPrefix(url, ":") {
|
||||||
|
url = "localhost" + url
|
||||||
|
}
|
||||||
|
// if url is just host:port add http://
|
||||||
|
if !strings.HasPrefix(url, "http:") && !strings.HasPrefix(url, "https:") {
|
||||||
|
url = "http://" + url
|
||||||
|
}
|
||||||
|
// if url doesn't end with / add it
|
||||||
|
if !strings.HasSuffix(url, "/") {
|
||||||
|
url += "/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the user set flagName set the output to its value
|
||||||
|
func setAlternateFlag(flagName string, output *string) {
|
||||||
|
if rcFlag := pflag.Lookup(flagName); rcFlag != nil && rcFlag.Changed {
|
||||||
|
*output = rcFlag.Value.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// do a call from (path, in) to (out, err).
|
// do a call from (path, in) to (out, err).
|
||||||
//
|
//
|
||||||
// if err is set, out may be a valid error return or it may be nil
|
// if err is set, out may be a valid error return or it may be nil
|
||||||
func doCall(path string, in rc.Params) (out rc.Params, err error) {
|
func doCall(path string, in rc.Params) (out rc.Params, err error) {
|
||||||
// Do HTTP request
|
// Do HTTP request
|
||||||
client := fshttp.NewClient(fs.Config)
|
client := fshttp.NewClient(fs.Config)
|
||||||
url := url
|
|
||||||
// set the user use --rc-addr as well as --url
|
|
||||||
if rcAddrFlag := pflag.Lookup("rc-addr"); rcAddrFlag != nil && rcAddrFlag.Changed {
|
|
||||||
url = rcAddrFlag.Value.String()
|
|
||||||
if strings.HasPrefix(url, ":") {
|
|
||||||
url = "localhost" + url
|
|
||||||
}
|
|
||||||
url = "http://" + url + "/"
|
|
||||||
}
|
|
||||||
if !strings.HasSuffix(url, "/") {
|
|
||||||
url += "/"
|
|
||||||
}
|
|
||||||
url += path
|
url += path
|
||||||
data, err := json.Marshal(in)
|
data, err := json.Marshal(in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to encode JSON")
|
return nil, errors.Wrap(err, "failed to encode JSON")
|
||||||
}
|
}
|
||||||
resp, err := client.Post(url, "application/json", bytes.NewBuffer(data))
|
|
||||||
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "failed to make request")
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
if authUser != "" || authPass != "" {
|
||||||
|
req.SetBasicAuth(authUser, authPass)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "connection failed")
|
return nil, errors.Wrap(err, "connection failed")
|
||||||
}
|
}
|
||||||
|
@ -172,6 +211,11 @@ func list() error {
|
||||||
}
|
}
|
||||||
fmt.Printf("### %s: %s\n\n", info["Path"], info["Title"])
|
fmt.Printf("### %s: %s\n\n", info["Path"], info["Title"])
|
||||||
fmt.Printf("%s\n\n", info["Help"])
|
fmt.Printf("%s\n\n", info["Help"])
|
||||||
|
if authRequired := info["AuthRequired"]; authRequired != nil {
|
||||||
|
if authRequired.(bool) {
|
||||||
|
fmt.Printf("Authentication is required for this call.\n\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue