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