1
mirror of https://github.com/rclone/rclone synced 2024-12-02 11:53:45 +01:00
rclone/cmd/cryptdecode/cryptdecode.go
Nick Craig-Wood f3f48d7d49 Implement new backend config system
This unifies the 3 methods of reading config

  * command line
  * environment variable
  * config file

And allows them all to be configured in all places.  This is done by
making the []fs.Option in the backend registration be the master
source of what the backend options are.

The backend changes are:

  * Use the new configmap.Mapper parameter
  * Use configstruct to parse it into an Options struct
  * Add all config to []fs.Option including defaults and help
  * Remove all uses of pflag
  * Remove all uses of config.FileGet
2018-07-16 21:20:47 +01:00

93 lines
2.2 KiB
Go

package cryptdecode
import (
"errors"
"fmt"
"github.com/ncw/rclone/backend/crypt"
"github.com/ncw/rclone/cmd"
"github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/config/flags"
"github.com/spf13/cobra"
)
// Options set by command line flags
var (
Reverse = false
)
func init() {
cmd.Root.AddCommand(commandDefinition)
flagSet := commandDefinition.Flags()
flags.BoolVarP(flagSet, &Reverse, "reverse", "", Reverse, "Reverse cryptdecode, encrypts filenames")
}
var commandDefinition = &cobra.Command{
Use: "cryptdecode encryptedremote: encryptedfilename",
Short: `Cryptdecode returns unencrypted file names.`,
Long: `
rclone cryptdecode returns unencrypted file names when provided with
a list of encrypted file names. List limit is 10 items.
If you supply the --reverse flag, it will return encrypted file names.
use it like this
rclone cryptdecode encryptedremote: encryptedfilename1 encryptedfilename2
rclone cryptdecode --reverse encryptedremote: filename1 filename2
`,
Run: func(command *cobra.Command, args []string) {
cmd.CheckArgs(2, 11, command, args)
cmd.Run(false, false, command, func() error {
fsInfo, _, _, config, err := fs.ConfigFs(args[0])
if err != nil {
return err
}
if fsInfo.Name != "crypt" {
return errors.New("The remote needs to be of type \"crypt\"")
}
cipher, err := crypt.NewCipher(config)
if err != nil {
return err
}
if Reverse {
return cryptEncode(cipher, args[1:])
}
return cryptDecode(cipher, args[1:])
})
},
}
// cryptDecode returns the unencrypted file name
func cryptDecode(cipher crypt.Cipher, args []string) error {
output := ""
for _, encryptedFileName := range args {
fileName, err := cipher.DecryptFileName(encryptedFileName)
if err != nil {
output += fmt.Sprintln(encryptedFileName, "\t", "Failed to decrypt")
} else {
output += fmt.Sprintln(encryptedFileName, "\t", fileName)
}
}
fmt.Printf(output)
return nil
}
// cryptEncode returns the encrypted file name
func cryptEncode(cipher crypt.Cipher, args []string) error {
output := ""
for _, fileName := range args {
encryptedFileName := cipher.EncryptFileName(fileName)
output += fmt.Sprintln(fileName, "\t", encryptedFileName)
}
fmt.Printf(output)
return nil
}