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