2022-05-14 17:54:35 +00:00
package fs
2022-05-13 14:21:47 +00:00
import (
2023-06-05 11:49:33 +00:00
"io/fs"
2023-06-04 23:06:42 +00:00
"sort"
2023-06-05 11:49:33 +00:00
"time"
2022-05-13 14:21:47 +00:00
2023-06-02 15:24:04 +00:00
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/filer"
2022-05-13 14:21:47 +00:00
"github.com/spf13/cobra"
)
2023-06-05 11:49:33 +00:00
type jsonDirEntry struct {
Name string ` json:"name" `
IsDir bool ` json:"is_directory" `
Size int64 ` json:"size" `
ModTime time . Time ` json:"last_modified" `
}
func toJsonDirEntry ( f fs . DirEntry ) ( * jsonDirEntry , error ) {
info , err := f . Info ( )
if err != nil {
return nil , err
}
return & jsonDirEntry {
Name : f . Name ( ) ,
IsDir : f . IsDir ( ) ,
Size : info . Size ( ) ,
ModTime : info . ModTime ( ) ,
} , nil
}
2022-05-13 14:21:47 +00:00
// lsCmd represents the ls command
var lsCmd = & cobra . Command {
2023-06-02 15:24:04 +00:00
Use : "ls <dir-name>" ,
Short : "Lists files" ,
Long : ` Lists files ` ,
Args : cobra . ExactArgs ( 1 ) ,
PreRunE : root . MustWorkspaceClient ,
Annotations : map [ string ] string {
"template" : cmdio . Heredoc ( `
{ { range . } } { { . Name } }
{ { end } }
` ) ,
} ,
2022-09-16 09:06:58 +00:00
2023-04-11 14:59:27 +00:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2023-06-02 15:24:04 +00:00
ctx := cmd . Context ( )
w := root . WorkspaceClient ( ctx )
2023-06-04 23:34:28 +00:00
path , err := filer . ResolveDbfsPath ( args [ 0 ] )
2023-06-02 15:24:04 +00:00
if err != nil {
return err
}
2023-06-04 23:34:28 +00:00
f , err := filer . NewDbfsClient ( w , path )
2023-06-02 15:24:04 +00:00
if err != nil {
return err
}
entries , err := f . ReadDir ( ctx , "" )
if err != nil {
return err
}
2023-06-05 11:49:33 +00:00
lsOutputs := make ( [ ] jsonDirEntry , 0 )
2023-06-02 15:24:04 +00:00
for _ , entry := range entries {
2023-06-05 11:49:33 +00:00
parsedEntry , err := toJsonDirEntry ( entry )
2023-06-02 15:24:04 +00:00
if err != nil {
return err
}
lsOutputs = append ( lsOutputs , * parsedEntry )
2023-06-04 23:06:42 +00:00
sort . Slice ( lsOutputs , func ( i , j int ) bool {
return lsOutputs [ i ] . Name < lsOutputs [ j ] . Name
} )
2023-06-02 15:24:04 +00:00
}
// Use template for long mode if the flag is set
if longMode {
2023-06-05 11:49:33 +00:00
return cmdio . RenderWithTemplate ( ctx , lsOutputs , cmdio . Heredoc ( `
{ { range . } } { { if . IsDir } } DIRECTORY { { else } } FILE { { end } } { { . Size } } { { . ModTime | pretty_date } } { { . Name } }
{ { end } }
` ) )
2023-06-02 15:24:04 +00:00
}
return cmdio . Render ( ctx , lsOutputs )
2022-05-13 14:21:47 +00:00
} ,
}
2023-06-02 15:24:04 +00:00
var longMode bool
2022-05-13 14:21:47 +00:00
func init ( ) {
2023-06-02 15:24:04 +00:00
lsCmd . Flags ( ) . BoolVarP ( & longMode , "long" , "l" , false , "Displays full information including size, file type and modification time since Epoch in milliseconds." )
2022-05-13 14:21:47 +00:00
fsCmd . AddCommand ( lsCmd )
}