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

     1  package lsjson
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/pkg/errors"
    10  	"github.com/rclone/rclone/cmd"
    11  	"github.com/rclone/rclone/cmd/ls/lshelp"
    12  	"github.com/rclone/rclone/fs/config/flags"
    13  	"github.com/rclone/rclone/fs/operations"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  var (
    18  	opt operations.ListJSONOpt
    19  )
    20  
    21  func init() {
    22  	cmd.Root.AddCommand(commandDefinition)
    23  	cmdFlags := commandDefinition.Flags()
    24  	flags.BoolVarP(cmdFlags, &opt.Recurse, "recursive", "R", false, "Recurse into the listing.")
    25  	flags.BoolVarP(cmdFlags, &opt.ShowHash, "hash", "", false, "Include hashes in the output (may take longer).")
    26  	flags.BoolVarP(cmdFlags, &opt.NoModTime, "no-modtime", "", false, "Don't read the modification time (can speed things up).")
    27  	flags.BoolVarP(cmdFlags, &opt.NoMimeType, "no-mimetype", "", false, "Don't read the mime type (can speed things up).")
    28  	flags.BoolVarP(cmdFlags, &opt.ShowEncrypted, "encrypted", "M", false, "Show the encrypted names.")
    29  	flags.BoolVarP(cmdFlags, &opt.ShowOrigIDs, "original", "", false, "Show the ID of the underlying Object.")
    30  	flags.BoolVarP(cmdFlags, &opt.FilesOnly, "files-only", "", false, "Show only files in the listing.")
    31  	flags.BoolVarP(cmdFlags, &opt.DirsOnly, "dirs-only", "", false, "Show only directories in the listing.")
    32  }
    33  
    34  var commandDefinition = &cobra.Command{
    35  	Use:   "lsjson remote:path",
    36  	Short: `List directories and objects in the path in JSON format.`,
    37  	Long: `List directories and objects in the path in JSON format.
    38  
    39  The output is an array of Items, where each Item looks like this
    40  
    41     {
    42        "Hashes" : {
    43           "SHA-1" : "f572d396fae9206628714fb2ce00f72e94f2258f",
    44           "MD5" : "b1946ac92492d2347c6235b4d2611184",
    45           "DropboxHash" : "ecb65bb98f9d905b70458986c39fcbad7715e5f2fcc3b1f07767d7c83e2438cc"
    46        },
    47        "ID": "y2djkhiujf83u33",
    48        "OrigID": "UYOJVTUW00Q1RzTDA",
    49        "IsBucket" : false,
    50        "IsDir" : false,
    51        "MimeType" : "application/octet-stream",
    52        "ModTime" : "2017-05-31T16:15:57.034468261+01:00",
    53        "Name" : "file.txt",
    54        "Encrypted" : "v0qpsdq8anpci8n929v3uu9338",
    55        "EncryptedPath" : "kja9098349023498/v0qpsdq8anpci8n929v3uu9338",
    56        "Path" : "full/path/goes/here/file.txt",
    57        "Size" : 6,
    58        "Tier" : "hot",
    59     }
    60  
    61  If --hash is not specified the Hashes property won't be emitted.
    62  
    63  If --no-modtime is specified then ModTime will be blank. This can speed things up on remotes where reading the ModTime takes an extra request (eg s3, swift).
    64  
    65  If --no-mimetype is specified then MimeType will be blank. This can speed things up on remotes where reading the MimeType takes an extra request (eg s3, swift).
    66  
    67  If --encrypted is not specified the Encrypted won't be emitted.
    68  
    69  If --dirs-only is not specified files in addition to directories are returned
    70  
    71  If --files-only is not specified directories in addition to the files will be returned.
    72  
    73  The Path field will only show folders below the remote path being listed.
    74  If "remote:path" contains the file "subfolder/file.txt", the Path for "file.txt"
    75  will be "subfolder/file.txt", not "remote:path/subfolder/file.txt".
    76  When used without --recursive the Path will always be the same as Name.
    77  
    78  If the directory is a bucket in a bucket based backend, then
    79  "IsBucket" will be set to true. This key won't be present unless it is
    80  "true".
    81  
    82  The time is in RFC3339 format with up to nanosecond precision.  The
    83  number of decimal digits in the seconds will depend on the precision
    84  that the remote can hold the times, so if times are accurate to the
    85  nearest millisecond (eg Google Drive) then 3 digits will always be
    86  shown ("2017-05-31T16:15:57.034+01:00") whereas if the times are
    87  accurate to the nearest second (Dropbox, Box, WebDav etc) no digits
    88  will be shown ("2017-05-31T16:15:57+01:00").
    89  
    90  The whole output can be processed as a JSON blob, or alternatively it
    91  can be processed line by line as each item is written one to a line.
    92  ` + lshelp.Help,
    93  	Run: func(command *cobra.Command, args []string) {
    94  		cmd.CheckArgs(1, 1, command, args)
    95  		fsrc := cmd.NewFsSrc(args)
    96  		cmd.Run(false, false, command, func() error {
    97  			fmt.Println("[")
    98  			first := true
    99  			err := operations.ListJSON(context.Background(), fsrc, "", &opt, func(item *operations.ListJSONItem) error {
   100  				out, err := json.Marshal(item)
   101  				if err != nil {
   102  					return errors.Wrap(err, "failed to marshal list object")
   103  				}
   104  				if first {
   105  					first = false
   106  				} else {
   107  					fmt.Print(",\n")
   108  				}
   109  				_, err = os.Stdout.Write(out)
   110  				if err != nil {
   111  					return errors.Wrap(err, "failed to write to output")
   112  				}
   113  				return nil
   114  			})
   115  			if err != nil {
   116  				return err
   117  			}
   118  			if !first {
   119  				fmt.Println()
   120  			}
   121  			fmt.Println("]")
   122  			return nil
   123  		})
   124  	},
   125  }