From 4a9cc279aae9691ebc1cce0e91379823d1e888f3 Mon Sep 17 00:00:00 2001 From: Athena Metis Date: Thu, 23 Apr 2020 15:36:03 +0100 Subject: [PATCH 1/2] Add option to output file size and date/times in a machine readable format rather than humanised --- cmd/ls.go | 12 +++++++++--- cmd/revs.go | 4 +++- cmd/search.go | 4 +++- contrib/zsh-completion/_dbxcli | 1 + 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/ls.go b/cmd/ls.go index 1b6be59..e276b3e 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -44,9 +44,13 @@ func printFolderMetadata(w io.Writer, e *files.FolderMetadata, longFormat bool) fmt.Fprintf(w, "%s\t", e.PathDisplay) } -func printFileMetadata(w io.Writer, e *files.FileMetadata, longFormat bool) { +func printFileMetadata(w io.Writer, e *files.FileMetadata, longFormat bool, machineReadable bool) { if longFormat { - fmt.Fprintf(w, "%s\t%s\t%s\t", e.Rev, humanize.IBytes(e.Size), humanize.Time(e.ServerModified)) + if (machineReadable) { + fmt.Fprintf(w, "%s\t%d\t%s\t", e.Rev, e.Size, e.ServerModified) + } else { + fmt.Fprintf(w, "%s\t%s\t%s\t", e.Rev, humanize.IBytes(e.Size), humanize.Time(e.ServerModified)) + } } fmt.Fprintf(w, "%s\t", e.PathDisplay) } @@ -62,6 +66,7 @@ func ls(cmd *cobra.Command, args []string) (err error) { arg := files.NewListFolderArg(path) arg.Recursive, _ = cmd.Flags().GetBool("recurse") + machineReadable, _ := cmd.Flags().GetBool("machine") res, err := dbx.ListFolder(arg) var entries []files.IsMetadata @@ -110,7 +115,7 @@ func ls(cmd *cobra.Command, args []string) (err error) { for i, entry := range entries { switch f := entry.(type) { case *files.FileMetadata: - printFileMetadata(w, f, long) + printFileMetadata(w, f, long, machineReadable) case *files.FolderMetadata: printFolderMetadata(w, f, long) } @@ -139,4 +144,5 @@ func init() { lsCmd.Flags().BoolP("long", "l", false, "Long listing") lsCmd.Flags().BoolP("recurse", "R", false, "Recursively list all subfolders") + lsCmd.Flags().BoolP("machine", "m", false, "Machine readable file size and time") } diff --git a/cmd/revs.go b/cmd/revs.go index 14f425b..342e6bd 100644 --- a/cmd/revs.go +++ b/cmd/revs.go @@ -42,6 +42,7 @@ func revs(cmd *cobra.Command, args []string) (err error) { } long, _ := cmd.Flags().GetBool("long") + machineReadable, _ := cmd.Flags().GetBool("machine") if long { fmt.Printf("Revision\tSize\tLast modified\tPath\n") @@ -49,7 +50,7 @@ func revs(cmd *cobra.Command, args []string) (err error) { for _, e := range res.Entries { if long { - printFileMetadata(os.Stdout, e, long) + printFileMetadata(os.Stdout, e, long, machineReadable) } else { fmt.Printf("%s\n", e.Rev) } @@ -69,4 +70,5 @@ func init() { RootCmd.AddCommand(revsCmd) revsCmd.Flags().BoolP("long", "l", false, "Long listing") + revsCmd.Flags().BoolP("machine", "m", false, "Machine readable file size and time") } diff --git a/cmd/search.go b/cmd/search.go index 1790d5d..b8c1e96 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -46,6 +46,7 @@ func search(cmd *cobra.Command, args []string) (err error) { return } + machineReadable, _ := cmd.Flags().GetBool("machine") long, _ := cmd.Flags().GetBool("long") if long { fmt.Printf("Revision\tSize\tLast modified\tPath\n") @@ -54,7 +55,7 @@ func search(cmd *cobra.Command, args []string) (err error) { for _, m := range res.Matches { switch f := m.Metadata.(type) { case *files.FileMetadata: - printFileMetadata(os.Stdout, f, long) + printFileMetadata(os.Stdout, f, long, machineReadable) case *files.FolderMetadata: printFolderMetadata(os.Stdout, f, long) } @@ -73,4 +74,5 @@ var searchCmd = &cobra.Command{ func init() { RootCmd.AddCommand(searchCmd) searchCmd.Flags().BoolP("long", "l", false, "Long listing") + searchCmd.Flags().BoolP("machine", "m", false, "Machine readable file size and time") } diff --git a/contrib/zsh-completion/_dbxcli b/contrib/zsh-completion/_dbxcli index 8717a8d..45012b4 100644 --- a/contrib/zsh-completion/_dbxcli +++ b/contrib/zsh-completion/_dbxcli @@ -24,6 +24,7 @@ function _dbxcli() { _arguments -C \ '(-l --long)'{-l,--long}'[Long listing]' \ '(-h --help)'{-h,--help}'[Print information about a command]' \ + '(-m --machine)'{-m,--machine}'[Output machine readable file sizes and times]' \ '--token[(string) Access token]' \ '(-v --verbose)'{-v,--verbose}'[Enable verbose logging]' \ && ret=0 From bc531eef234ad7a2172725b53501e60fe7bfd0c6 Mon Sep 17 00:00:00 2001 From: Athena Metis Date: Thu, 23 Apr 2020 16:40:17 +0100 Subject: [PATCH 2/2] Imply long flag if machine is set, as this is the only view the machine flag affects --- cmd/ls.go | 6 +++++- cmd/revs.go | 5 ++++- cmd/search.go | 4 ++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/cmd/ls.go b/cmd/ls.go index e276b3e..9ae3d0f 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -66,7 +66,6 @@ func ls(cmd *cobra.Command, args []string) (err error) { arg := files.NewListFolderArg(path) arg.Recursive, _ = cmd.Flags().GetBool("recurse") - machineReadable, _ := cmd.Flags().GetBool("machine") res, err := dbx.ListFolder(arg) var entries []files.IsMetadata @@ -106,7 +105,12 @@ func ls(cmd *cobra.Command, args []string) (err error) { } } + machineReadable, _ := cmd.Flags().GetBool("machine") long, _ := cmd.Flags().GetBool("long") + + //If machine is set imply long + long = long || machineReadable + w := new(tabwriter.Writer) w.Init(os.Stdout, 4, 8, 1, ' ', 0) if long { diff --git a/cmd/revs.go b/cmd/revs.go index 342e6bd..25de4c5 100644 --- a/cmd/revs.go +++ b/cmd/revs.go @@ -41,8 +41,11 @@ func revs(cmd *cobra.Command, args []string) (err error) { return } - long, _ := cmd.Flags().GetBool("long") machineReadable, _ := cmd.Flags().GetBool("machine") + long, _ := cmd.Flags().GetBool("long") + + //If machine is set imply long + long = long || machineReadable if long { fmt.Printf("Revision\tSize\tLast modified\tPath\n") diff --git a/cmd/search.go b/cmd/search.go index b8c1e96..80398ac 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -48,6 +48,10 @@ func search(cmd *cobra.Command, args []string) (err error) { machineReadable, _ := cmd.Flags().GetBool("machine") long, _ := cmd.Flags().GetBool("long") + + //If machine is set imply long + long = long || machineReadable + if long { fmt.Printf("Revision\tSize\tLast modified\tPath\n") }