cmd: new command lsf

This commit is contained in:
Jakub Tasiemski
2018-01-06 14:39:31 +00:00
committed by Nick Craig-Wood
parent 8f47d7fc06
commit 0d041602cf
11 changed files with 379 additions and 0 deletions
+78
View File
@@ -0,0 +1,78 @@
package lsf
import (
"fmt"
"io"
"os"
"github.com/ncw/rclone/cmd"
"github.com/ncw/rclone/fs"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
format string
separator string
dirSlash bool
recurse bool
)
func init() {
cmd.Root.AddCommand(commandDefintion)
flags := commandDefintion.Flags()
flags.StringVarP(&format, "format", "F", "", "Output format.")
flags.StringVarP(&separator, "separator", "s", "", "Separator.")
flags.BoolVarP(&dirSlash, "dir-slash", "d", false, "Dir name contains slash one the end.")
commandDefintion.Flags().BoolVarP(&recurse, "recursive", "R", false, "Recurse into the listing.")
}
var commandDefintion = &cobra.Command{
Use: "lsf remote:path",
Short: `List all the objects in the path with modification time, size and path in specific format: 'p' - path, 's' - size, 't' - modification time, ex. 'tsp'. Default output contains only path. If format is empty, dir-slash flag is always true.`,
Run: func(command *cobra.Command, args []string) {
cmd.CheckArgs(1, 1, command, args)
fsrc := cmd.NewFsSrc(args)
cmd.Run(false, false, command, func() error {
return Lsf(fsrc, os.Stdout)
})
},
}
//Lsf lists all the objects in the path with modification time, size and path in specific format.
func Lsf(fsrc fs.Fs, out io.Writer) error {
return fs.Walk(fsrc, "", false, fs.ConfigMaxDepth(recurse), func(path string, entries fs.DirEntries, err error) error {
if err != nil {
fs.Stats.Error(err)
fs.Errorf(path, "error listing: %v", err)
return nil
}
if format == "" {
format = "p"
dirSlash = true
}
if separator == "" {
separator = ";"
}
var list fs.ListFormat
list.SetSeparator(separator)
list.SetDirSlash(dirSlash)
for _, char := range format {
switch char {
case 'p':
list.AddPath()
case 't':
list.AddModTime()
case 's':
list.AddSize()
default:
return errors.Wrap(err, "failed to parse format argument")
}
}
for _, entry := range entries {
fmt.Fprintln(out, fs.ListFormatted(&entry, &list))
}
return nil
})
}
+194
View File
@@ -0,0 +1,194 @@
package lsf
import (
"bytes"
"testing"
"github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fstest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "github.com/ncw/rclone/local"
)
func TestDefaultLsf(t *testing.T) {
fstest.Initialise()
buf := new(bytes.Buffer)
f, err := fs.NewFs("testfiles")
require.NoError(t, err)
err = Lsf(f, buf)
require.NoError(t, err)
assert.Equal(t, `file1
file2
file3
subdir/
`, buf.String())
}
func TestRecurseFlag(t *testing.T) {
fstest.Initialise()
buf := new(bytes.Buffer)
f, err := fs.NewFs("testfiles")
require.NoError(t, err)
recurse = true
err = Lsf(f, buf)
require.NoError(t, err)
assert.Equal(t, `file1
file2
file3
subdir/
subdir/file1
subdir/file2
subdir/file3
`, buf.String())
recurse = false
}
func TestDirSlashFlag(t *testing.T) {
fstest.Initialise()
buf := new(bytes.Buffer)
f, err := fs.NewFs("testfiles")
require.NoError(t, err)
dirSlash = true
format = "p"
err = Lsf(f, buf)
require.NoError(t, err)
assert.Equal(t, `file1
file2
file3
subdir/
`, buf.String())
buf = new(bytes.Buffer)
dirSlash = false
err = Lsf(f, buf)
require.NoError(t, err)
assert.Equal(t, `file1
file2
file3
subdir
`, buf.String())
}
func TestFormat(t *testing.T) {
fstest.Initialise()
f, err := fs.NewFs("testfiles")
require.NoError(t, err)
buf := new(bytes.Buffer)
format = "p"
err = Lsf(f, buf)
require.NoError(t, err)
assert.Equal(t, `file1
file2
file3
subdir
`, buf.String())
buf = new(bytes.Buffer)
format = "s"
err = Lsf(f, buf)
require.NoError(t, err)
assert.Equal(t, `0
321
1234
-1
`, buf.String())
buf = new(bytes.Buffer)
format = "t"
err = Lsf(f, buf)
require.NoError(t, err)
items, _ := fs.ListDirSorted(f, true, "")
var expectedOutput string
for _, item := range items {
expectedOutput += item.ModTime().Format("2006-01-02 15:04:05") + "\n"
}
assert.Equal(t, expectedOutput, buf.String())
buf = new(bytes.Buffer)
format = "sp"
err = Lsf(f, buf)
require.NoError(t, err)
assert.Equal(t, `0;file1
321;file2
1234;file3
-1;subdir
`, buf.String())
format = ""
}
func TestSeparator(t *testing.T) {
fstest.Initialise()
f, err := fs.NewFs("testfiles")
require.NoError(t, err)
format = "ps"
buf := new(bytes.Buffer)
err = Lsf(f, buf)
require.NoError(t, err)
assert.Equal(t, `file1;0
file2;321
file3;1234
subdir;-1
`, buf.String())
separator = "__SEP__"
buf = new(bytes.Buffer)
err = Lsf(f, buf)
require.NoError(t, err)
assert.Equal(t, `file1__SEP__0
file2__SEP__321
file3__SEP__1234
subdir__SEP__-1
`, buf.String())
format = ""
separator = ""
}
func TestWholeLsf(t *testing.T) {
fstest.Initialise()
f, err := fs.NewFs("testfiles")
require.NoError(t, err)
format = "pst"
separator = "_+_"
recurse = true
dirSlash = true
buf := new(bytes.Buffer)
err = Lsf(f, buf)
require.NoError(t, err)
items, _ := fs.ListDirSorted(f, true, "")
itemsInSubdir, _ := fs.ListDirSorted(f, true, "subdir")
var expectedOutput []string
for _, item := range items {
expectedOutput = append(expectedOutput, item.ModTime().Format("2006-01-02 15:04:05"))
}
for _, item := range itemsInSubdir {
expectedOutput = append(expectedOutput, item.ModTime().Format("2006-01-02 15:04:05"))
}
assert.Equal(t, `file1_+_0_+_`+expectedOutput[0]+`
file2_+_321_+_`+expectedOutput[1]+`
file3_+_1234_+_`+expectedOutput[2]+`
subdir/_+_-1_+_`+expectedOutput[3]+`
subdir/file1_+_0_+_`+expectedOutput[4]+`
subdir/file2_+_1_+_`+expectedOutput[5]+`
subdir/file3_+_111_+_`+expectedOutput[6]+`
`, buf.String())
format = ""
separator = ""
recurse = false
dirSlash = false
}
View File
Binary file not shown.
Binary file not shown.
View File
Binary file not shown.
Binary file not shown.