Alright, so I had some free time this afternoon. And I'm close, but I can't figure out how to get it to print the '\n' separator as a newline when passed in as an arg. It just prints it literally... I poked at it for an hour or two, but can't figure it out.
diff --git a/cmd/cat/cat.go b/cmd/cat/cat.go
index 03ed4e6e3..674f5b162 100644
--- a/cmd/cat/cat.go
+++ b/cmd/cat/cat.go
@@ -16,11 +16,12 @@ import (
// Globals
var (
- head = int64(0)
- tail = int64(0)
- offset = int64(0)
- count = int64(-1)
- discard = false
+ head = int64(0)
+ tail = int64(0)
+ offset = int64(0)
+ count = int64(-1)
+ discard = false
+ separator = string("")
)
func init() {
@@ -31,6 +32,7 @@ func init() {
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")
}
var commandDefinition = &cobra.Command{
@@ -82,7 +84,7 @@ Note that if offset is negative it will count from the end, so
w = io.Discard
}
cmd.Run(false, false, command, func() error {
- return operations.Cat(context.Background(), fsrc, w, offset, count)
+ return operations.Cat(context.Background(), fsrc, w, offset, count, []byte(separator))
})
},
}
diff --git a/fs/operations/operations.go b/fs/operations/operations.go
index 3f6fb6e73..78cf96b9a 100644
--- a/fs/operations/operations.go
+++ b/fs/operations/operations.go
@@ -1259,7 +1259,7 @@ type readCloser struct {
//
// if count < 0 then it will be ignored
// if count >= 0 then only that many characters will be output
-func Cat(ctx context.Context, f fs.Fs, w io.Writer, offset, count int64) error {
+func Cat(ctx context.Context, f fs.Fs, w io.Writer, offset, count int64, sep []byte) error {
var mu sync.Mutex
ci := fs.GetConfig(ctx)
return ListFn(ctx, f, func(o fs.Object) {
@@ -1301,6 +1301,14 @@ func Cat(ctx context.Context, f fs.Fs, w io.Writer, offset, count int64) error {
err = fs.CountError(err)
fs.Errorf(o, "Failed to send to output: %v", err)
}
+ if len(sep) >= 0 {
+ sepReader := bytes.NewReader(sep)
+ _, err = io.Copy(w, sepReader)
+ if err != nil {
+ err = fs.CountError(err)
+ fs.Errorf(o, "Failed to send seperator to output: %v", err)
+ }
+ }
})
}
When using rclone cat ... --separator '\n'
, that code will output:
"file1"\n"file2"\n"file3"\n
If I change the default value of the separator to string("\n")
and don't pass --separator
at all, then it actually does the right thing:
"file1"
"file2"
"file3"
So I guess there's some trick to reading and parsing the arg? Any tips on that?