Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to output file size and date/times in a machine readable format #139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -101,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 {
Expand All @@ -110,7 +119,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)
}
Expand Down Expand Up @@ -139,4 +148,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")
}
7 changes: 6 additions & 1 deletion cmd/revs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,19 @@ func revs(cmd *cobra.Command, args []string) (err error) {
return
}

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")
}

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)
}
Expand All @@ -69,4 +73,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")
}
8 changes: 7 additions & 1 deletion cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,20 @@ func search(cmd *cobra.Command, args []string) (err error) {
return
}

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")
}

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)
}
Expand All @@ -73,4 +78,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")
}
1 change: 1 addition & 0 deletions contrib/zsh-completion/_dbxcli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down