2022-08-28 19:21:57 +08:00
|
|
|
// Package cat provides the cat command.
|
2016-08-19 05:43:02 +08:00
|
|
|
package cat
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2017-02-08 16:09:41 +08:00
|
|
|
"io"
|
2016-08-19 05:43:02 +08:00
|
|
|
"os"
|
2020-05-25 15:50:20 +08:00
|
|
|
"strings"
|
2016-08-19 05:43:02 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2024-08-18 22:58:35 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2019-10-11 23:55:04 +08:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2016-08-19 05:43:02 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2017-02-08 16:09:41 +08:00
|
|
|
// Globals
|
|
|
|
var (
|
2023-04-24 19:01:53 +08:00
|
|
|
head = int64(0)
|
|
|
|
tail = int64(0)
|
|
|
|
offset = int64(0)
|
|
|
|
count = int64(-1)
|
|
|
|
discard = false
|
|
|
|
separator = string("")
|
2017-02-08 16:09:41 +08:00
|
|
|
)
|
|
|
|
|
2016-08-19 05:43:02 +08:00
|
|
|
func init() {
|
2019-10-11 23:58:11 +08:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2019-10-11 23:55:04 +08:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
2023-07-11 01:34:10 +08:00
|
|
|
flags.Int64VarP(cmdFlags, &head, "head", "", head, "Only print the first N characters", "")
|
|
|
|
flags.Int64VarP(cmdFlags, &tail, "tail", "", tail, "Only print the last N characters", "")
|
|
|
|
flags.Int64VarP(cmdFlags, &offset, "offset", "", offset, "Start printing at offset N (or from end if -ve)", "")
|
|
|
|
flags.Int64VarP(cmdFlags, &count, "count", "", count, "Only print N characters", "")
|
|
|
|
flags.BoolVarP(cmdFlags, &discard, "discard", "", discard, "Discard the output instead of printing", "")
|
|
|
|
flags.StringVarP(cmdFlags, &separator, "separator", "", separator, "Separator to use between objects when printing multiple files", "")
|
2016-08-19 05:43:02 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 23:58:11 +08:00
|
|
|
var commandDefinition = &cobra.Command{
|
2016-08-19 05:43:02 +08:00
|
|
|
Use: "cat remote:path",
|
|
|
|
Short: `Concatenates any files and sends them to stdout.`,
|
2020-05-25 15:50:20 +08:00
|
|
|
// Warning! "|" will be replaced by backticks below
|
2024-08-13 00:17:46 +08:00
|
|
|
Long: strings.ReplaceAll(`Sends any files to standard output.
|
2016-08-19 05:43:02 +08:00
|
|
|
|
|
|
|
You can use it like this to output a single file
|
|
|
|
|
|
|
|
rclone cat remote:path/to/file
|
|
|
|
|
2020-02-10 23:07:40 +08:00
|
|
|
Or like this to output any file in dir or its subdirectories.
|
2016-08-19 05:43:02 +08:00
|
|
|
|
|
|
|
rclone cat remote:path/to/dir
|
|
|
|
|
2020-02-10 23:07:40 +08:00
|
|
|
Or like this to output any .txt files in dir or its subdirectories.
|
2016-08-19 05:43:02 +08:00
|
|
|
|
|
|
|
rclone --include "*.txt" cat remote:path/to/dir
|
2017-02-08 16:09:41 +08:00
|
|
|
|
2020-05-25 15:50:20 +08:00
|
|
|
Use the |--head| flag to print characters only at the start, |--tail| for
|
|
|
|
the end and |--offset| and |--count| to print a section in the middle.
|
2017-02-08 16:09:41 +08:00
|
|
|
Note that if offset is negative it will count from the end, so
|
2020-05-25 15:50:20 +08:00
|
|
|
|--offset -1 --count 1| is equivalent to |--tail 1|.
|
2023-04-24 19:01:53 +08:00
|
|
|
|
|
|
|
Use the |--separator| flag to print a separator value between files. Be sure to
|
|
|
|
shell-escape special characters. For example, to print a newline between
|
|
|
|
files, use:
|
|
|
|
|
|
|
|
* bash:
|
|
|
|
|
|
|
|
rclone --include "*.txt" --separator $'\n' cat remote:path/to/dir
|
|
|
|
|
|
|
|
* powershell:
|
|
|
|
|
|
|
|
rclone --include "*.txt" --separator "|n" cat remote:path/to/dir
|
2020-05-25 15:50:20 +08:00
|
|
|
`, "|", "`"),
|
2022-11-27 06:40:49 +08:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.33",
|
2023-07-11 01:34:10 +08:00
|
|
|
"groups": "Filter,Listing",
|
2022-11-27 06:40:49 +08:00
|
|
|
},
|
2016-08-19 05:43:02 +08:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
2017-02-08 16:09:41 +08:00
|
|
|
usedOffset := offset != 0 || count >= 0
|
|
|
|
usedHead := head > 0
|
|
|
|
usedTail := tail > 0
|
|
|
|
if usedHead && usedTail || usedHead && usedOffset || usedTail && usedOffset {
|
2024-08-18 22:58:35 +08:00
|
|
|
fs.Fatalf(nil, "Can only use one of --head, --tail or --offset with --count")
|
2017-02-08 16:09:41 +08:00
|
|
|
}
|
|
|
|
if head > 0 {
|
|
|
|
offset = 0
|
|
|
|
count = head
|
|
|
|
}
|
|
|
|
if tail > 0 {
|
|
|
|
offset = -tail
|
|
|
|
count = -1
|
|
|
|
}
|
2016-08-19 05:43:02 +08:00
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
fsrc := cmd.NewFsSrc(args)
|
2017-02-08 16:09:41 +08:00
|
|
|
var w io.Writer = os.Stdout
|
|
|
|
if discard {
|
2022-08-20 22:38:02 +08:00
|
|
|
w = io.Discard
|
2017-02-08 16:09:41 +08:00
|
|
|
}
|
2016-12-05 00:52:24 +08:00
|
|
|
cmd.Run(false, false, command, func() error {
|
2023-04-24 19:01:53 +08:00
|
|
|
return operations.Cat(context.Background(), fsrc, w, offset, count, []byte(separator))
|
2016-08-19 05:43:02 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|