github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/cmd/cat/cat.go (about)

     1  package cat
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"io/ioutil"
     7  	"log"
     8  	"os"
     9  
    10  	"github.com/rclone/rclone/cmd"
    11  	"github.com/rclone/rclone/fs/config/flags"
    12  	"github.com/rclone/rclone/fs/operations"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  // Globals
    17  var (
    18  	head    = int64(0)
    19  	tail    = int64(0)
    20  	offset  = int64(0)
    21  	count   = int64(-1)
    22  	discard = false
    23  )
    24  
    25  func init() {
    26  	cmd.Root.AddCommand(commandDefinition)
    27  	cmdFlags := commandDefinition.Flags()
    28  	flags.Int64VarP(cmdFlags, &head, "head", "", head, "Only print the first N characters.")
    29  	flags.Int64VarP(cmdFlags, &tail, "tail", "", tail, "Only print the last N characters.")
    30  	flags.Int64VarP(cmdFlags, &offset, "offset", "", offset, "Start printing at offset N (or from end if -ve).")
    31  	flags.Int64VarP(cmdFlags, &count, "count", "", count, "Only print N characters.")
    32  	flags.BoolVarP(cmdFlags, &discard, "discard", "", discard, "Discard the output instead of printing.")
    33  }
    34  
    35  var commandDefinition = &cobra.Command{
    36  	Use:   "cat remote:path",
    37  	Short: `Concatenates any files and sends them to stdout.`,
    38  	Long: `
    39  rclone cat sends any files to standard output.
    40  
    41  You can use it like this to output a single file
    42  
    43      rclone cat remote:path/to/file
    44  
    45  Or like this to output any file in dir or subdirectories.
    46  
    47      rclone cat remote:path/to/dir
    48  
    49  Or like this to output any .txt files in dir or subdirectories.
    50  
    51      rclone --include "*.txt" cat remote:path/to/dir
    52  
    53  Use the --head flag to print characters only at the start, --tail for
    54  the end and --offset and --count to print a section in the middle.
    55  Note that if offset is negative it will count from the end, so
    56  --offset -1 --count 1 is equivalent to --tail 1.
    57  `,
    58  	Run: func(command *cobra.Command, args []string) {
    59  		usedOffset := offset != 0 || count >= 0
    60  		usedHead := head > 0
    61  		usedTail := tail > 0
    62  		if usedHead && usedTail || usedHead && usedOffset || usedTail && usedOffset {
    63  			log.Fatalf("Can only use one of  --head, --tail or --offset with --count")
    64  		}
    65  		if head > 0 {
    66  			offset = 0
    67  			count = head
    68  		}
    69  		if tail > 0 {
    70  			offset = -tail
    71  			count = -1
    72  		}
    73  		cmd.CheckArgs(1, 1, command, args)
    74  		fsrc := cmd.NewFsSrc(args)
    75  		var w io.Writer = os.Stdout
    76  		if discard {
    77  			w = ioutil.Discard
    78  		}
    79  		cmd.Run(false, false, command, func() error {
    80  			return operations.Cat(context.Background(), fsrc, w, offset, count)
    81  		})
    82  	},
    83  }